Joining many tables
Real questions span several tables. Follow the keys: each JOINJOIN (SQL)Combining rows from two tables that belong together, for example each order with its customer.Open in glossary → connects one foreign keyForeign keyA column that points to the primary key of a row in another table, like orders.user_id → users.id.Open in glossary → to the table it points to.
SELECT o.id, u.name AS customer, r.name AS restaurant, c.name AS courier
FROM orders o
JOIN users u ON u.id = o.user_id
JOIN restaurants r ON r.id = o.restaurant_id
JOIN couriers c ON c.id = o.courier_id
WHERE o.status = 'delivered';Short aliases (o, u, r, c) keep it readable, and prefixing every column (o.id, u.name) avoids “ambiguous column” errors when two tables share a column name.
Trap 1: a LEFT JOIN filter in WHERE
You want restaurants without a delivered order. A LEFT JOIN keeps every restaurant and fills NULLs where nothing matches. But a condition on the joined table in WHERE throws those NULL rows away again:
Condition in ON: keeps the empty matches
SELECT r.name, o.id
FROM restaurants r
LEFT JOIN orders o
ON o.restaurant_id = r.id
AND o.status = 'delivered'
WHERE o.id IS NULL;Green Bowl and Le Petit Café, both with NULL.
Condition in WHERE: turns it into an INNER JOIN
SELECT r.name, o.id
FROM restaurants r
LEFT JOIN orders o ON o.restaurant_id = r.id
WHERE o.status = 'delivered'
AND o.id IS NULL;No rows: o.status is NULL on exactly the rows you wanted.
Trap 2: joins that multiply rows
A JOIN returns one row for every matching pair. Join an order with 3 items to its payment, and that payment appears 3 times. Sum the payments afterwards, and the sum is three times too high:
| Restaurant | Paid, correct | Paid, after also joining order_items |
|---|---|---|
| Sushi Zen Garden | 151.50 | 255.00 |
| Pizzeria Napoli | 106.00 | 176.00 |
| Balkan Grill Kod Žike | 64.50 | 129.00 |
| Burger Brothers | 61.00 | 159.00 |
This multiplying is called fan-out. The rule: join only the tables the question needs, and when you need sums from two “many” tables (items and payments), sum each one separately first, then join the totals. If a total looks suspiciously big, count the rows before summing.
This is called fan-out, and it’s one of the most common reasons a report “doesn’t match”. Three ways to avoid it:
- Join only what the question needs. Payments per restaurant need restaurants, orders and payments. Not items.
- Aggregate first, then join. Sum the items per order in one query, the payments per order in another, and join the two totals on
order_id:
WITH items AS (
SELECT order_id, SUM(quantity * unit_price) AS items_total
FROM order_items GROUP BY order_id
),
paid AS (
SELECT order_id, SUM(amount) AS paid
FROM payments WHERE status = 'succeeded' GROUP BY order_id
)
SELECT o.id, items.items_total, paid.paid
FROM orders o
JOIN items ON items.order_id = o.id
LEFT JOIN paid ON paid.order_id = o.id;- Check the row count. Before summing,
SELECT COUNT(*)with the same joins. If 14 orders turn into 31 rows, something multiplied.
Check this part
You LEFT JOIN restaurants to orders and add WHERE o.status = 'delivered'. What happens to restaurants with no delivered order?
A restaurant's paid total doubles after you add order_items to the query. Why?