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
- Each reference is unique within each source in this case. Preserve references present on either side.
- Use MATCH only when both records exist and amounts agree. Other statuses are AMOUNT_DIFF, BANK_ONLY, or LEDGER_ONLY.
- 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 TEXT | amount_cents INTEGER |
|---|---|
| T1 | 10000 |
| T2 | 4000 |
| T3 | -250 |
| T5 | 900 |
| T6 | 0 |
ledger
4 source rows · Download ledger CSV
| reference TEXT | amount_cents INTEGER |
|---|---|
| T1 | 10000 |
| T2 | 4500 |
| T4 | 1200 |
| T6 | 0 |
Plan the transformation
- Build the union of reference keys; this avoids losing ledger-only T4.
- Join both sources to that key list and distinguish missing rows from zero amounts.
- 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;
| reference | bank_cents | ledger_cents | difference_cents | status |
|---|---|---|---|---|
| T1 | 10000 | 10000 | 0 | MATCH |
| T2 | 4000 | 4500 | -500 | AMOUNT_DIFF |
| T3 | -250 | NULL | -250 | BANK_ONLY |
| T4 | NULL | 1200 | -1200 | LEDGER_ONLY |
| T5 | 900 | NULL | 900 | BANK_ONLY |
| T6 | 0 | 0 | 0 | MATCH |
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