Join Multiple 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

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 nn number of tables, but, to join nn number of tables, the joins required are n1n-1, that is for 33 tables, 22 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_idNameBr_idEmailCity_id
1001Ankit101ankit@bmail.com1
1002Pranav105pranav@bmail.com2
1003Raj102raj@bmail.com2
1004Shyam112shyam@bmail.com4
1005Duke112duke@bmail.com2
1006Jhon102jhon@bmail.com3
1007Aman101aman@bmail.com4
1008Pavan111pavan@bmail.com13
1009Virat112virat@bmail.com12

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_idBr_nameHODContact
101CSESH Rao22345
102MECHAP Sharma28210
103EXTCVK Reddy34152
104CHEMSK Mehta45612
105ITVL Shelke22521
106AIKH Verma12332
107PRODPG Kakde90876

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_idCityPincode
1Mumbai400121
2Pune450011
3Lucknow553001
4Delhi443221
5Kolkata213445
6Chennai345432
7Nagpur323451
8Sri Nagar321321

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:

NameBr_idHODCityPincode
Ankit101SH RaoMumbai400121
Pranav105VL ShelkePune450011
Raj102AP SharmaPune450011
Jhon102AP SharmaLucknow553001
Aman101SH RaoDelhi443221

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:

NameCityBr_name
AnkitMumbaiCSE
PranavPuneIT
RajPuneMECH
ShyamDelhiNULL
DukePuneNULL
JhonLucknowMECH
AmanDelhiCSE
PavanNULLNULL
ViratNULLNULL

Explanation: In this query, the first left join gets all the rows with the names of students from the StudentStudent 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 AddressAddress 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:

NameCityBr_name
AnkitMumbaiCSE
PranavPuneIT
RajPuneMECH
ShyamDelhiNULL
DukePuneNULL
JhonLucknowMECH
AmanDelhiCSE

Explanation:
In this query, first all the names from the StudentStudent 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 AddressAddress 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:

NameBr_idHODCityPincode
Ankit101SH RaoMumbai400121
Pranav105VL ShelkePune450011
Raj102AP SharmaPune450011
Jhon102AP SharmaLucknow553001
Aman101SH RaoDelhi443221

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 BranchBranch table and the foreign key of the StudentStudent table. Similarly, City_id is the primary key to the AddressAddress table and the foreign key to the StudentStudent 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.

See Also: