SQL reconciliation · Original fictional case · Free files and answers
Stop a refund JOIN from multiplying sales
A marketplace has separate order lines and refund events. Produce one ledger row per order before finance compares net sales.
Suggested time: 15–25 minutes. All records, companies, rates and work policies are invented for this exercise.
Your output contract
- Aggregate sales lines and refunds separately before joining. Amounts are integer USD cents.
- Collapse identical refund events using all three fields. Refunds for unknown orders go to a separate review; exclude them from this order ledger.
- Keep all four order IDs, including zero-value A13. Return order_id, gross_cents, refund_cents, net_cents sorted by order_id.
Required columns: order_id, gross_cents, refund_cents, net_cents. Row order is part of the contract. Empty output CSV fields represent NULL; zero remains 0.
Original source tables
lines
6 source rows · Download lines CSV
| order_id TEXT | line_id TEXT | amount_cents INTEGER |
|---|---|---|
| A10 | L1 | 10000 |
| A10 | L2 | 5000 |
| A11 | L3 | 8000 |
| A12 | L4 | 6000 |
| A12 | L5 | 4000 |
| A13 | L6 | 0 |
refunds
5 source rows · Download refunds CSV
| refund_id TEXT | order_id TEXT | amount_cents INTEGER |
|---|---|---|
| R1 | A10 | 2000 |
| R2 | A10 | 1000 |
| R2 | A10 | 1000 |
| R3 | A12 | 2500 |
| R4 | A99 | 700 |
Plan the transformation
- First inspect A10: two sale lines and two unique refunds create four rows in a raw join.
- Deduplicate events, sum each table to order grain, then LEFT JOIN onto sales.
- Use COALESCE for orders without refunds and verify the net total independently.
Watch for this mistake
SUM(DISTINCT amount_cents) would discard legitimate equal-value lines. Deduplicate event identity, not money.
Run and check your SQLite result
The database runs locally in a worker. Use one SELECT or WITH query. Your query is saved on this device when storage is available. Download your SQL before clearing browser data.
Or work in Excel or another spreadsheet, then upload the final CSV with exactly the required columns.
Worked SQL and complete expected output
The orphan R4 is 700 cents and is not part of the 27,500-cent net ledger.
WITH s AS (SELECT order_id,SUM(amount_cents) gross_cents FROM lines GROUP BY order_id), r AS (SELECT order_id,SUM(amount_cents) refund_cents FROM (SELECT DISTINCT * FROM refunds) GROUP BY order_id) SELECT s.order_id,s.gross_cents,COALESCE(r.refund_cents,0) refund_cents,s.gross_cents-COALESCE(r.refund_cents,0) net_cents FROM s LEFT JOIN r USING(order_id) ORDER BY s.order_id;
| order_id | gross_cents | refund_cents | net_cents |
|---|---|---|---|
| A10 | 15000 | 3000 | 12000 |
| A11 | 8000 | 0 | 8000 |
| A12 | 10000 | 2500 | 7500 |
| A13 | 0 | 0 | 0 |
Download reference SQL · Download complete expected CSV · Download an empty answer CSV · Download SQLite setup
What to pass to a colleague
Keep the checked output and SQL or spreadsheet result. Explain the exception policy, the cross-check above and any held records. The checker confirms this fixed case’s output; it does not evaluate arbitrary private business data.
SQLite SELECT, joins and aggregation reference · Continue with the larger workplace projects