SQL receivables · Original fictional case · Free files and answers
Age receivables after partial payments
Collections needs open balances as of 12 November. Apply only posted payments received by the cutoff and keep an overpayment as a credit instead of erasing it.
Suggested time: 15–25 minutes. All records, companies, rates and work policies are invented for this exercise.
Your output contract
- Cutoff is 2026-11-12 inclusive; ignore pending and later payments.
- Balance equals invoice minus eligible payment total. A negative balance is CREDIT. Positive balances due before cutoff are OVERDUE, due on cutoff are DUE_TODAY, and later balances are NOT_DUE.
- Output invoice_id,balance_cents,collection_status in invoice_id order.
Required columns: invoice_id, balance_cents, collection_status. Row order is part of the contract. Empty output CSV fields represent NULL; zero remains 0.
Original source tables
invoices
4 source rows · Download invoices CSV
| invoice_id TEXT | due_date TEXT | amount_cents INTEGER |
|---|---|---|
| V1 | 2026-11-01 | 10000 |
| V2 | 2026-11-12 | 6000 |
| V3 | 2026-11-20 | 8000 |
| V4 | 2026-10-01 | 5000 |
payments
5 source rows · Download payments CSV
| payment_id TEXT | invoice_id TEXT | paid_on TEXT | status TEXT | amount_cents INTEGER |
|---|---|---|---|---|
| M1 | V1 | 2026-11-03 | posted | 3000 |
| M2 | V1 | 2026-11-10 | posted | 2000 |
| M3 | V2 | 2026-11-13 | posted | 6000 |
| M4 | V3 | 2026-11-11 | pending | 8000 |
| M5 | V4 | 2026-11-08 | posted | 5500 |
Plan the transformation
- Aggregate posted payments through cutoff, so V1 receives 5,000 cents.
- Check negative balances before date-based statuses.
- Validate open debits and credits separately, then reconcile the signed balance.
Watch for this mistake
Summing all payments includes V2’s future receipt and V3’s pending receipt, understating collections work.
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
Positive balances total 19,000 cents; the credit is 500 cents; signed balance is 18,500 cents.
WITH p AS (SELECT invoice_id,SUM(amount_cents) paid FROM payments WHERE status='posted' AND paid_on<='2026-11-12' GROUP BY invoice_id), b AS (SELECT i.*,amount_cents-COALESCE(p.paid,0) balance_cents FROM invoices i LEFT JOIN p USING(invoice_id)) SELECT invoice_id,balance_cents,CASE WHEN balance_cents<0 THEN 'CREDIT' WHEN balance_cents=0 THEN 'PAID' WHEN due_date<'2026-11-12' THEN 'OVERDUE' WHEN due_date='2026-11-12' THEN 'DUE_TODAY' ELSE 'NOT_DUE' END collection_status FROM b ORDER BY invoice_id;
| invoice_id | balance_cents | collection_status |
|---|---|---|
| V1 | 5000 | OVERDUE |
| V2 | 6000 | DUE_TODAY |
| V3 | 8000 | NOT_DUE |
| V4 | -500 | CREDIT |
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