← SQL and spreadsheet practice

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

  1. Cutoff is 2026-11-12 inclusive; ignore pending and later payments.
  2. 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.
  3. 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 TEXTdue_date TEXTamount_cents INTEGER
V12026-11-0110000
V22026-11-126000
V32026-11-208000
V42026-10-015000

payments

5 source rows · Download payments CSV

payment_id TEXTinvoice_id TEXTpaid_on TEXTstatus TEXTamount_cents INTEGER
M1V12026-11-03posted3000
M2V12026-11-10posted2000
M3V22026-11-13posted6000
M4V32026-11-11pending8000
M5V42026-11-08posted5500

Plan the transformation

  1. Aggregate posted payments through cutoff, so V1 receives 5,000 cents.
  2. Check negative balances before date-based statuses.
  3. 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_idbalance_centscollection_status
V15000OVERDUE
V26000DUE_TODAY
V38000NOT_DUE
V4-500CREDIT

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