What are SQL Multiple JOINS?

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

Multiple joins in SQL are used to join records of more than two tables based on related columns between them. We can perform the same or different types of joins on different tables as per the requirement. Multiple matrix multiplications can be related to SQL multiple join except for the fact that multiple join is much more flexible, we can join selected rows and columns as well.

Whenever we perform more than one join of the same or different type on multiple tables in a single query, it is called Multiple Join in SQL.

multiple-joins

Types of SQL JOINS

  • INNER JOIN - It returns rows/records that have matching values in both tables.
  • LEFT (OUTER) JOIN - It returns all the rows that are present in the left table and only matching rows from the right table.
  • RIGHT (OUTER) JOIN - It returns all the rows that are present in the right table and only matching rows from the left table.
    • FULL (OUTER) JOIN - It returns all the rows from both the left and right tables.

inner-join-sql

left-join-sql2

right-join-sql

full-outer-join-sql

Syntax

Let us now see how we can combine more than two tables using Multiple joins in SQL. Here is a syntax for 3 tables, the same can be extended for 4 or more tables.

In the above syntax, we are performing inner join on table1 and table2 and left join on the resultant table from inner join and table3. We can also specify conditions to select only specific records.

Examples of SQL Multiple JOINS

Let us take an example of the Company's database. It has many relations out of which only 3 are of our interest. These are EMP, PROJECT, AND EMP_PROJ. EMP table has all the data related to the employee like their ID, NAME, JOB, HIRE DATE, etc., the PROJECT table only has the name of the project associated with the project ID, and the EMP_PROJ gives the data of employees who completed particular projects, time is taken by them, and so on.

Now, we are required to fetch the data of employees' names and project names from these tables. None of the tables independently contains the information so we will be performing multiple joins to fetch the data. Also, let us say we want the names of only those employees who worked for more than or equal to 1.00 EMPTIME.

EMP TABLE:

EMPNOFIRSTNAMELASTNAMEHIREDATEJOB
000010CHRISTINEHAAS1965-01-01PRES
000030SALLYKWAN1975-04-05MANAGER
000100THEODORESPENSER1980-06-19MANAGER
000110VINCENZOLUCCHESSI1958-05-16SALESREP
000120SEANO'CONNELL1963-12-05CLERK
000130DOLORESQUINTANA1971-07-28ANALYST
000140HEATHERNICHOLLS1976-12-15ANALYST
000160ELIZABETHPIANKA1977-10-11DESIGNER
000250JAMESJEFFERSON1966-11-21CLERK
000240SALVATOREMARINO1979-12-05CLERK
000330WINGLEE1976-02-23FIELDREP

EMP_PROJ TABLE

EMPNOPROJECTNOACTNOEMPTIME
000010AD3110101.00
00030AD3111601.00
000020AD3111600.50
000100AD3111801.00
000250AD3112601.00
000120AD3112700.50
000250AD3112701.00
000140AD3112700.25
000150AD3113700.50
000330AD31131801.00

PROJECT TABLE:

PROJECTNOPROJECTNAME
AD3110CLOUD MANAGEMENT
AD3111SUSTAINABLE DEVELOPMENT
AD3112TRACKING APPLICATION
AD3113DEEP LEARNING

Firstly we are required to join the EMP and EMP_PROJ, but we need all the records of EMP_PROJ and only matching records of the EMP table. Thus, we will be performing the right join on EMPNO field from both tables. Similarly, we need only matching records of the PROJECT table so we will now perform a left join on the PROJECT NO field from EMP_PROJ and PROJECT table. Lastly, we will add a where condition to check if EMPTIME >= 1.00.

Output:

How SQL Multiple JOINS Work?

Most Relational Database Management Systems have query optimizers that calculate the cost of each possible way of performing these joins. From data distributions and statistics gathered periodically the optimizer determines the cost. Out of the multiple optional costs typically the lowest or the most efficient query plan is executed.

Application of SQL Multiple JOINS

Multiple Join is a very important and useful concept of SQL. Very often we are required to fetch data from multiple tables based on some conditions. As seen in the above example, multiple joins are easy to implement and very frequently used when working with multiple tables. We usually have tables of every individual entity in our database, using lossless (i.e. where none of our data is lost) joins we retrieve data as per the requirement.

Conclusion

  • SQL provides multiple types of joins such as INNER JOIN, LEFT JOIN, RIGHT JOIN, etc.
  • We can use more than one joins of the same or different types in a single SQL query and this is known as Multiple joins in SQL.
  • Before performing any joins, query optimizers calculate the cost of performing joins by various paths and return the most efficient way of performing the multiple join function.

Learn More