Lesson 215 of 296
JOINs
What you will learn
- Why joins exist
- INNER JOIN
- LEFT JOIN and finding missing matches
- RIGHT, FULL OUTER, CROSS and self joins
- Joining many tables
- Where to put conditions: ON vs WHERE
- How duplicates appear in joins
On this page
Why joins?
Data is split across tables to avoid repetition (Lesson 227): the order stores customer_id, not the customer's name. A join combines rows from two tables using a matching condition, usually foreign key = primary key.
INNER JOIN
Returns only rows that have a match in both tables:
SELECT o.id AS order_id, c.name, o.status
FROM orders o
JOIN customers c ON c.id = o.customer_id
ORDER BY o.id;| order_id | name | status |
|---|---|---|
| 101 | Aisha Khan | paid |
| 102 | Aisha Khan | paid |
| 103 | Ben Smith | cancelled |
| 104 | Chen Li | paid |
| 105 | Dara Patel | pending |
| 106 | Chen Li | paid |
JOIN means INNER JOIN. Eve does not appear: she has no orders, so there is no match.
LEFT JOIN
Returns every row from the left table, with matching rows from the right table, or NULLs when there is no match:
SELECT c.name, o.id AS order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
ORDER BY c.id, o.id;| name | order_id |
|---|---|
| Aisha Khan | 101 |
| Aisha Khan | 102 |
| Ben Smith | 103 |
| Chen Li | 104 |
| Chen Li | 106 |
| Dara Patel | 105 |
| Eve Jones | NULL |
Finding rows with no match (the anti-join)
"Customers who have never ordered" is one of the most common interview questions:
SELECT c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL;| name |
|---|
| Eve Jones |
Same pattern for "products never ordered":
SELECT p.name
FROM products p
LEFT JOIN order_items oi ON oi.product_id = p.id
WHERE oi.order_id IS NULL;| name |
|---|
| Kettle |
NOT EXISTS (Lesson 217) is an equally good way to write this.
Other joins
| Join | Returns |
|---|---|
RIGHT JOIN | All rows from the right table. Rarely used: swap the tables and use LEFT JOIN instead |
FULL OUTER JOIN | All rows from both sides, NULLs where there is no match. Useful for comparing two lists |
CROSS JOIN | Every combination of rows (5 customers × 5 products = 25 rows). Useful for generating grids, dangerous by accident |
| Self join | A table joined to itself with two aliases: employees e JOIN employees m ON m.id = e.manager_id to show each employee's manager |
Joining many tables
SELECT o.id, c.name, p.name AS product, oi.quantity,
oi.quantity * oi.unit_price_pence AS line_total_pence
FROM orders o
JOIN customers c ON c.id = o.customer_id
JOIN order_items oi ON oi.order_id = o.id
JOIN products p ON p.id = oi.product_id
WHERE o.id IN (101, 104)
ORDER BY o.id, p.name;| id | name | product | quantity | line_total_pence |
|---|---|---|---|---|
| 101 | Aisha Khan | Mug | 2 | 1798 |
| 101 | Aisha Khan | Notebook | 1 | 450 |
| 104 | Chen Li | Mug | 1 | 899 |
| 104 | Chen Li | Pen Set | 2 | 2500 |
ON vs WHERE in a LEFT JOIN
For INNER JOINs it makes no difference. For LEFT JOIN it changes the result:
- A condition in ON decides which right-side rows match; left rows are still all kept.
- A condition in WHERE filters the final rows, so a condition on the right table (other than
IS NULL) removes the unmatched left rows and turns your LEFT JOIN into an INNER JOIN by accident.
"All customers with their paid orders": put o.status = 'paid' in the ON clause. If you put it in WHERE, customers with no paid orders disappear.
Joins multiply rows
Joining one order to its three items gives three rows. Joining a customer to 2 orders and 5 addresses gives 2 × 5 = 10 rows, and any SUM over that result is wrong (counted several times). Watch for this "fan-out" when joining two one-to-many relationships at once; aggregate each one separately first (in a CTE or subquery, Lesson 217), or use COUNT(DISTINCT ...).
How to say it in an interview
"INNER JOIN returns only matching rows; LEFT JOIN keeps every row from the left table with NULLs where nothing matches, and LEFT JOIN plus WHERE right.id IS NULL finds missing matches, like customers who never ordered. FULL OUTER keeps both sides, CROSS JOIN gives every combination, and a self join links a table to itself, for example employees to managers. In a LEFT JOIN, conditions on the right table belong in ON, because in WHERE they turn it into an inner join. And joining two one-to-many tables at once multiplies rows, so I aggregate separately first."
Key words
| Word | Meaning |
|---|---|
| JOIN / INNER JOIN | Rows with a match in both tables |
| LEFT JOIN | All left rows, NULLs where no match |
| Anti-join | Finding rows with no match |
| FULL OUTER JOIN | All rows from both tables |
| CROSS JOIN | Every combination of rows |
| Self join | A table joined to itself |
| Fan-out | Rows multiplying when joining one-to-many relationships |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.