Trusted answers to developer questions

How to join 3 or more tables in SQL

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

In a database, information is typically separated into different tables. The JOIN (or INNER JOIN) statement enables us to combine the columns we require into a single table. The syntax of joining 3 or more tables is the same as joining 2 tables:​

JOIN, INNER JOIN and , can be used to join tables. However, if a , is used, then the WHERE clause needs to be used instead of the ON clause.

1. Simple Join

First, all the tables are joined using the JOIN keyword, then the WHERE clause is used:

FROM Employee e JOIN Salary s JOIN Department d
        WHERE e.ID = s.Emp_ID AND e.Dep_ID = d.ID

2. Nested Join

The nested JOIN statement is used with the ON keyword:

SELECT e.ID, e.Name, s.Salary, d.Name
FROM (Employee e JOIN Salary s ON e.ID = s.Emp_ID)
  JOIN Department d ON e.Dep_ID = d.ID
Joining 3 tables
Joining 3 tables

RELATED TAGS

join
table
sql
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?