← SQL workplace practice

Cash allocation and financial controls · Original fictional data · Free files and full answers

Separate excess cash from unpaid receivables

At day9 for every customer present in invoices or cash, return customer,invoice_cents,net_cash_cents,unapplied_credit=max(0,cash-invoice),unpaid=max(0,invoice-cash). Sort customer.

Use this fixed business case to build a checked CSV and a replayable SQL handoff. No account required. Scenario datasets are shared across related exercises; each exercise has its own decision and output contract.

Your output contract

  1. At day9 for every customer present in invoices or cash, return customer,invoice_cents,net_cash_cents,unapplied_credit=max(0,cash-invoice),unpaid=max(0,invoice-cash). Sort customer.
  2. Return exactly the declared columns and order. Money uses integer cents; time and date units follow the task and field dictionary. NULL is unknown and differs from numeric zero.
  3. These are original fictional records and exercise policies. Scenario data may be shared with related tasks, whose decisions and output contracts differ.

Return columns exactly as customer, invoice_cents, net_cash_cents, unapplied_credit, unpaid. Sort order follows the task. Output has 4 rows; an empty CSV field means NULL.

Source data

invoices

6 source rows · Download invoices CSV

id INTEGERcustomer TEXTdue INTEGERcents INTEGER
1A1500
2A5400
3A9300
4B2600
5B7200
6C4250

cash

7 source rows · Download cash CSV

id INTEGERcustomer TEXTday INTEGERcents INTEGERoriginal_id INTEGER
1A3700NULL
2A8300NULL
3B41000NULL
4B6-2003
5D5150NULL
6C10100NULL
7C10100NULL

milestones

5 source rows · Download milestones CSV

contract TEXTname TEXTrequired INTEGERapproved INTEGERcents INTEGER
Xdesign111000
Xreview102000
Ydesign11500
Yreview11700
Zoptional00300
Field dictionary and schema
invoices:
  id INTEGER — Stable source record identifier; not a row position.
  customer TEXT — Literal business key or category label. Match exactly and retain NULL where present.
  due INTEGER — Invoice due day used for oldest-first order.
  cents INTEGER — Signed integer currency cents.

cash:
  id INTEGER — Stable source record identifier; not a row position.
  customer TEXT — Literal business key or category label. Match exactly and retain NULL where present.
  day INTEGER — Integer day index, or ISO YYYY-MM-DD date when stored as text; the task declares cutoff semantics.
  cents INTEGER — Signed integer currency cents.
  original_id INTEGER — Referenced original cash capture identifier, NULL for an original record.

milestones:
  contract TEXT — Literal business key or category label. Match exactly and retain NULL where present.
  name TEXT — Literal business key or category label. Match exactly and retain NULL where present.
  required INTEGER — Boolean requirement flag,1 required and0 optional.
  approved INTEGER — Boolean approval flag.
  cents INTEGER — Signed integer currency cents.

Download complete SQLite setup

A method you can reuse

  1. At day9 for every customer present in invoices or cash, return customer,invoice_cents,net_cash_cents,unapplied_credit=max(0,cash-invoice),unpaid=max(0,invoice-cash). Sort customer.
  2. For the first output record, customer = A; invoice_cents = 1200; net_cash_cents = 1000; unapplied_credit = 0; unpaid = 200.
  3. Inspect the source grain, keys and ties. Compare each result column with the full worked SQL and expected CSV.

Watch for this wrong turn

A net balance alone cannot distinguish a customer credit from an open receivable.

Run and check in SQLite

Your SQL runs locally in the existing SQLite worker. The checker compares every column, row, type, value and row order with this fixed output. It accepts equivalent SELECT or WITH queries. Browser storage keeps your SQL on this device when available.

Complete worked SQL and expected output
WITH k AS (SELECT customer FROM invoices UNION SELECT customer FROM cash), i AS (SELECT customer,SUM(cents) v FROM invoices GROUP BY customer), p AS (SELECT customer,SUM(cents) v FROM cash WHERE day<=9 GROUP BY customer) SELECT k.customer,COALESCE(i.v,0) invoice_cents,COALESCE(p.v,0) net_cash_cents,MAX(0,COALESCE(p.v,0)-COALESCE(i.v,0)) unapplied_credit,MAX(0,COALESCE(i.v,0)-COALESCE(p.v,0)) unpaid FROM k LEFT JOIN i USING(customer) LEFT JOIN p USING(customer) ORDER BY k.customer;
"customer","invoice_cents","net_cash_cents","unapplied_credit","unpaid"
"A","1200","1000","0","200"
"B","800","800","0","0"
"C","250","0","0","250"
"D","0","150","150","0"
Reference SQL · Complete expected CSV · Empty answer CSV

Choose another SQL task · Original data exception cases · Larger workplace projects