← SQL workplace practice

Subscription service and billing · Original fictional data · Free files and full answers

Bill usage with graduated rather than volume pricing

For each contract sum only entitled usage. First100 units cost2 cents each; next100 cost1 cent each; remaining units cost3 cents each. Return contract_id,eligible_units,usage_charge_cents for every contract. Sort contract_id.

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 each contract sum only entitled usage. First100 units cost2 cents each; next100 cost1 cent each; remaining units cost3 cents each. Return contract_id,eligible_units,usage_charge_cents for every contract. Sort contract_id.
  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 contract_id, eligible_units, usage_charge_cents. Sort order follows the task. Output has 6 rows; an empty CSV field means NULL.

Source data

contracts

6 source rows · Download contracts CSV

id INTEGERstart_day INTEGERend_day INTEGERseats INTEGERdaily_cents INTEGERtrial_end INTEGERcancel_day INTEGER
103021003NULL
25201150515
3104038012NULL
4010120008
52030212025NULL
60601500NULL

usage

9 source rows · Download usage CSV

id INTEGERcontract_id INTEGERday INTEGERunits INTEGER
11340
21480
31820
42650
521610
6315250
7430
852780
9610120

bills

5 source rows · Download bills CSV

id INTEGERcontract_id INTEGERstart_day INTEGERend_day INTEGERcents INTEGER
113101400
2112303600
325151500
44081600
560301500
Field dictionary and schema
contracts:
  id INTEGER — Stable source record identifier; not a row position.
  start_day INTEGER — Day index or ISO date as declared in this task; preserve its source representation.
  end_day INTEGER — Day index or ISO date as declared in this task; preserve its source representation.
  seats INTEGER — Number of paid service seats.
  daily_cents INTEGER — Contracted cents per seat per day.
  trial_end INTEGER — First day of paid service, after the free trial.
  cancel_day INTEGER — Exclusive service end day if cancelled; NULL means no recorded cancellation.

usage:
  id INTEGER — Stable source record identifier; not a row position.
  contract_id INTEGER — Foreign business-record identifier; match to the named source key.
  day INTEGER — Integer day index, or ISO YYYY-MM-DD date when stored as text; the task declares cutoff semantics.
  units INTEGER — Physical unit quantity, not a monetary amount.

bills:
  id INTEGER — Stable source record identifier; not a row position.
  contract_id INTEGER — Foreign business-record identifier; match to the named source key.
  start_day INTEGER — Day index or ISO date as declared in this task; preserve its source representation.
  end_day INTEGER — Day index or ISO date as declared in this task; preserve its source representation.
  cents INTEGER — Signed integer currency cents.

Download complete SQLite setup

A method you can reuse

  1. For each contract sum only entitled usage. First100 units cost2 cents each; next100 cost1 cent each; remaining units cost3 cents each. Return contract_id,eligible_units,usage_charge_cents for every contract. Sort contract_id.
  2. For the first output record, contract_id = 1; eligible_units = 140; usage_charge_cents = 240.
  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

Applying the highest reached unit rate to all usage is volume pricing, not a graduated band charge.

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 u AS (SELECT c.id,COALESCE(SUM(CASE WHEN u.day>=MAX(c.start_day,c.trial_end,0) AND u.day<MIN(c.end_day,COALESCE(c.cancel_day,30),30) THEN u.units ELSE 0 END),0) n FROM contracts c LEFT JOIN usage u ON u.contract_id=c.id GROUP BY c.id) SELECT id contract_id,n eligible_units,MIN(n,100)*2+MIN(MAX(n-100,0),100)+MAX(n-200,0)*3 usage_charge_cents FROM u ORDER BY id;
"contract_id","eligible_units","usage_charge_cents"
"1","140","240"
"2","50","100"
"3","250","450"
"4","0","0"
"5","80","160"
"6","120","220"
Reference SQL · Complete expected CSV · Empty answer CSV

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