🔹 1. What is SQL JOIN?
SQL JOIN is used to combine rows from two or more tables based on a related column
between them — often a primary key in one table and a foreign key in the other.
✅ It helps to fetch data from multiple tables in a single query using logical
relationships.
🔹 2. Types of SQL JOIN
JOIN Type Description
INNER JOIN Returns only records that have matching values in both tables
LEFT JOIN Returns all records from the left table and the matched records
from right
RIGHT JOIN Returns all records from the right table and matched records
from left
FULL OUTER Returns all records when there is a match in either table
JOIN
CROSS JOIN Returns Cartesian product of the two tables
SELF JOIN A table is joined with itself
🔹 3. Example Tables
We’ll use these two example tables in most queries:
🧾 Table: Students
student_id name class_id
1 Alice 101
2 Bob 102
3 Carol 103
4 Dave NULL
🧾 Table: Classes
class_id class_name
101 Math
102 Science
104 History
🔹 4. SQL JOIN Types with Examples
🔸 1. INNER JOIN
Returns only matching rows in both tables.
● SELECT [Link], Classes.class_name
● FROM Students
● INNER JOIN Classes ON Students.class_id = Classes.class_id;
📌 Result:
name class_name
Alice Math
Bob Science
🔸 2. LEFT JOIN (LEFT OUTER JOIN)
Returns all rows from the left table and matched rows from the right table.
● SELECT [Link], Classes.class_name
● FROM Students
● LEFT JOIN Classes ON Students.class_id = Classes.class_id;
📌 Result:
name class_name
Alice Math
Bob Science
Carol NULL
Dave NULL
🔸 3. RIGHT JOIN (RIGHT OUTER JOIN)
Returns all rows from the right table and matched rows from the left table.
● SELECT [Link], Classes.class_name
● FROM Students
● RIGHT JOIN Classes ON Students.class_id = Classes.class_id;
📌 Result:
name class_name
Alice Math
Bob Science
NULL History
🔸 4. FULL OUTER JOIN
Returns all records when there is a match in either table.
(Not supported directly in MySQL – use UNION workaround)
● SELECT [Link], Classes.class_name
● FROM Students
● FULL OUTER JOIN Classes ON Students.class_id = Classes.class_id;
📌 Expected Result:
name class_name
Alice Math
Bob Science
Carol NULL
Dave NULL
NULL History
🔸 5. CROSS JOIN
Returns the Cartesian product (all combinations) of both tables.
● SELECT [Link], Classes.class_name
● FROM Students
● CROSS JOIN Classes;
📌 Result: (Only first few rows shown)
name class_name
Alice Math
Alice Science
Alice History
Bob Math
Bob Science
... ...
🔸 6. SELF JOIN
Used to join a table with itself — often for hierarchical data.
🧾 Table: Employees
emp_id emp_name manager_id
1 John NULL
2 Mike 1
3 Sara 1
4 Tom 2
● SELECT E1.emp_name AS Employee, E2.emp_name AS Manager
● FROM Employees E1
● LEFT JOIN Employees E2 ON E1.manager_id = E2.emp_id;
📌 Result:
Employee Manager
John NULL
Mike John
Sara John
Tom Mike
🔹 5. SQL Questions (With Tables & Answers)
✅ Table: Students
student_id name class_id
1 Alice 101
2 Bob 102
3 Carol 103
4 Dave NULL
✅ Table: Classes
class_id class_name
101 Math
102 Science
104 History
❓ Q1: Show names of all students with their class names. Even if a student is not
assigned to any class.
● SELECT [Link], Classes.class_name
● FROM Students
● LEFT JOIN Classes ON Students.class_id = Classes.class_id;
❓ Q2: Show all classes with student names. Even if no student is enrolled in the class.
● SELECT Classes.class_name, [Link]
● FROM Classes
● LEFT JOIN Students ON Classes.class_id = Students.class_id;
❓ Q3: List students who are not enrolled in any class.
● SELECT name
● FROM Students
● WHERE class_id IS NULL;
OR
● SELECT [Link]
● FROM Students
● LEFT JOIN Classes ON Students.class_id = Classes.class_id
● WHERE Classes.class_id IS NULL;
❓ Q4: Show only students who are assigned to existing classes.
● SELECT [Link], Classes.class_name
● FROM Students
● INNER JOIN Classes ON Students.class_id = Classes.class_id
❓ Q5: Get all possible combinations of students and classes.
● SELECT [Link], Classes.class_name
● FROM Students
● CROSS JOIN Classes;
❓ Q6: Show employee and their manager names (SELF JOIN).
✅ Table: Employees
emp_id emp_name manager_id
1 John NULL
2 Mike 1
3 Sara 1
4 Tom 2
● SELECT E1.emp_name AS Employee, E2.emp_name AS Manager
● FROM Employees E1
● LEFT JOIN Employees E2 ON E1.manager_id = E2.emp_id;
📝 6. Summary Table of JOINs
JOIN Type Includes Non-Matching Returns
Rows From
INNER JOIN ❌ None Matching rows only
LEFT JOIN ✅ Left table only All from left, match from right
RIGHT JOIN ✅ Right table only All from right, match from left
FULL OUTER ✅ Both tables All rows with NULLs where no
JOIN match
CROSS JOIN ❌ N/A All combinations
SELF JOIN ✅ Same table For hierarchical or
self-matching