Skip to content

Debug a report

Here the query is already written, and the report is wrong. A debugging task checks how you look for errors, not how fast you type: run, compare with a simpler query, find the cause, say it, then fix it. The bugs are the classic ones from Revenue reports and Row multiplication.

D01. Merchant revenue report for August 2026

Section titled “D01. Merchant revenue report for August 2026”
SQLD01 · Difficulty: medium · Target time: 12 min

Bug report

"The August report below looks wrong. The totals are much higher than what we see in the channel dashboards, and sales from August 31 seem to be missing. Please fix the query."

What the report should show

Per merchant: number of transactions and revenue NET of refunds for August 2026 (UTC, start_time in [2026-08-01, 2026-09-01)), test purchases excluded, trials and promo included. Only merchants with at least one transaction.

Output columns: merchant_id, merchant_name, transactions, net_usdOrder: ORDER BY net_usd DESC, merchant_id

About the data: transactions, products, merchants, channel_payouts
  • transactions → products → merchants, as in q02.
  • channel_payouts has many rows per merchant: one per channel and month.

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

SELECT m.id AS merchant_id,
       m.name                       AS merchant_name,
       COUNT(*)                     AS transactions,
       SUM(t.charged_usd) AS net_usd
FROM merchants m
JOIN products p           ON p.merchant_id = m.id
JOIN transactions t       ON t.product_id = p.id
LEFT JOIN channel_payouts cp ON cp.merchant_id = m.id
WHERE t.start_time BETWEEN TIMESTAMP '2026-08-01' AND TIMESTAMP '2026-08-31'
  AND NOT t.is_test
GROUP BY m.id, m.name
ORDER BY net_usd DESC, m.id;
Hint: what the interviewer would nudge

Run it as it is and pick one merchant. Count that merchant's August transactions with a plain query, compare the two numbers, and read each clause against the "should show" text.

Reference solution
-- Bugs: (1) the join to channel_payouts multiplies every transaction by the number of payouts;
-- (2) charged_usd is gross — net of refunds is revenue_usd;
-- (3) BETWEEN ... '2026-08-31' stops at 2026-08-31 00:00 — use a half-open interval.
SELECT m.id AS merchant_id,
m.name AS merchant_name,
COUNT(*) AS transactions,
SUM(t.revenue_usd) AS net_usd
FROM merchants m
JOIN products p ON p.merchant_id = m.id
JOIN transactions t ON t.product_id = p.id
WHERE t.start_time >= TIMESTAMP '2026-08-01'
AND t.start_time < TIMESTAMP '2026-09-01'
AND NOT t.is_test
GROUP BY m.id, m.name
ORDER BY net_usd DESC, m.id;

Expected output

merchant_idmerchant_nametransactionsnet_usd
mer_06Orbit Fitness561299.52
mer_12Kite Works76902.36
mer_04North Pixel53885.53
mer_02Lumen Labs68802.36
mer_11Sunday Software75618.35
mer_08Brightleaf52617.51
mer_10Paper Fox43497.66
mer_09Cobalt Studio33451.72
mer_03Driftwood Labs19304.84
mer_05Maple Games27299.79
mer_07Quiet Mind25203.80
mer_01Pocket Quest23198.81
12 rows
WalkthroughTry the task first: the walkthrough is most useful after your own attempt.

What it is for. Finance sees totals that are too high and misses the sales of 31 August. Such an error stays invisible until someone compares the report with another source.

The bugs.

  1. LEFT JOIN channel_payouts on the merchant multiplies each transaction by the number of that merchant’s payouts (13 to 15 here). The report does not need this join at all: remove it.
  2. charged_usd is gross. Revenue net of refunds is revenue_usd.
  3. BETWEEN '2026-08-01' AND '2026-08-31' stops at 31 August 00:00. Use a half-open interval: >= '2026-08-01' and < '2026-09-01'.

How to find them. Compare COUNT(*) with a simple query without the join. If the report has more rows, the join multiplied them. Then recalculate one merchant by hand. For mer_06, fixing one bug at a time:

Steptransactionsnet_usd
The query as written75617255.42
1. Payouts join removed541232.53
2. revenue_usd instead of gross541219.54
3. Half-open interval561299.52

756 = 54 × 14: mer_06 has 14 payouts, and each one repeats every transaction. Across all merchants, BETWEEN lost 7 sales of 31 August, worth 148.94 net.

What to say. One sentence per bug, before the fix: “The bug is the join to channel_payouts, because every transaction is repeated once per payout.” The reason earns more credit than the edit.

In production. After the fix the query is the Q01 shape with two joins through primary keys, so an index on transactions(start_time) is enough. A simple guard would have caught bug 1: compare the report total with a plain sum of transactions for the same month.

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