SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
Joining three or more tables in SQL is a continuation of the same principles used when joining two tables. You essentially chain the joins, specifying the join conditions for each pair of tables.
Consider the following three tables:
users
: user_id
, username
orders
: order_id
, user_id
, product_id
products
: product_id
, product_name
Example: To find out which user ordered which product, you need to join all three tables.
SELECT users.username, products.product_name FROM users JOIN orders ON users.user_id = orders.user_id JOIN products ON orders.product_id = products.product_id;
Here's a breakdown:
users
is joined with orders
based on user_id
.products
based on product_id
.You can also combine different types of joins:
SELECT users.username, products.product_name FROM users LEFT JOIN orders ON users.user_id = orders.user_id INNER JOIN products ON orders.product_id = products.product_id;
In this example:
LEFT JOIN
between users
and orders
. This means that even if a user hasn't made any orders, they'll still appear in the intermediate result.INNER JOIN
with products
. This means only those rows with a valid product will appear in the final result.database_name.table_name
).Joining Multiple Tables in SQL Query:
SELECT employees.employee_id, employees.employee_name, departments.department_name, projects.project_name FROM employees INNER JOIN departments ON employees.department_id = departments.department_id INNER JOIN employee_projects ON employees.employee_id = employee_projects.employee_id INNER JOIN projects ON employee_projects.project_id = projects.project_id;
How to Perform a Three-Table Join in SQL:
SELECT * FROM table1 INNER JOIN table2 ON table1.column_id = table2.column_id INNER JOIN table3 ON table2.column_id = table3.column_id;
Joining Four or More Tables in a Single SQL Query:
SELECT * FROM table1 INNER JOIN table2 ON table1.column_id = table2.column_id INNER JOIN table3 ON table2.column_id = table3.column_id INNER JOIN table4 ON table3.column_id = table4.column_id;
SQL INNER JOIN with Three or More Tables:
SELECT * FROM table1 INNER JOIN table2 ON table1.column_id = table2.column_id INNER JOIN table3 ON table2.column_id = table3.column_id INNER JOIN table4 ON table3.column_id = table4.column_id;
Complex Multi-Table Joins in SQL Explained:
SELECT * FROM employees INNER JOIN departments ON employees.department_id = departments.department_id LEFT OUTER JOIN employee_projects ON employees.employee_id = employee_projects.employee_id INNER JOIN projects ON employee_projects.project_id = projects.project_id WHERE projects.status = 'Active';
Outer Join with Three or More Tables in SQL:
SELECT * FROM table1 LEFT OUTER JOIN table2 ON table1.column_id = table2.column_id INNER JOIN table3 ON table2.column_id = table3.column_id;
Joining Tables Using Subqueries in SQL:
SELECT * FROM table1 INNER JOIN ( SELECT column_id, column_name FROM table2 WHERE condition ) AS subquery ON table1.column_id = subquery.column_id;