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.
What runs in CodeSplash
- Descriptive statistics Verified in Python and R
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
A lookup with two rows per key
ReadBiscoe 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?
Predict the count
PredictPredict first
There are 168 penguins on Biscoe, 124 on Dream and 52 on Torgersen. What does the query return?
Reveal
- Each Biscoe penguin matches two notes rows, so it appears twice: . 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.
What it does to an average
In the appRun 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.
Check the key first
ExploreBefore 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.
Fix it
ExploreReduce 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.
Check your understanding
CheckpointCheck yourself
How many rows does the first query in this lesson return?
Show the answer
Answer: 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.
Audit one report
ReflectThink 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:
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(*) > 1before you join. - Aggregate the lookup to one row per key, and compare row counts before and after.
Concepts in this lesson