LEFT JOIN vs INNER JOIN: Why Your Row Count Changes

By ··7 min read

Short answer

INNER JOIN returns only the rows that match on both sides. LEFT JOIN returns every row from the left table and fills the right side with NULL when nothing matches. The row count changes in two cases. The right table may have more than one match for a left row, which we call fan-out, or a WHERE filter on the right table may remove the NULL rows.

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.

JoinRows returnedDaniel, who has no orders
INNER JOINOnly customers that have ordersDropped
LEFT JOINAll customers, with orders where they existKept, 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 ordered

Three checks before you trust a join

  1. Count the rows of the left table, then count the rows after the join. If the number grew, find out why.
  2. 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.
  3. Compare a total from the joined result with the same total from the source table.

Frequently asked questions

Why does my LEFT JOIN return more rows than the left table?

Because the right table has several matching rows for some left rows. Each match produces its own output row. Aggregate the right table to one row per key before joining.

When should I use INNER JOIN instead of LEFT JOIN?

Use INNER JOIN when you only want rows that exist on both sides. Use LEFT JOIN when the left table is the population you are measuring and missing matches matter.

Does the order of tables matter in an INNER JOIN?

Not for the result. The database may reorder an INNER JOIN. In a LEFT JOIN the order matters, because the left table is the one that is kept in full.

References

Run these queries yourself.

Predict the result, run it, and check the row count. That habit is what Practice trains.