# Join Pitfalls: Duplicates & Fan-Out

> See how joining two child tables multiplies rows, why NULL keys vanish, and how to spot a join that returns too many rows.

Subject: SQL | Lesson 18 of 44 | 5 min read
Interactive lesson: https://codewithanimation.in/sql/join-pitfalls
Previous: [UNION, INTERSECT & EXCEPT](https://codewithanimation.in/sql/set-operations.md) | Next: [Aggregate Functions](https://codewithanimation.in/sql/aggregate-functions.md)

## More rows than you expect

A join makes one row for every matching pair. When a parent row has several children, the parent repeats, once per child. This growth is called **fan-out**.

Fan-out is harmless on its own. It becomes a bug when you join two child tables to the same parent: their counts multiply.

## Wrong numbers that look right

A query with fan-out runs without an error and shows plausible rows. If you then add up order values, each order is counted several times and the total is wrong. Knowing the pattern is how you catch it.

## Two child tables, one parent

```sql
SELECT c.name, o.id AS order_id, t.id AS ticket_id
FROM customers AS c
JOIN shop_orders AS o ON c.id = o.customer_id
JOIN tickets AS t ON c.id = t.customer_id;
```

Asha has 2 orders and 2 tickets, so she gets 2 x 2 = 4 rows. Ben has 1 order and 1 ticket, so he gets 1. The result has 5 rows. Each of Asha's orders appears twice, once per ticket, even though the two tables have nothing to do with each other.

The other trap is silent loss. Ticket 204 has a `NULL` `customer_id`. A `NULL` never matches in `ON`, so an `INNER JOIN` drops the ticket. Joining customers to tickets returns 3 rows, even though there are 4 tickets.

## Common mistake: DISTINCT hides the problem

`SELECT DISTINCT` can make the repeated rows disappear, and the result looks right. But the database still built all the rows first, and DISTINCT can also hide rows that are truly separate.

The better fix is to avoid the multiplication: join only the tables the question needs, or summarise each child table before joining (the next stage shows how).

## Tip: Predict the count, then compare

Before you run a join, say the expected row count out loud. For a parent with children, it is the number of children. If the real count is bigger, you have fan-out. If it is smaller, rows are being dropped, often by a NULL key.

## Try it: Watch the rows multiply

Run the three joins and compare the counts. In the last one, look at the rows marked as repeats, then try the DISTINCT patch.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/join-pitfalls

## Check yourself

### Question 1

A customer has 3 orders and 2 tickets. Joining customers to both tables gives how many rows for that customer?

- A. 6
- B. 5
- C. 3

Correct answer: A

Each of the 3 orders is paired with each of the 2 tickets, so 3 x 2 = 6.

- Why not B: 5 is 3 plus 2. The counts multiply, they do not add.
- Why not C: Every order is paired with every ticket for the same customer.

### Question 2

Four tickets exist, one with a NULL customer_id. `customers INNER JOIN tickets ON c.id = t.customer_id` returns only 3 rows. Why?

- A. The NULL ticket never matches, so it is dropped
- B. The database removes the oldest ticket
- C. INNER JOIN only returns three rows at a time

Correct answer: A

NULL = anything is not true, so the ticket finds no customer and an INNER JOIN leaves it out.

- Why not B: Nothing is removed by age. The cause is the NULL key.
- Why not C: There is no such limit. The row count depends on the matches.

### Question 3

You joined two child tables and some orders appear twice. What is the best fix?

- A. Join only the tables the question needs, or summarise one child table first
- B. Always add DISTINCT and move on
- C. Change INNER JOIN to LEFT JOIN

Correct answer: A

The repeats come from the multiplication, so remove the multiplication instead of hiding it.

- Why not B: DISTINCT hides the repeats but not the cause, and it can hide rows that are truly separate.
- Why not C: That keeps more rows, so it cannot reduce the repeats.

## Remember this

- A join returns one row per matching pair, so a parent repeats once per child.
- Two child tables joined to one parent multiply: 2 orders and 2 tickets make 4 rows.
- A NULL key never matches, so those rows vanish from an INNER JOIN without a warning.
- DISTINCT hides repeats but does not fix the cause. Predict the row count before you run a join.
