๐งฉ Day 7 โ SQL JOINS
๐น What is a Join?
A Join is used to combine rows from two or more tables based on a related column between them.
๐ธ Types of Joins
| Join Type |
Description |
Result |
| 1. INNER JOIN |
Returns only the matching records from both tables. |
Common rows only |
| 2. LEFT OUTER JOIN |
Returns all records from left table, and matched rows from right table. |
All left + matched right |
| 3. RIGHT OUTER JOIN |
Returns all records from right table, and matched rows from left table. |
All right + matched left |
| 4. FULL OUTER JOIN |
Returns all matched and unmatched rows from both tables. |
All data from both |
| 5. CROSS JOIN |
Returns Cartesian product (every row of table A ร every row of table B). |
m ร n combinations |
| 6. SELF JOIN |
Joins a table with itself. Useful for hierarchical data (e.g., employeeโmanager). |
Same table joined on itself |
๐น Example Tables
employees
| employee_id |
first_name |
manager_id |
department_id |
| 101 |
John |
201 |
10 |
| 102 |
David |
201 |
20 |
departments
| department_id |
department_name |
| 10 |
Sales |
| 20 |
IT |
| 30 |
HR |
๐น Examples
1๏ธโฃ Inner Join
โ
Returns matched rows from both tables.
2๏ธโฃ Left Join
โ
Returns all employees, even if they donโt belong to a department.
3๏ธโฃ Right Join
โ
Returns all departments, even if no employees belong to them.
4๏ธโฃ Full Outer Join
โ
Returns all records from both tables (matched + unmatched).
5๏ธโฃ Cross Join
โ
Produces a Cartesian product (if 10 employees ร 5 departments โ 50 rows).
6๏ธโฃ Self Join
โ
Joins employees table with itself to show who reports to whom.
๐งฎ SQL Joins Explained with SET Examples
Each SET represents two tables (A and B, or C and D, etc.) โ weโll calculate what each type of join returns.
โ๏ธ Join Logic Reminder
| Join Type |
Description |
What You Get |
| INNER JOIN |
Only rows with matching values in both tables |
Common values |
| LEFT JOIN |
All rows from left table + matched rows from right |
All left + matches |
| RIGHT JOIN |
All rows from right table + matched rows from left |
All right + matches |
| FULL JOIN |
All rows from both tables (matched + unmatched) |
Everything |
| CROSS JOIN |
Every row in A combined with every row in B |
Cartesian product (AรB) |
๐ธ SET 1
Table A
Table B
Results
| Join Type |
Result IDs |
Count |
| INNER JOIN |
1, 2, 3, 4, 5 |
5 |
| LEFT JOIN |
1, 2, 3, 4, 5, 6 |
6 |
| RIGHT JOIN |
1, 2, 3, 4, 5, 8 |
6 |
| FULL JOIN |
1, 2, 3, 4, 5, 6, 8 |
7 |
| CROSS JOIN |
6ร6 = 36 combinations |
36 |
๐ธ SET 2
Table C
Table D
Results
| Join Type |
Result IDs |
Count |
| INNER JOIN |
1, 2, 2, 3, 3, 5 |
6 |
| LEFT JOIN |
1, 2, 2, 3, 3, 4, 5 |
7 |
| RIGHT JOIN |
1, 2, 2, 3, 3, 4, 5, 8 |
8 |
| FULL JOIN |
1, 2, 2, 3, 3, 4, 5, 8 |
8 |
| CROSS JOIN |
7ร5 = 35 combinations |
35 |
๐ธ SET 3
Table E
Table F
Results
| Join Type |
Result IDs |
Count |
| INNER JOIN |
10 rows (because 5 twos in E ร 2 twos in F = 10) |
10 |
| LEFT JOIN |
All 6 rows from E + 5 matches (total 11) |
11 |
| RIGHT JOIN |
2s (10) + unmatched 3,4 โ total 12 |
12 |
| FULL JOIN |
1 (from E unmatched) + 2ร10 + 3 + 4 = 13 |
13 |
| CROSS JOIN |
6ร4 = 24 |
24 |
๐ธ SET 4
Table G
Table H
Results
| Join Type |
Result IDs |
Count |
| INNER JOIN |
5ร3 = 15 (all are 2) |
15 |
| LEFT JOIN |
same as inner (all match) |
15 |
| RIGHT JOIN |
same as inner (all match) |
15 |
| FULL JOIN |
same as inner (all match) |
15 |
| CROSS JOIN |
5ร3 = 15 combinations |
15 |
๐ธ SET 5
Table I
Table J
Results
| Join Type |
Result IDs |
Count |
| INNER JOIN |
0, 1, 2, 2, 2, 2, 4 (NULLs donโt match) |
7 |
| LEFT JOIN |
all from I (7) + matched right (2 extra) โ 9 |
9 |
| RIGHT JOIN |
all from J (7) + matched left (2 extra) โ 9 |
9 |
| FULL JOIN |
all from both (including nulls & unmatched 3,5) โ 11 |
11 |
| CROSS JOIN |
7ร7 = 49 combinations |
49 |
โ
KEY POINTS TO REMEMBER
-
INNER JOIN ignores unmatched rows.
-
LEFT JOIN keeps all left rows.
-
RIGHT JOIN keeps all right rows.
-
FULL JOIN keeps everything.
-
CROSS JOIN multiplies all rows.
-
NULL values never match in join conditions (e.g., NULL = NULL is false).