Multiple Joins in SQL - GeeksforGeeks

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.
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 affectedQuery ok, 0 row affectedQuery 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 affectedQuery ok, 1 row affectedQuery ok, 1 row affectedQuery 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 affectedQuery ok, 1 row affectedQuery ok, 1 row affectedQuery 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 affectedQuery ok, 1 row affectedQuery ok, 1 row affectedQuery ok, 1 row affected
7. View data inside the tables :
select *from students;
Output –  Students Table –
select *from marks;
Output – Marks Table-
select *from attendance;
Output –Attendance table-
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 –
Screenshot of Final Output –
Output after Multiple Joins
Reference: https://www.geeksforgeeks.org/sql-query-to-find-the-highest-salary-of-each-department/
DBMS-SQL
Picked
SQL
SQL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
CTE in SQL
SQL Trigger | Student Database
How to Update Multiple Columns in Single Update Statement in SQL?
Difference between DDL and DML in DBMS
Difference between SQL and NoSQL
SQL | GROUP BY
SQL | Views
Difference between DELETE, DROP and TRUNCATE
MySQL | Group_CONCAT() Function
Difference between DELETE and TRUNCATE