Two tables this week:
customers orders
+----+---------+ +----+-------------+--------+
| id | name | | id | customer_id | amount |
+----+---------+ +----+-------------+--------+
| 1 | Alice | | 1 | 1 | 250 |
| 2 | Bob | | 2 | 1 | 100 |
| 3 | Charlie | | 3 | 2 | 75 |
+----+---------+ +----+-------------+--------+
Notice Charlie has no orders.
INNER JOIN - only rows that match in both tables:
sql
SELECT c.name, o.amount
FROM customers c
INNER JOIN orders o ON c.id = o.customer_id;
Charlie won't appear - he has no matching order.
LEFT JOIN - all rows from the left table, matched or not:
sql
SELECT c.name, o.amount
FROM customers c
LEFT JOIN orders o ON c.id = o.customer_id;
Charlie appears with
amount = NULL.⚠️ Interview trap: "Find customers with zero orders."
sql
SELECT c.name
FROM customers c
LEFT JOIN orders o ON c.id = o.customer_id
WHERE o.id IS NULL;
People often try
WHERE o.amount = 0 here - wrong, that finds orders worth $0, not customers with no orders at all. Filtering on IS NULL after a LEFT JOIN is the pattern to remember.Which JOIN type trips you up the most? RIGHT and FULL OUTER are coming in a few weeks 👀