0% found this document useful (0 votes)
2 views

Types of JOINS in SQL

The document provides an overview of various types of SQL joins, including INNER JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN, FULL OUTER JOIN, SELF JOIN, and THETA JOIN. Each type is defined, accompanied by syntax examples and scenarios illustrating their use with employee and department tables. The document emphasizes how different joins retrieve data based on specific conditions and relationships between tables.

Uploaded by

saiyadavavala123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Types of JOINS in SQL

The document provides an overview of various types of SQL joins, including INNER JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN, FULL OUTER JOIN, SELF JOIN, and THETA JOIN. Each type is defined, accompanied by syntax examples and scenarios illustrating their use with employee and department tables. The document emphasizes how different joins retrieve data based on specific conditions and relationships between tables.

Uploaded by

saiyadavavala123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

OUTER JOIN

INNER JOIN • LEFT OUTER JOIN


• RIGHT OUTER JOIN
• FULL OUTER JOIN

Types of SELF JOIN THETA JOIN


JOINS in SQL

CROSS JOIN NATURAL JOIN


INNER JOIN

• 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

department d 5 Eve Finance

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

employee e 3 Charlie NULL NULL

LEFT JOIN 4 Diana 101 HR

department d 5 Eve 103 Finance

ON e.dept_id = d.dept_id; 6 Frank NULL NULL


RIGHT OUTER JOIN

• 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

ON e.dept_id = d.dept_id; NULL NULL 104 Marketing


FULL OUTER JOIN

• 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

FROM 2 Bob 102 IT

employee e 3 Charlie NULL NULL

FULL OUTER JOIN 4 Diana 101 HR


department d 5 Eve 103 Finance
ON e.dept_id = d.dept_id;
6 Frank NULL NULL

NULL NULL 104 Marketing


SELF JOIN

• 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

5 Eve 12000 10000 19999 D


6 Frank 29990
7 Henry 47670
Writing THETA JOIN as follows will result in
SELECT worker_id name salary grade
w.worker_id, w.name, w.salary, p.grade 1 Alice 24500 C
FROM
2 Bob 16900 D
worker w JOIN payment p
3 Charlie 40000 A
ON
w.salary >= p.min_salary 4 Diana 35650 B
AND 5 Eve 12000 C
w.salary <= p.max_salary;
6 Frank 29990 D

7 Henry 47670 A

You might also like