Join Multiple Tables in SQL
Overview
We use multiple tables joins to combine data from more than two tables. The join operator is used multiple times to join multiple tables in SQL, as for each new table, one join is added. In SQL, joining multiple tables means you can join number of tables, but, to join number of tables, the joins required are , that is for tables, joins are required.
Introduction
Note: Ensure that you are familiar with SQL Joins before continuing. You can refer to this article on Scaler Topics for more information.
We often need to get data from three or more tables to get meaningful information. This can be accomplished using a multiple-table join. Data can be retrieved using different types of joins like INNER JOIN, LEFT JOIN, or both based on different requirements.
Let's find out how to use them. Let us assume that there are three tables in our database Student, Branch, and Address.
Student Table
The Student table contains Stud_id as the primary key, Name, Br_id as the foreign key referring Branch table, Email and City_id as the foreign key referring Address table.
Stud_id | Name | Br_id | City_id | |
---|---|---|---|---|
1001 | Ankit | 101 | ankit@bmail.com | 1 |
1002 | Pranav | 105 | pranav@bmail.com | 2 |
1003 | Raj | 102 | raj@bmail.com | 2 |
1004 | Shyam | 112 | shyam@bmail.com | 4 |
1005 | Duke | 112 | duke@bmail.com | 2 |
1006 | Jhon | 102 | jhon@bmail.com | 3 |
1007 | Aman | 101 | aman@bmail.com | 4 |
1008 | Pavan | 111 | pavan@bmail.com | 13 |
1009 | Virat | 112 | virat@bmail.com | 12 |
You can use the below query to create the above table.
Query:
Branch Table
The Branch table contains Br_id as primary key, Br_name, HOD and Contact.
Br_id | Br_name | HOD | Contact |
---|---|---|---|
101 | CSE | SH Rao | 22345 |
102 | MECH | AP Sharma | 28210 |
103 | EXTC | VK Reddy | 34152 |
104 | CHEM | SK Mehta | 45612 |
105 | IT | VL Shelke | 22521 |
106 | AI | KH Verma | 12332 |
107 | PROD | PG Kakde | 90876 |
You can use the below query to create the above table.
Address Table
The Address table contains city_id as the primary key, City and Pincode.
City_id | City | Pincode |
---|---|---|
1 | Mumbai | 400121 |
2 | Pune | 450011 |
3 | Lucknow | 553001 |
4 | Delhi | 443221 |
5 | Kolkata | 213445 |
6 | Chennai | 345432 |
7 | Nagpur | 323451 |
8 | Sri Nagar | 321321 |
You can use the below query to create the above table.
Join multiple tables using INNER JOIN
In SQL, an inner join obtains common rows from all of the tables mentioned in a query. In our case, if we see the Student and Branch table have Br_id common and the Address and Student table has city_id common. So, to retrieve data, first, we need to join two tables and then the third table. Let's take an example to make it more clear.
Example: Obtain students' names along with their branch names, HOD, city, and Pincode.
Query:
Output:
Name | Br_id | HOD | City | Pincode |
---|---|---|---|---|
Ankit | 101 | SH Rao | Mumbai | 400121 |
Pranav | 105 | VL Shelke | Pune | 450011 |
Raj | 102 | AP Sharma | Pune | 450011 |
Jhon | 102 | AP Sharma | Lucknow | 553001 |
Aman | 101 | SH Rao | Delhi | 443221 |
Explanation: So to retrieve data from more than two tables, we need to use the JOIN condition multiple times. The first join creates the virtual table, which has data from the first two tables, and then the second JOIN condition is applied to that virtual table. Here, we first INNER JOIN two tables, Branch and Student, using Br_id, then another INNER JOIN added to the output of the first two tables using city_id.
Note: A virtual table is a table that is created in memory, when we join three tables, the result from joining the first two tables is stored in a virtual table, and when the second join is applied, it is applied on the virtual table.
Join Multiple Tables Using LEFT JOIN
When we use left join on the multiple tables, it will include all the rows from the left table, that is the table mentioned on the left side of the join and the next left joins will be applied to the result of the first left join.
Let's take an example to understand this.
Example: Obtain all the names of the students even if they are not present in any City and Branch.
Query:
Output:
Name | City | Br_name |
---|---|---|
Ankit | Mumbai | CSE |
Pranav | Pune | IT |
Raj | Pune | MECH |
Shyam | Delhi | NULL |
Duke | Pune | NULL |
Jhon | Lucknow | MECH |
Aman | Delhi | CSE |
Pavan | NULL | NULL |
Virat | NULL | NULL |
Explanation: In this query, the first left join gets all the rows with the names of students from the table along with all the Br_name. If the Name is not present in any branch the Br_name field we get for that Name is NULL. On this table, another left join is applied on the table which keeps all the Name and Br_name as it is and returns all rows from the city column. If the Name and Br_name are not present in any City the City field we get for those Name and Br_name is Null.
Join multiple tables using both – INNER JOIN & LEFT JOIN
The combination of inner join and left join can be used in SQL query where the order of join decides the result of the query.
Let's take an example to understand this.
Example: Obtain all the Student Names even if they are not present in any Branch whereas excluding the Name which is not present in any city.
Query:
Output:
Name | City | Br_name |
---|---|---|
Ankit | Mumbai | CSE |
Pranav | Pune | IT |
Raj | Pune | MECH |
Shyam | Delhi | NULL |
Duke | Pune | NULL |
Jhon | Lucknow | MECH |
Aman | Delhi | CSE |
Explanation:
In this query, first all the names from the table are returned and the Br_name is returned even if the name does not have any Br_name by using left join. On this result, an inner join is applied with the table which eliminates the name with no city.
Using Parent-Child Relationship
The parent-child states the Primary key Foreign key relationship between two tables. The table has a primary key is stated as parent and table with foreign key is stated as a child. Using this method you can simply join multiple tables in SQL.
Let's take the same example we took for inner join and solve it by the parent-child relationship method.
Example: Obtain Names of students along with their branch name, their HOD, city, and Pincode.
Query:
Output:
Name | Br_id | HOD | City | Pincode |
---|---|---|---|---|
Ankit | 101 | SH Rao | Mumbai | 400121 |
Pranav | 105 | VL Shelke | Pune | 450011 |
Raj | 102 | AP Sharma | Pune | 450011 |
Jhon | 102 | AP Sharma | Lucknow | 553001 |
Aman | 101 | SH Rao | Delhi | 443221 |
Explanation: As you can see, the output from the inner join query and this query are the same. In this query, Br_id is the primary key of the table and the foreign key of the table. Similarly, City_id is the primary key to the table and the foreign key to the table. This is an alternative approach to an inner join.
Conclusion
- The multiple joins in SQL can be performed by using LEFT JOIN, INNER JOIN, and a combination of both INNER & LEFT JOIN.
- We can also join multiple tables in SQL using the parent-child relationship which is equivalent to an inner join.