← SQL and spreadsheet practice

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

  1. Aggregate sales lines and refunds separately before joining. Amounts are integer USD cents.
  2. Collapse identical refund events using all three fields. Refunds for unknown orders go to a separate review; exclude them from this order ledger.
  3. 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 TEXTline_id TEXTamount_cents INTEGER
A10L110000
A10L25000
A11L38000
A12L46000
A12L54000
A13L60

refunds

5 source rows · Download refunds CSV

refund_id TEXTorder_id TEXTamount_cents INTEGER
R1A102000
R2A101000
R2A101000
R3A122500
R4A99700

Plan the transformation

  1. First inspect A10: two sale lines and two unique refunds create four rows in a raw join.
  2. Deduplicate events, sum each table to order grain, then LEFT JOIN onto sales.
  3. 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_idgross_centsrefund_centsnet_cents
A1015000300012000
A11800008000
A121000025007500
A13000

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

Try another case