← SQL workplace practice

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

Split invoice amounts into three installments exactly

For every invoice divide into3 integer-cent installments: first two use floor(cents/3), final installment receives the residual. Return invoice_id,installment,amount_cents ordered both.

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. For every invoice divide into3 integer-cent installments: first two use floor(cents/3), final installment receives the residual. Return invoice_id,installment,amount_cents ordered both.
  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 invoice_id, installment, amount_cents. Sort order follows the task. Output has 18 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. For every invoice divide into3 integer-cent installments: first two use floor(cents/3), final installment receives the residual. Return invoice_id,installment,amount_cents ordered both.
  2. For the first output record, invoice_id = 1; installment = 1; amount_cents = 166.
  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

Rounding three equal decimal shares independently can lose or invent a cent.

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 n(k) AS (VALUES(1),(2),(3)) SELECT i.id invoice_id,n.k installment,CASE WHEN n.k<3 THEN cents/3 ELSE cents-2*(cents/3) END amount_cents FROM invoices i CROSS JOIN n ORDER BY i.id,n.k;
"invoice_id","installment","amount_cents"
"1","1","166"
"1","2","166"
"1","3","168"
"2","1","133"
"2","2","133"
"2","3","134"
"3","1","100"
"3","2","100"
"3","3","100"
"4","1","200"
"4","2","200"
"4","3","200"
"5","1","66"
"5","2","66"
"5","3","68"
"6","1","83"
"6","2","83"
"6","3","84"
Reference SQL · Complete expected CSV · Empty answer CSV

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