How to Join 3 Tables in SQL?

Learn via video course
FREE
View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Topics Covered

In SQL, we can join n number of tables to produce and fetch the desired result from the SQL queries. You can use JOIN in SQL to join two tables. Also, using JOIN in SQL doesn't mean you can join only two tables. It's not as difficult as it sounds to join more than two tables in SQL. You can join 3 tables in SQL using two JOIN keywords and this is explained with the examples below.

In this article, we'll examine various techniques for merging three tables together while keeping the value of n equal to three. You can join 3 tables in SQL using the JOIN clause in SQL. This method can also be used if you want to join more than 3 tables.

Joining 3 Tables Using a Junction Table

When using three tables, one of them is the Junction table, whose primary purpose is to join the other two tables together. In this article, we'll understand with an example how you can use the junction table to merge the three given tables. You can have a look at the junction table below and observe that atleast one of the columns from the junction table is also present in the other two tables so that two tables can JOIN with the help of the JUNCTION table.

junction-table-used-join-three-table [IMAGE 1 FINISH SAMPLE]

To extract the necessary rows, many queries can be developed, but if the dataset contains a large number of rows, they are not efficient and take a lot of time. Therefore, it is best to only write one line of the query.

SQL has 4 types of JOINS i.e., INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN. These different types of JOINS can be leveraged accordingly to join 3 tables. In the below examples, we'll use the LEFT JOIN in SQL.

Some points to consider here are:

  1. All the 3 tables must have some relationship with each other through a foreign key.
  2. Each table must have a common column of data.
  3. The name and datatype of the common column can be same the or different.
  4. The common column of the first table needs to be Primary Key and that of the second table needs to be the Foreign Key of that table.

Now, let's take an example to understand how to join 3 tables in SQL using a junction table. For example, let's consider the below-given tables that are available in our schema and all the examples make use of the below tables only.

Example:

Employees:

emp_idemp_nameemp_agemanager_id
1Roan Kent291010
2Beau Merrill311015
3Reiss Mullen291010
4Neve Michael281095
5Fateh Mcgrath301020
6Saima Joyner311050
7Nastya Drova321015
8Mark Dwell301095
9Lisa Maywell311030

Department:

dept_iddept_namedept_locationmanager_id
201FinanceRussia1050
401AdminIndia1010
501FinanceNetherlands1020
601ITIreland1095
801SalesUAE1050
901ITUSA1020

Salary:

dept_iddept_locationsalary_grademin_salarymax_salary
401UAEII3000060000
901RussiaIII4000050000
801NetherlandsIV3000060000
501USAIII3000050000
201IrelandI6000080000
601IndiaII4000070000

Information:

idemp_countrymanager_profile
1USALaissez-faire
5NetherlandsDemocratic
8UAEServantLeadership

Now, in this example we want to JOIN the Employees and Salary table but as there aren't any common columns between the two tables so we have to use a junction table i.e. Department table. The department table helps to merge the Employees and Salary table as the department table has such columns which are present in both the Employees and the Department table.

Let's take a look at the SQL query below,

SQL

Here is the result of the above query,

Output:

Joining SQL Tables Without a Junction Table

Now, we'll join the Employees and Information table. As you can observe that there are no columns present which is common so we'll use all the tables in the schema to join the tables.

Let's take an example in which the junction table is not present but still you can join the tables via another table.

Example:

The result of the above query is given below:

Output:

Learn More

Conclusion

  • JOIN statements with ON conditions are one of the essential techniques for joining three tables.
  • As you learned from the article that joining three tables in SQL isn't hard as it sounds. Although you can join as many tables as you want and the idea behind it is the same as joining two tables.
  • If there is a junction table then joining two or more tables could be an easy task.
  • If there are not any junction tables present in the schema then also you can join the tables after finding similar columns in the table that are present in the schema.