Please help me in joining 3 and more tables in SQL with simple syntax. I was not able to find the exact syntax for this.
Asked
Active
Viewed 140 times
-5
-
There you go http://www.w3schools.com/sql/sql_join_inner.asp – sagi Feb 22 '16 at 09:12
-
Hi Mureinik, I tried with these links. https://technet.microsoft.com/en-us/library/ms191430%28v=sql.105%29.aspx http://www.codeproject.com/Articles/435694/Understanding-Table-Joins-using-SQL#_articleTop – Kanagaraj Subramaniam Feb 22 '16 at 09:23
-
Be more concrete. Give us an example SQL not the pages you visited. – wumpz Feb 22 '16 at 09:29
2 Answers
1
Assuming that you have table 1, table 2 and table 3. Let´s create a simple example.
table 1: employees: id, department_id, first_name, last_name, salary...
table 2: departments: id, location_id, department_name...
table 3: locations: id, city...
department_id and location_id are foreign keys. Employees have a department_id and Departments have a location_id. You need this foreign keys in order to JOIN different tables.
Then you can use this query to join the tables:
SELECT first_name, salary, department_name, city
FROM departments JOIN employees USING (department_id)
JOIN locations USING (location_id)
GROUP BY first_name, salary, department_name, city;
If you want to learn more about different kind of joins I found a good explanation here.
Hope it helps!
Community
- 1
- 1
Diego Vidal
- 954
- 7
- 21