Skip to content

Row multiplication

Many report tasks put two facts about one key side by side: products and advances per merchant, revenue and payouts per merchant. Both facts live in “many” tables. Join them directly and every row of one side pairs with every row of the other. The query runs, the sums grow, and nothing warns you.

A join returns one row for every pair of rows that match the condition. A join to the “one” side is safe: each transaction finds exactly one product, so the row count stays the same (Joins shows the check). A join to a “many” side repeats rows, and that is often what you want.

The trouble starts when two “many” tables hang off the same key and have no link to each other. A merchant has several products and several advances. An advance does not belong to a product. So the join cannot match them one to one: it matches every product of the merchant with every advance of the same merchant.

SQLTrap: what will it return?core

mer_09 has two products and seven advances worth 1122.81 in total. How many rows does this query return, and what sum?

Guess the result first, then run the query. Fix the query, then Check.

Output columns: id, products, advanced_usd

Tables, columns and how they connect

merchants — merchants (12 rows): businesses that sell subscriptions on the platform

  • id — key: mer_01 …
id
mer_01
mer_02
mer_03

products — products (20 rows); a merchant can own several

  • id — key: prd_01 …
  • merchant_id — → merchants.id
idmerchant_id
prd_01mer_01
prd_02mer_02
prd_03mer_03

advances — advances: early payouts to merchants against marketplace payouts that have not arrived yet

  • merchant_id — → merchants.id
  • amount_usd — amount advanced (principal)
merchant_idamount_usd
mer_0148.75
mer_0196.34
mer_0168.38

Joins: products.merchant_id = merchants.id; advances.merchant_id = merchants.id

"Today" is 2026-09-30 00:00 UTC. All times are UTC. Every run is rolled back, so you cannot break the data.

SELECT COUNT(*)          AS joined_rows,
       SUM(v.amount_usd) AS advanced_usd
FROM merchants m
JOIN products p     ON p.merchant_id = m.id
JOIN advances v ON v.merchant_id = m.id
WHERE m.id = 'mer_09';
Show answer

14 rows and 2245.62, exactly twice the real total. Products and advances are not linked to each other, so the join pairs every product with every advance: 2 × 7. Reduce each table to one row per merchant first, in a CTE or a subquery, and join after that.

This is what PostgreSQL returns for the query as it is:

joined_rowsadvanced_usd
142245.62
Show the fix
WITH product_counts AS (SELECT merchant_id, COUNT(*) AS products FROM products GROUP BY merchant_id),
adv AS (SELECT merchant_id, SUM(amount_usd) AS advanced_usd FROM advances GROUP BY merchant_id)
SELECT m.id, n.products, v.advanced_usd
FROM merchants m
JOIN product_counts n ON n.merchant_id = m.id
JOIN adv v ON v.merchant_id = m.id
WHERE m.id = 'mer_09';
idproductsadvanced_usd
mer_0921122.81
1 row

Here is the same mistake for every merchant with an advance:

SELECT m.id AS merchant_id,
COUNT(p.id) AS products,
SUM(v.amount_usd) AS advanced_usd
FROM merchants m
JOIN products p ON p.merchant_id = m.id
JOIN advances v ON v.merchant_id = m.id
GROUP BY m.id
ORDER BY m.id;
merchant_idproductsadvanced_usd
mer_016359.72
mer_037779.13
mer_046934.77
mer_076431.66
mer_09142245.62
mer_11122392.46

Four merchants have one product, so their sums are correct. Only mer_09 and mer_11, with two products each, get double sums. This is why the bug survives a quick look: the first rows seem fine. The products column is wrong everywhere: it counts pairs, not products.

Two quick “fixes” make it worse, because they hide the problem:

  • COUNT(DISTINCT p.id) repairs the products column (1, 1, 1, 1, 2, 2). The sum for mer_09 stays 2245.62.
  • SUM(DISTINCT v.amount_usd) adds each different amount once. mer_09 has two advances of 95.14 and two of 124.88, so the result is 902.79 instead of 1122.81. Equal amounts are normal in money data: the two of 124.88 are for July and for August.

Count the rows before and after the join. If the join was meant to add columns, the count must not change:

SELECT (SELECT COUNT(*) FROM advances) AS advances,
COUNT(*) AS rows_after_join,
SUM(v.amount_usd) AS advanced_usd
FROM advances v
JOIN products p ON p.merchant_id = v.merchant_id;
advancesrows_after_joinadvanced_usd
38517143.36

38 advances became 51 rows, and the total grew from 4824.32 to 7143.36. Before each join, ask one question: “For one row on the left, how many rows match on the right?” One means safe. Many means aggregate first.

The bigger the “many” sides, the bigger the error. Put September transactions and all advances together in one join:

SELECT p.merchant_id,
SUM(t.revenue_usd) AS net_usd,
SUM(v.amount_usd) AS advanced_usd,
COUNT(*) AS joined_rows
FROM transactions t
JOIN products p ON p.id = t.product_id
JOIN advances v ON v.merchant_id = p.merchant_id
WHERE NOT t.is_test
AND t.start_time >= TIMESTAMP '2026-09-01'
AND t.start_time < TIMESTAMP '2026-10-01'
GROUP BY p.merchant_id
ORDER BY p.merchant_id;
merchant_idnet_usdadvanced_usdjoined_rows
mer_011588.867554.12126
mer_032728.7415582.60140
mer_04933.8437390.80240

