We have already used joins to bring two tables together. Now we look at what happens to the number of rows. On a report built from a few thousand orders, a join that adds or drops rows can go unnoticed, because the query still runs and the total still looks reasonable. To avoid this, we first compare the two join types, and then we look at the two ways a row count goes wrong.
The difference in one example
Marketing wants a list of customers and their orders. Ruby, Ethan and Daniel are customers, and Daniel has not ordered yet.
| Join | Rows returned | Daniel, who has no orders |
|---|---|---|
| INNER JOIN | Only customers that have orders | Dropped |
| LEFT JOIN | All customers, with orders where they exist | Kept, and the order columns are NULL |
SELECT c.customer_id, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id;Since customers is the left table, every customer stays in the result. In most databases, the order of the tables in an INNER JOIN does not change the result. In a LEFT JOIN it does, because only the left table is kept in full.
Why the row count grows: fan-out
A join returns one row for every matching pair. If one order has three items, joining orders to order_items turns that order into three rows. We call this fan-out, which means one row on the left fans out into several rows after the join. A SUM over the order total then counts the order three times.
-- WRONG: total_amount repeats on every item row
SELECT SUM(o.total_amount) AS revenue
FROM orders o
JOIN order_items i ON i.order_id = o.order_id;
-- right: aggregate the many side first, then join one row per order
WITH items AS (
SELECT order_id, COUNT(*) AS n_items
FROM order_items GROUP BY order_id
)
SELECT SUM(o.total_amount) AS revenue, SUM(i.n_items) AS items
FROM orders o
LEFT JOIN items i ON i.order_id = o.order_id;The WHERE trap: a LEFT JOIN that behaves like an INNER JOIN
Say we want every customer, with paid orders only. For a customer with no order, o.status is NULL, and NULL = 'paid' is not true. As a result, WHERE drops that customer, and the join no longer keeps the left rows. Moving the condition into ON fixes this, since ON decides which rows match and WHERE decides which rows stay.
-- WRONG: removes customers with no order
SELECT c.customer_id, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.status = 'paid';
-- right: the condition moves into ON
SELECT c.customer_id, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id AND o.status = 'paid';Find the rows with no match
To list the customers who never ordered, we keep the NULL rows on purpose. We test a column of the right table that is always filled when a match exists, such as its key.
SELECT c.customer_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL; -- customers who never orderedThree checks before you trust a join
- Count the rows of the left table, then count the rows after the join. If the number grew, find out why.
- Check that the join key is unique on the side you expect to be "one", with
SELECT key, COUNT(*) FROM t GROUP BY key HAVING COUNT(*) > 1. - Compare a total from the joined result with the same total from the source table.


