Stop 06 · Joining TablesTopics · 10/10

Duplicate Rows from Joins (Fan-Out)

Part of the SQL Roadmap

In short

Your revenue suddenly looks three times bigger after adding a join. Nothing is wrong with the database. The join repeated each order once per item, and SUM counted the same rupees again and again.

When one row matches many rows in the other table, it repeats, and any SUM on the repeated columns gets inflated. This is one of the most common analyst mistakes.

The jaadu carries a tray of dosa, filter coffee and vada at a South Indian restaurant, with an identical paper bill pinned to each plate, while a puzzled cashier holds three copies.

Think of it like this

The bill stapled to every dish

At a busy Udupi restaurant, a waiter staples a copy of the full ₹1,200 table bill to every dish he brings: the dosa, the coffee and the vada. If the cashier adds up every stapled bill, she gets ₹3,600 for a ₹1,200 meal. Fan-out is the same: a one-side value copied onto each many-side row, then summed.

Watch it happen

orders joined to order_items

O1 ₹1200 · Dosa
0
O1 ₹1200 · Coffee
1
O1 ₹1200 · Vada
2
O2 ₹500 · Thali
3

1/4Two orders: O1 is ₹1,200 with three items, O2 is ₹500 with one item.

The big ideas

What you need to know

Know each table's grain

orders: one row per order. order_items: one row per item line. Joining them changes the grain to item lines.

One-side values repeat

Anything stored once per order (amount, shipping fee) is copied onto every item row.

Aggregate before joining

Sum or count the many-side table per key first, then join the result one-to-one.

Check row counts

If a join increases the row count and you didn't expect it, you have fan-out.

How it works

Fix a fanned-out total

  1. 1

    Spot it

    The total after the join is bigger than the total from the orders table alone.

  2. 2

    Find the grain

    Ask what one row means in each table you joined.

  3. 3

    Pre-aggregate

    Group the many-side table by the join key, so it has one row per order.

  4. 4

    Join one-to-one

    Join the summarised result. Row count now equals the number of orders.

  5. 5

    Re-check

    Compare the new total with SUM(amount) from orders alone.

See it in code

Walk through the code

sql
1WITH items_per_order AS (2  SELECT3    order_id,4    COUNT(*)      AS item_lines,5    SUM(quantity) AS units6  FROM order_items7  GROUP BY order_id8)9SELECT10  o.city,11  COUNT(*)      AS orders,12  SUM(o.amount) AS revenue,13  SUM(i.units)  AS units_sold14FROM orders AS o15JOIN items_per_order AS i16  ON i.order_id = o.order_id17GROUP BY o.city18ORDER BY revenue DESC;

1/3Squash order_items down to one row per order first.

Revenue and units per city without double counting. WITH names a step, which you'll explore in Phase 7. Works the same in MySQL 8 and SQL Server.

Save yourself the pain

Common mistakes

Oops

Fixing it with SUM(DISTINCT o.amount).

Do this instead

Two different orders of ₹499 would merge into one. Pre-aggregate the many side instead.

Oops

Joining orders to both order_items and payments in one go.

Do this instead

Two many-side tables multiply each other: 3 items × 2 payments = 6 rows per order. Aggregate each one separately first.

Oops

Only checking the final number.

Do this instead

Compare row counts before and after each join. Fan-out shows up there before it hides in a total.

Your mission

Catch the fan-out

~25 min

Quick check

An order worth ₹900 has 4 item lines. After joining orders to order_items, what does SUM(o.amount) give for that order?

Go deeper

  1. 1Join orders to order_items and watch an order-level total double count.
  2. 2Fix it by aggregating the many-side table first, then joining.
  3. 3Check the grain (what one row means) of every table before joining.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge