← SQL and spreadsheet practice

SQL accounting · Original fictional case · Free files and answers

Reconcile bank and ledger exceptions on both sides

A treasurer has one bank export and one internal cash ledger. Deliver every payment reference with a signed bank-minus-ledger difference and an explanation status.

Suggested time: 15–25 minutes. All records, companies, rates and work policies are invented for this exercise.

Your output contract

  1. Each reference is unique within each source in this case. Preserve references present on either side.
  2. Use MATCH only when both records exist and amounts agree. Other statuses are AMOUNT_DIFF, BANK_ONLY, or LEDGER_ONLY.
  3. For difference arithmetic only, treat an absent side as zero. Output reference,bank_cents,ledger_cents,difference_cents,status; absent source amounts remain NULL.

Required columns: reference, bank_cents, ledger_cents, difference_cents, status. Row order is part of the contract. Empty output CSV fields represent NULL; zero remains 0.

Original source tables

bank

5 source rows · Download bank CSV

reference TEXTamount_cents INTEGER
T110000
T24000
T3-250
T5900
T60

ledger

4 source rows · Download ledger CSV

reference TEXTamount_cents INTEGER
T110000
T24500
T41200
T60

Plan the transformation

  1. Build the union of reference keys; this avoids losing ledger-only T4.
  2. Join both sources to that key list and distinguish missing rows from zero amounts.
  3. Sum the differences and compare total bank minus total ledger.

Watch for this mistake

Checking only bank rows misses T4. Using amount=0 to detect absence incorrectly marks T6 as missing.

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

Bank total 14,650 less ledger total 15,700 equals the exception sum of -1,050 cents.

WITH k AS (SELECT reference FROM bank UNION SELECT reference FROM ledger) SELECT k.reference,b.amount_cents bank_cents,l.amount_cents ledger_cents,COALESCE(b.amount_cents,0)-COALESCE(l.amount_cents,0) difference_cents,CASE WHEN b.reference IS NULL THEN 'LEDGER_ONLY' WHEN l.reference IS NULL THEN 'BANK_ONLY' WHEN b.amount_cents=l.amount_cents THEN 'MATCH' ELSE 'AMOUNT_DIFF' END status FROM k LEFT JOIN bank b USING(reference) LEFT JOIN ledger l USING(reference) ORDER BY k.reference;
referencebank_centsledger_centsdifference_centsstatus
T110000100000MATCH
T240004500-500AMOUNT_DIFF
T3-250NULL-250BANK_ONLY
T4NULL1200-1200LEDGER_ONLY
T5900NULL900BANK_ONLY
T6000MATCH

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