SQL Joins
SQL Joins
Understanding SQL Joins is fundamental when it comes to database management and data
analysis.Here explores the four primary types of SQL Joins: Left, Right, Inner, and Full. Each
type uniquely combines rows from two or more tables based on a related column. By the end
ofHere, you'll have a solid understanding of how these joins work, when to use them, and how
they impact data manipulation and querying. Whether you're a beginner or an experienced SQL
user looking to refine your skills, this guide is valuable for exploring SQL Joins.
Table of Content
What are Joins?
Types of Joins
INNER JOIN
LEFT JOIN
RIGHT JOIN
FULL JOIN
What are SQL Joins?
SQL Joins are a fundamental concept in SQL (Structured Query Language), used to combine
rows from two or more tables based on a related column between them. The primary purpose of
joins is to query data from multiple tables to create a set of results that can be used for analysis or
reporting.
In simple terms, joins are the backbone of combining data from multiple tables in SQL. They act
like bridges, connecting related information scattered across different tables based on shared
fields. Essentially, joins let you retrieve data from more than one table in a single query,
presenting a unified view of your information.
There are 4 different types of joins that you can use. Refer below.
Types of Joins in SQL
Following are the four types of joins that you can use:
INNER JOIN
LEFT JOIN
RIGHT JOIN
FULL JOIN
Now, in the next section, we will understand how these joins work with the help of examples.
For that, we will take three tables: Patients Table (patient_id, patient_name, sex, age,
phone_number, state, country, haemoglobin), Medicines Table (medicine_id,
medicine_name, patient_id, doctor_id, price), Doctors Table (doctor_id, doctor_name,
phone_number, email_id patient_id).
Patients Table
Patient ID Patient Name Sex Age Phone Number State Country Haemoglobin
1 Sheela F 23 9876543210 Telangana India 82.321
2 Rehan M 21 7429061312 Karnataka India 83.231
3 Anay M 56 7382221121 Haryana India 94.567
4 Mahira F 42 9318281092 Gujarat India 78.567
5 Nishant M 12 8209109311 Maharashtra India 65.234
Medicines Table
MedicineID Medicine Name PatientID DoctorID Price
M01 MeftalForte 1 4 500
M02 Dolo 2 2 670
M03 Wikoryl 3 1 980
M04 PanD 1 4 420
M05 Uprise 4 3 930
M06 Boroline 6 3 470
M07 Volini 6 1 820
M08 Vicks 4 2 640
M09 Cetaphil Day Cream 3 3 230
M10 MeftalSpas 5 5 850
Doctors Table
Do Doct
Phone
cto or Patie
Numb EmailID
r Nam ntID
er
ID e
Dr.M 76263 manoj@g
01 2
anoj 13013 mail.com
Dr.D 86543 dipika@g
02 1
ipika 45678 mail.com
Dr.S 94567 sana@gma
03 4
ana 86543 il.com
Dr.P 84232 paras@gm
04 3
aras 31133 ail.com
Dr.
67890 suhail@gm
05 Suhai 4
56798 ail.com
l
Fetch the record of patient_id, patient_name, patient_age, medicine_id and medicine name
using medicine and patient table.
SELECT TableName1.ColumnName1,TableName2.ColumnName1,....
FROM TableName1
FULL JOIN TableName2
ON TableName1.MatchingColumnName = TableName2.MatchingColumnName;
Syntax
SELECT column_names
FROM table_1
WHERE conditions
UNION
SELECT column_names
FROM table_2
WHERE conditions;
SELECT column_names
FROM table_1
WHERE conditions
UNION ALL
SELECT column_names
FROM table_2
WHERE conditions;
Rules
The SELECT statements must have the same number of columns and data types.
Columns in both the SELECT statements must be in the same order.
The names of the columns can be different, but the final result set will show the column name
of the first selected query.
Let’s understand the difference between UNION and UNION ALL by an example.
Example
Let us have two tables of employee names and manager names. Combine both the tables using
UNION and UNION ALL.
Employee
Employee_id Name
1 Ram
2 Shyam
3 Joya
4 Vidit
Manager
Employee_id Name
1 Ram
4 Vidit
UNION
SELECT Employee_id, name
FROM Employee
UNION
SELECT *
FROM Manager;
output
Employee_id Name
1 Ram
2 Shyam
3 Joya
4 Vidit
UNION ALL
SELECT Employee_id, name
FROM Employee
UNION
SELECT *
FROM Manager;
output
Employee_id Name
1 Ram
2 Shyam
3 Joya
4 Vidit
1 Ram
4 Vidit
Key differences
Both UNION and UNION ALL combine the result of two or more tables.
The result set of UNION does not contain duplicate rows, while the result set of UNION ALL
returns all the rows from both tables.
The execution time of UNION ALL is less than the execution time of UNION as it does not
remove the duplicate rows.
GROUP BY
The GROUP BY clause in sql is used to group the rows that have identical values by one
or more columns. InHere, we will briefly discuss how GROUP BY clause is used in SQL
with aggregation function, having clause, order by clause and with the case statement.
SELECT column_names
FROM table_name
WHERE conditions
GROUP BY column_names
ORDER BY column_names;
Let’s have a Student table that contains the StudentID, Name, Percentage, and corresponding
Grade.
Student ID Name Percentage Grade Remark
1001 Ajay 87 A Excellent
1002 Babloo 81 A Excellent
1003 Chhavi 79 B Good
1004 Dheeraj 93 O Excellent
1005 Evina 95 O Excellent
1006 Fredy 80.7 A Excellent
1007 Garima 63 C Poor
1008 Hans 49 F Fail
1009 Ivanka 88 A Excellent
1010 Jai 74 B Good
1011 Kundan 88 A Excellent
1012 Himanshi 37 F Fail
1013 Atul 67 B Good
1014 Jaya 48 F Fail
1015 Aquib 75 B Good
COUNT with GROUP BY
Example: Use the GROUP BY clause to count the number of students by the grade.
Query
FROM Student
GROUP BY Grade;
Output
Grade Number of Students
A 5
B 4
C 1
F 3
O 2
FROM Student
GROUP BY Grade
HAVING Number of Students >= 3;
Output
Grade Number of Students
A 5
B 4
F 3
FROM Student
GROUP BY Grade
ORDER BY Number of Students DESC;
Output
Grade Number of Students
A 5
B 4
F 3
O 2
C 1