These are the first three of six rows. mer_01 has 21 transactions and 6 advances, so 126 rows. Revenue is multiplied by 6 and advances by 21. When a number is ten times larger than you expect, suspect row multiplication first.

Give each “many” table its own CTE that returns one row per key. Then join the CTEs. Every join is now one to one, and nothing can multiply.

WITH revenue AS (
SELECT p.merchant_id, SUM(t.revenue_usd) AS net_usd
FROM transactions t
JOIN products p ON p.id = t.product_id
WHERE NOT t.is_test
AND t.start_time >= TIMESTAMP '2026-09-01'
AND t.start_time < TIMESTAMP '2026-10-01'
GROUP BY p.merchant_id
),
advanced AS (
SELECT merchant_id, SUM(amount_usd) AS advanced_usd
FROM advances
GROUP BY merchant_id
)
SELECT v.merchant_id,
COALESCE(r.net_usd, 0) AS net_usd,
v.advanced_usd
FROM advanced v
LEFT JOIN revenue r ON r.merchant_id = v.merchant_id
ORDER BY v.merchant_id;
merchant_idnet_usdadvanced_usd
mer_01264.81359.72
mer_03389.82779.13
mer_04155.64934.77
mer_07225.80431.66
mer_09569.731122.81
mer_11359.531196.23

Two details matter here:

  • Which table goes in FROM. The task says “per merchant with an advance”, so the rows come from advanced. Revenue is joined with LEFT JOIN and COALESCE, so a merchant with advances and no sales stays in the result with 0. See LEFT JOIN and zeros.
  • Filters stay inside their CTE. The September filter belongs to revenue only. Advances are counted for all time.

The result must add up to the source tables. Compute the totals with no join that could multiply:

SELECT (SELECT SUM(amount_usd) FROM advances) AS advanced_usd,
(SELECT SUM(t.revenue_usd)
FROM transactions t
JOIN products p ON p.id = t.product_id
WHERE p.merchant_id IN (SELECT merchant_id FROM advances)
AND NOT t.is_test
AND t.start_time >= TIMESTAMP '2026-09-01'
AND t.start_time < TIMESTAMP '2026-10-01') AS net_usd;
advanced_usdnet_usd
4824.321965.33

The six rows above add up to the same 4824.32 and 1965.33. IN (SELECT …) only tests membership, so it never adds rows. EXISTS behaves the same way.

TalkSay it out loud

Your totals are much higher than the finance dashboard. What do you check first?

Say your answer out loud first, then open it.

Show the answer

“I check the joins first. I count the rows before and after each join, and I check one merchant with a separate simple query. If two tables have many rows per merchant, I aggregate each of them to one row per merchant and join after that.”

SQLWrite a querycore

For each merchant with at least one advance: the number of products and the total advanced amount.

Output columns: merchant_id, products, advanced_usdOrder: ORDER BY merchant_id

Tables, columns and how they connect

products — products (20 rows); a merchant can own several

  • id — key: prd_01 …
  • merchant_id — → merchants.id
idmerchant_id
prd_01mer_01
prd_02mer_02
prd_03mer_03

advances — advances: early payouts to merchants against marketplace payouts that have not arrived yet

  • merchant_id — → merchants.id
  • amount_usd — amount advanced (principal)
merchant_idamount_usd
mer_0148.75
mer_0196.34
mer_0168.38

"Today" is 2026-09-30 00:00 UTC. All times are UTC. Every run is rolled back, so you cannot break the data.

-- your query here
Show answer

Reference

WITH product_counts AS (
SELECT merchant_id, COUNT(*) AS products
FROM products
GROUP BY merchant_id
),
advanced AS (
SELECT merchant_id, SUM(amount_usd) AS advanced_usd
FROM advances
GROUP BY merchant_id
)
SELECT v.merchant_id, COALESCE(c.products, 0) AS products, v.advanced_usd
FROM advanced v
LEFT JOIN product_counts c ON c.merchant_id = v.merchant_id
ORDER BY v.merchant_id;

Expected output

merchant_idproductsadvanced_usd
mer_011359.72
mer_031779.13
mer_041934.77
mer_071431.66
mer_0921122.81
mer_1121196.23
6 rows

Products and advances are not linked, so a direct join of the three tables gives products × advances, and the sum grows. Each side becomes one row per merchant first, then the two CTEs are joined. LEFT JOIN from the advances is a safeguard: advances decide the rows, and products must not drop one. In this dataset every merchant has at least one product, so an inner join returns the same rows and Check cannot tell them apart. Say why you chose LEFT JOIN: a merchant with an advance but no product yet must stay in the result.

Site content: CC BY 4.0. Code: MIT. The exercises run in your browser: PostgreSQL by PGlite, Python by Pyodide.