Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Multiple Joins in SQL

  • Difficulty Level : Medium
  • Last Updated : 30 Jun, 2021

Structured Query Language or SQL is a standard database language that is used to create, maintain and retrieve the data from relational databases like MySQL, Oracle, etc. In this article, we will be using the Microsoft SQL Server.

Multiple Joins :
Here we are going to implement the concept of multiple joins. Multiple joins can be described as a query containing joins of the same or different types used more than once, thus giving them the ability to combine multiple tables. For this article we will first create a database geeks and then create three tables in it and then run our queries on those tables.

Attention reader! Don’t stop learning now. Learn SQL for interviews using SQL Course  by GeeksforGeeks.

Venn Diagram Representation of  Multiple Joins

1. Creating Database :

CREATE geeks;

Output –
Query ok, 1 row affected



2. To use this database :

USE geeks;

Output –
Database changed

3. Adding Tables to the Database :

create table students(id int, name varchar(50),branch varchar(50));
create table marks(id int, marks int);
create table attendance(id int, attendance int);

Output –
Query ok, 0 row affected
Query ok, 0 row affected
Query ok, 0 row affected

4. Inserting Data into Tables:
Students table –

--students
insert into students values(1,'anurag','cse');
insert into students values(2,'harsh','ece');
insert into students values(3,'sumit','ece');
insert into students values(4,'kae','cse');

Output –
Query ok, 1 row affected
Query ok, 1 row affected
Query ok, 1 row affected
Query ok, 1 row affected

5. Marks Table :

--marks
insert into marks values(1,95);
insert into marks values(2,85);
insert into marks values(3,80);
insert into marks values(4,65);

Output –
Query ok, 1 row affected
Query ok, 1 row affected
Query ok, 1 row affected
Query ok, 1 row affected



6. Attendance table :

--attendance
insert into attendance values(1,75);
insert into attendance values(2,65);
insert into attendance values(3,80);
insert into attendance values(4,80);

Output –
Query ok, 1 row affected
Query ok, 1 row affected
Query ok, 1 row affected
Query ok, 1 row affected

7. View data inside the tables :

select *from students;

Output –  
Students Table –

idnamebranch
1anuragcse
2harshece
3sumitece
4kaecse
select *from marks;

Output – 
Marks Table-

id marks
195
285
380
465
select *from attendance;

Output –
Attendance table-

idattendance
175
265
380
487

Screenshot of Final Output –

Tables after data Insertion

8. Performing Multiple Joins :
Now we will perform multiple joins on our tables. First we will inner join the students and the marks tables and then we will join the resulting table with the attendance table only for those students which have attendance greater than or equal to 75.

Syntax –

JOIN
table1.column_name=table2.column_name
JOIN
table2.column_name=table3.column_name

Example query :

select s.id, name, marks, attendance
from students as s
inner join
marks as m
on s.id=m.id
inner join
attendance as a
on m.id=a.id
where a.attendance>=75;

Output –

idnamemarksattendance
1anurag9575
3sumit8080
4kae6587

Screenshot of Final Output –

Output after Multiple Joins

Reference: https://www.geeksforgeeks.org/sql-query-to-find-the-highest-salary-of-each-department/

My Personal Notes arrow_drop_up
Recommended Articles
Page :