Types of JOINS in SQL
Types of JOINS in SQL
• Definition
• Combines rows from two tables only where the join condition is
evaluated to true
• Syntax
SELECT col1, col2, …. FROM
table1 JOIN table2
ON table1.some_column = table2.some_column;
• Scenario
• You want to find employees and their respective department names
from two different tables employee and department
Assume that you have the following two tables
employee department
emp_id name salary dept_id dept_id dept_name
1 Alice 50000 101
101 HR
2 Bob 45000 102
102 IT
3 Charlie 60000 NULL
103 Finance
4 Diana 48000 101
5 Eve 70000 103 104 Marketing
6 Frank 65000 105
Writing INNER JOIN as follows will result in
SELECT e.emp_id, e.name, emp_id name dept_name
d.dept_name
1 Alice HR
FROM
2 Bob IT
employee e
JOIN 4 Diana HR
ON e.dept_id = d.dept_id;
LEFT OUTER JOIN
• Definition
• Retrieves all rows from the left table and matching rows from the
right table. If there is no match, NULL is returned for the right table
• Syntax
SELECT col1, col2, …. FROM
table1 LEFT JOIN table2
ON table1.some_column = table2.some_column;
• Scenario
• You want to find all employees, including those who are not assigned
to any department from two different tables employee and
department
Assume that you have the following two tables
employee department
emp_id name salary dept_id dept_id dept_name
1 Alice 50000 101
101 HR
2 Bob 45000 102
102 IT
3 Charlie 60000 NULL
103 Finance
4 Diana 48000 101
5 Eve 70000 103 104 Marketing
6 Frank 65000 105
Writing LEFT OUTER JOIN as follows will
result in
emp_id name dept_id dept_name
SELECT e.emp_id, e.name,
1 Alice 101 HR
d.dept_id, d.dept_name
FROM 2 Bob 102 IT
• Definition
• Retrieves all rows from the right table and matching rows from the
left table. If there is no match, NULL is returned for the left table
• Syntax
SELECT col1, col2, …. FROM
table1 RIGHT JOIN table2
ON table1.some_column = table2.some_column;
• Scenario
• You want to find all departments, including those with no employees
from two different tables employee and department
Assume that you have the following two tables
employee department
emp_id name salary dept_id dept_id dept_name
1 Alice 50000 101
101 HR
2 Bob 45000 102
102 IT
3 Charlie 60000 NULL
103 Finance
4 Diana 48000 101
5 Eve 70000 103 104 Marketing
6 Frank 65000 105
Writing RIGHT OUTER JOIN as follows will
result in
SELECT e.emp_id, e.name, emp_id name dept_id dept_name
d.dept_id, d.dept_name
1 Alice 101 HR
FROM
2 Bob 102 IT
employee e
4 Diana 101 HR
RIGHT JOIN
department d 5 Eve 103 Finance
• Definition
• Combines the results of both left and right outer joins. All Rows from
both tables are included, even if they don’t match.
• Syntax
SELECT col1, col2, …. FROM
table1 FULL OUTER JOIN table2
ON table1.some_column = table2.some_column;
• Scenario
• You want to find all employees and departments, including
employees without departments and departments without
employees from two different tables employee and department
Assume that you have the following two tables
employee department
emp_id name salary dept_id dept_id dept_name
1 Alice 50000 101
101 HR
2 Bob 45000 102
102 IT
3 Charlie 60000 NULL
103 Finance
4 Diana 48000 101
5 Eve 70000 103 104 Marketing
6 Frank 65000 105
Writing FULL OUTER JOIN as follows will
result in
emp_id name dept_id dept_name
SELECT e.emp_id, e.name,
d.dept_id, d.dept_name 1 Alice 101 HR
• Definition
• A table is joined with itself. Useful for hierarchical or parent-child
relationships. SELF JOIN is written with normal JOIN keyword only, but
instead of using two tables we use one table with different alias names
• Syntax
SELECT col1, col2, …. FROM
table alias_1 JOIN table alias_2
ON alilas_1.some_column = alias_2.some_column;
• Scenario
• You want to find all professor and their hod (assuming the hod is also
a professor) from only one table professor.
Assume that you have the following table
professor
prof_id name salary hod_id
1 Alice 50000 3
2 Bob 45000 NULL
3 Charlie 60000 NULL
4 Diana 48000 2
5 Eve 70000 NULL
6 Frank 65000 5
7 Henry 55000 3
You need to think like combining the table with itself
as shown below
professor (p1) professor (p2)
prof_id name salary hod_id prof_id name salary hod_id
1 Alice 50000 3 1 Alice 50000 3
2 Bob 45000 NULL 2 Bob 45000 NULL
3 Charlie 60000 NULL 3 Charlie 60000 NULL
4 Diana 48000 2 4 Diana 48000 2
5 Eve 70000 NULL 5 Eve 70000 NULL
6 Frank 65000 5 6 Frank 65000 5
7 Henry 55000 3 7 Henry 55000 3
Writing SELF JOIN (INNER) as follows will
result in professor
SELECT professor_id
_name
hod_id hod_name
p1.prof_id AS professor_id, 1 Alice 3 Charlie
p1.prof_name AS professor_name,
p2.prof_id AS hod_id, 4 Diana 2 Bob
p2.prof_name AS hod_name
6 Frank 5 Eve
FROM
professor p1 7 Henry 3 Charlie
JOIN
professor p2
ON p1.hod_id = p2.prof_id
THETA JOIN
• Definition
• A THETA JOIN is a type of join in SQL where the condition between the
two tables uses a comparison operator other than equality (=).
• It can involved other operators like (<, <=, >, >= , ….)
• Syntax
SELECT col1, col2, …. FROM
table1 JOIN table2
ON <some_other_comparison> on columns of two tables
• Scenario
• Determining worker’s pay_grade based on grading criteria.
Assume that you have the following two tables
and we have to figure out into which pay_grade each employee
falls….?
worker payment
worker_id name salary min_salary max_salary grade
1 Alice 24500
40000 49999 A
2 Bob 16900
30000 39999 B
3 Charlie 40000
4 Diana 35650 20000 29999 C
7 Henry 47670 A