Skip to content
mentoor

Topic 2 of 5 · 4 min read

Joining tables without losing rows

Goal: Predict the row count of a join from its key cardinality and pick the join type that keeps the rows the question needs.

A join pairs rows from two tables on a key. Before you run it, predict how many rows it returns: an inner join keeps only matches, a left join keeps every left row, and a duplicate key on the right multiplies rows.

Check yourself · 1 of 3

100 customers, 300 orders, each order belongs to exactly one customer. At most how many rows does an inner join on customer_id return?

Choose one answer

Why it matters

Joins are where reports go quietly wrong. A missing customer or a doubled order total looks like a normal result. The habit that protects you is to estimate the row count before looking at the data.

How to do it

  1. Know the grain of each table. One row per customer? One row per order? The join key must be unique on at least one side, or rows multiply.
  2. Pick the join by the question. "Customers and their orders" where customers without orders do not matter: INNER JOIN. "Every customer, with order data if any": LEFT JOIN.
  3. Predict the count. Inner join of 100 customers and 300 orders, each order with one customer: at most 300 rows. A left join gives at least 100.
  4. Keep right-side filters in ON. After a left join, a WHERE on a right-hand column removes the unmatched rows, because their value is NULL. The left join becomes an inner join without any warning.
  5. Compare counts. Run count(*) before and after the join. If it grew when you did not expect it, a key is duplicated.

Before and after

The request: "every customer and the number of paid orders they have, zero if none."

-- Before
SELECT c.id, count(o.id) AS paid_orders
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.status = 'paid'
GROUP BY c.id;

Customers with no orders have o.status NULL, so WHERE removes them. The result has no zero rows.

-- After
SELECT c.id, count(o.id) AS paid_orders
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id AND o.status = 'paid'
GROUP BY c.id;

What goes wrong

  • A filter on the right table in WHERE. The left join silently becomes an inner join.
  • Joining on a non-unique key. If customers holds two rows with the same email and you join on email, every order appears twice.
  • Joining on the wrong column. Joining orders.id = customers.id is valid SQL and returns nonsense.
  • A missing join condition. FROM customers, orders with no matching condition in WHERE pairs every row with every row: 100 customers times 300 orders is 30,000 rows.

Examples

Counting the multiplication

customers has 100 rows. orders has 300 rows, each with one customer_id.

  • Inner join on customer_id: up to 300 rows.
  • Left join: at least 100 rows, 300 if every customer ordered.
  • If customers accidentally holds each customer twice, the inner join returns 600 rows and every revenue total doubles.

Finding customers with no orders

SELECT c.id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL;

Here the WHERE filter on the right table is the point: it keeps only the rows where no match was found. This pattern is called an anti-join.

Key points

  • Decide the grain of each table, then predict the row count before you run the join.
  • An inner join drops unmatched rows; a left join keeps every row from the left table.
  • A WHERE filter on the right table of a left join removes the unmatched rows; put that condition in ON.
  • A duplicate key on one side multiplies the rows from the other side.
  • Compare count(*) before and after the join to catch row multiplication.

Common mistakes

  • Putting a condition on the right table in WHERE after a LEFT JOIN and losing the unmatched rows.
  • Joining on a column that is not unique and not noticing the doubled rows.
  • Listing two tables with a comma and forgetting the join condition, which produces every pairing of rows.
  • Choosing INNER JOIN by habit when the question asks for every row on one side.

Check yourself · 2 of 3

After a LEFT JOIN from customers to orders, a query adds WHERE orders.status = 'paid'. What happens to customers with no orders?

Choose one answer

Check yourself · 3 of 3

A join of orders to customers on email returns 600 rows, but orders has 300 rows. What is the most likely cause?

Choose one answer

Practice items, not counted towards your mastery.

Start here