[Solved] How to join 3 and more tables in SQL?


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!

0

solved How to join 3 and more tables in SQL?