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”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.
Say it: Fixed: the payouts join multiplied rows, gross was used instead of net, and BETWEEN lost August 31.
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_usdFROM merchants mJOIN products p ON p.merchant_id = m.idJOIN transactions t ON t.product_id = p.idWHERE t.start_time >= TIMESTAMP '2026-08-01' AND t.start_time < TIMESTAMP '2026-09-01' AND NOT t.is_testGROUP BY m.id, m.nameORDER BY net_usd DESC, m.id;Expected output
| merchant_id | merchant_name | transactions | net_usd |
|---|---|---|---|
| mer_06 | Orbit Fitness | 56 | 1299.52 |
| mer_12 | Kite Works | 76 | 902.36 |
| mer_04 | North Pixel | 53 | 885.53 |
| mer_02 | Lumen Labs | 68 | 802.36 |
| mer_11 | Sunday Software | 75 | 618.35 |
| mer_08 | Brightleaf | 52 | 617.51 |
| mer_10 | Paper Fox | 43 | 497.66 |
| mer_09 | Cobalt Studio | 33 | 451.72 |
| mer_03 | Driftwood Labs | 19 | 304.84 |
| mer_05 | Maple Games | 27 | 299.79 |
| mer_07 | Quiet Mind | 25 | 203.80 |
| mer_01 | Pocket Quest | 23 | 198.81 |
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.
LEFT JOIN channel_payoutson 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.charged_usdis gross. Revenue net of refunds isrevenue_usd.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:
| Step | transactions | net_usd |
|---|---|---|
| The query as written | 756 | 17255.42 |
| 1. Payouts join removed | 54 | 1232.53 |
2. revenue_usd instead of gross | 54 | 1219.54 |
| 3. Half-open interval | 56 | 1299.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.