Skip to content

When a join multiplies your rows

A key that isn't unique multiplies rows, and every total and average after it is wrong with no error. Learn to spot it and prevent it.

Applied ~7 min8 steps

What runs in CodeSplash

You will be able to

  • Explain how a non-unique key inflates row counts
  • Show how fan-out changes an average
  • Check a key for uniqueness and aggregate before joining

Read first

01

A lookup with two rows per key

Read

Biscoe was surveyed twice, so a survey-notes table has two rows for it. The lookup below is made up to show what happens.

WITH notes(island, note) AS (
  VALUES ('Biscoe', '2007 survey'), ('Biscoe', '2008 survey'),
         ('Dream', '2007 survey'), ('Torgersen', '2007 survey')
)
SELECT count(*) AS n
FROM palmer_penguins_sample AS p
JOIN notes AS n ON p.island = n.island;

Every island has a match, so nothing can be lost. How many rows would you expect back?

02

Predict the count

Predict

Predict first

There are 168 penguins on Biscoe, 124 on Dream and 52 on Torgersen. What does the query return?

344, one per penguin512An error
Reveal
  1. Each Biscoe penguin matches two notes rows, so it appears twice: 168×2+124+52=512168 \times 2 + 124 + 52 = 512. No error, no warning. This is a fan-outFan-outA join that returns extra rows because a key repeats, which silently inflates every total and average after it., and every total you compute from here on counts Biscoe birds double.
03

What it does to an average

In the app

Run these two and compare.

SELECT round(avg(body_mass), 1) AS mean_mass
FROM palmer_penguins_sample;

WITH notes(island, note) AS (
  VALUES ('Biscoe', '2007 survey'), ('Biscoe', '2008 survey'),
         ('Dream', '2007 survey'), ('Torgersen', '2007 survey')
)
SELECT round(avg(p.body_mass), 1) AS mean_mass
FROM palmer_penguins_sample AS p
JOIN notes AS n ON p.island = n.island;

The first query gives the true mean, about 4,200 grams. The joined mean comes out about 170 grams heavier, because Biscoe's heavy Gentoos now count twice. Nobody would catch that by reading the result.

04

Check the key first

Explore

Before you join, ask whether the key is unique on the side you're adding. This query lists any key that appears more than once:

WITH notes(island, note) AS (
  VALUES ('Biscoe', '2007 survey'), ('Biscoe', '2008 survey'),
         ('Dream', '2007 survey'), ('Torgersen', '2007 survey')
)
SELECT island, count(*) AS n
FROM notes
GROUP BY island
HAVING count(*) > 1;

An empty result means it's safe. Here, Biscoe appears, and you've caught it before it did any damage.

05

Fix it

Explore

Reduce the lookup to one row per key before you join. What's right depends on what you need: the first, the latest, a count.

WITH notes(island, note) AS (
  VALUES ('Biscoe', '2007 survey'), ('Biscoe', '2008 survey'),
         ('Dream', '2007 survey'), ('Torgersen', '2007 survey')
),
one_per_island AS (
  SELECT island, count(*) AS surveys
  FROM notes
  GROUP BY island
)
SELECT count(*) AS n, round(avg(p.body_mass), 1) AS mean_mass
FROM palmer_penguins_sample AS p
JOIN one_per_island AS o ON p.island = o.island;

Back to 344 rows and the true mean. In a recipe, Prepare's join step warns you about this explosion before you apply it.

06

Check your understanding

Checkpoint

Check yourself

How many rows does the first query in this lesson return?

Show the answer

Answer: 512

168×2+124+52=512168 \times 2 + 124 + 52 = 512.

Check yourself

Which is the clearest sign of a fan-out?

Show the answer

Answer: b

A join to a lookup should never add rows. If it does, your key isn't unique on the lookup side.

Check yourself

Your sales table joined to a price-history table inflates revenue. What fixes it?

Show the answer

Answer: b

DISTINCT hides the symptom and can drop real duplicate sales. A LEFT JOIN fans out just the same. Fix the key.

07

Audit one report

Reflect

Think of a number you report that passes through a join. How do you know it isn't double counting? Add a before-and-after row count to that query today. A descriptive summary is a good second check that nothing shifted:

08

Recap

Recap
  • A non-unique key multiplies rows silently, and every sum and average after it is wrong.
  • Check the key with GROUP BY … HAVING count(*) > 1 before you join.
  • Aggregate the lookup to one row per key, and compare row counts before and after.

Concepts in this lesson