Joining three tables in single SQL query can be very tricky if you are not good with the concept of SQL Join. SQL Joins have always been tricky not only for new programmers but for many others, who are in programming and SQL for more than 2 to 3 years. There are enough to confuse someone on SQL JOIN ranging from various types of SQL JOIN like INNER and OUTER join, LEFT and RIGHT outer join, CROSS join etc. Between all of these fundamentals, What is most important about Join is, combining multiple tables. If you need data from multiple tables in one SELECT query you need to use either subquery or JOIN. Most of the times we only join two tables like Employee and Department but sometimes you may require joining more than two tables and a popular case is joining three tables in SQL.
In the case of joining three tables table, 1 relates to table 2 and then table 2 relates to table 3. If you look at closely you find that table 2 is a joining table which contains primary key from both table 1 and table 2. As I said it can be extremely confusing to understand join of three or more tables.

I have found that understanding table relationship as the primary key and foreign key helps to alleviate confusion than the classical matching row paradigm.

SQL Join is also a very popular topic in SQL interviews and there are always been some questions from Joins, like the difference between INNER and OUTER JOIN,SQL query with JOIN e.g. EmployeeDepartmentrelationship andDifference between LEFT and RIGHT OUTER JOIN etc. In short, this is one of the most important topics in SQL both from experience and interview point of view.

Btw, the only way to master SQL join is doing as much exercise as possible. If you could solve most of SQL puzzles from Joe Celko's classic book, SQL Puzzles, and Answers, 2nd edition, you will more confident about dealing with SQL joins, whether it could be two, three or four tables.

Three table JOIN syntax in SQL

Here is a general SQL query syntax to join three or more table. This SQL query should work in all major relation database e.g. MySQL, Oracle, Microsoft SQLServer, Sybase, and PostgreSQL:

We first join table 1 and table 2 which produce a temporary table with combined data from table1 and table2, which is then joined to table3. This formula can be extended to more than 3 tables to N tables, You just need to make sure that SQL query should have N-1 join statement in order to join N tables. like for joining two tables we require 1 join statement and for joining 3 tables we need 2 join statement.

Here is a nice diagram which also shows how does different types of JOINs e.g. inner, left outer, right outer and cross joins works in SQL:

SQL Query to JOIN three tables in MySQL

In order to better understand joining of 3 tables in SQL query let's see an example. Consider the popular example of Employee and Department schema. In our case, we have used a link table called Register which links or relate both Employee to Department. The primary key of Employee table (emp_id) is a foreign key in Register and similarly, primary key of Department table (dept_id) is a foreign key in Register table.

In order to write an SQL query to print employee name and department name alongside we need to join 3 tables. First JOIN statement will join Employee and Register and create a temporary table which will have dept_id as another column. Now second JOIN statement will join this temp table with Department table on dept_id to get the desired result. Here is the complete SELECT SQL query example to join 3 tables and it can be extended to join more than 3 or N tables.

If you want to understand it even better than try joining tables step by step. So instead of joining 3 tables in one go, first join 2 tables and see how the result table will look like. That’s all on How to join three tables in one SQL query in the relational database.

By the way, in this SQL JOIN Example, we have used ANSI SQL and it will work in another relational database as well e.g. Oracle, SQL Server, Sybase, PostgreSQL etc. Let us know if you face any issue while running this 3 table JOIN query in any other database.

Thanks for reading this article so far, if you like this article then please share with your friends and colleagues. If you have any questions, suggestions or doubt then please drop a comment and I'll try to answer your question.

24 comments
:

Tim
said...

From long time I had difficulty joining more than two tables, as soon as number of tables increased from 2 to 3, it's started getting messy for me. This particular example of How to join three tables in SQL made my life easy. keep the good work. Can you also share some JOIN examples from other major database e.g. Oracle, SQLServer 2008 and SQL Server 2010 please

I love this example. Just one thing must be informed to the reader. Remember that when we JOIN any two tables together on any condition the resultant table contains ALL THE COLUMNs HEADINGS of the two table. KEEP this thing clear it will solve the problem when JOIN works with three or more tables.

You can use L2.id or L1.id on second join syntanx depending upon your requirement. Remember, when we join two tables a third temporary table is created which contains all columns of both tables, so you are free to use key from any of the table.

Be careful, if a product is not associated to any category then the JOIN will discard it. If a product is not associated to any accessory, then it will be discarded. JOIN will silently remove a row if the "ON" condition is not fulfilled for that specific row. Use "LEFT JOIN" instead of "JOIN" to see these rows (select all the columns that are used on any of the ON conditions, the rows with NULL values are the ones that are failing one or more ON conditions depending on which column in NULL).