← Back to SQL, page 3

sql-0059 · Easy fresher

Both tables have the key 'Pen' twice. How many rows does the join return, and why?

A join pairs every matching row on the left with every matching row on the right. Two 'Pen' rows on each side give 2 x 2 = 4 joined rows for that key. This is expected behaviour, not a bug, but it explains many inflated counts when a lookup table that was assumed unique has duplicates.

Full answer

A join is not a lookup that stops at the first match. For each left row it returns one output row per matching right row.

sql
WITH prices(product, price) AS (VALUES ('Pen', 1), ('Pen', 2), ('Bag', 30)),
     stock(product, shelf)  AS (VALUES ('Pen', 'A1'), ('Pen', 'B4'), ('Bag', 'C2'))
SELECT p.product, p.price, s.shelf
FROM prices p JOIN stock s ON s.product = p.product
ORDER BY p.product, p.price, s.shelf;
productpriceshelf
Bag30C2
Pen1A1
Pen1B4
Pen2A1
Pen2B4

Count it: 2 Pen rows in the price list times 2 Pen rows in the stock table gives 4 rows for Pen, plus 1 x 1 for Bag.

A frequent real cause is a lookup table that someone assumed was unique. Here a price list accidentally holds two current prices for Pen, and every Pen sale is doubled when joined:

sql
CREATE TABLE price_list (product TEXT, price INTEGER);
INSERT INTO price_list VALUES ('Pen', 1), ('Pen', 1), ('Bag', 30);
CREATE TABLE sales (product TEXT, qty INTEGER);
INSERT INTO sales VALUES ('Pen', 10), ('Bag', 1);
SELECT SUM(s.qty * p.price) AS revenue
FROM sales s JOIN price_list p ON p.product = s.product;
revenue
50

The right answer is 10 x 1 + 1 x 30 = 40, but the duplicate Pen price row doubled the pen revenue. Finding the culprit:

sql
SELECT product, COUNT(*) AS copies
FROM price_list
GROUP BY product
HAVING COUNT(*) > 1;
productcopies
Pen2

The protection is a constraint, not a DISTINCT in the report: make the lookup key a PRIMARY KEY or UNIQUE so the duplicate can never be inserted. To find existing duplicates, group by the key and keep groups with COUNT(*) > 1.

sql
WITH prices(product, price) AS (VALUES ('Pen', 1), ('Pen', 2), ('Bag', 30)),
     stock(product, shelf)  AS (VALUES ('Pen', 'A1'), ('Pen', 'B4'), ('Bag', 'C2'))
SELECT p.product, p.price, s.shelf
FROM prices p JOIN stock s ON s.product = p.product
ORDER BY p.product, p.price, s.shelf;

CREATE TABLE price_list (product TEXT, price INTEGER);
INSERT INTO price_list VALUES ('Pen', 1), ('Pen', 1), ('Bag', 30);
CREATE TABLE sales (product TEXT, qty INTEGER);
INSERT INTO sales VALUES ('Pen', 10), ('Bag', 1);
SELECT SUM(s.qty * p.price) AS revenue
FROM sales s JOIN price_list p ON p.product = s.product;

SELECT product, COUNT(*) AS copies
FROM price_list
GROUP BY product
HAVING COUNT(*) > 1;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Each left row pairs with every matching right row
  • Duplicate keys on both sides multiply: m x n rows per key
  • Duplicate keys in a lookup table inflate totals
  • Enforce uniqueness with a constraint instead of DISTINCT

Common mistakes

  • Assuming a join returns one match per left row
  • Hiding duplicated rows with DISTINCT rather than fixing the data

Follow-ups

  • How do you find duplicate keys in the price list?
  • How would you pick only the latest price when several exist?

Tips

  • Say the m x n rule before showing the result