← SQL workplace practice

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

Separate earned revenue from future paid service

At start of day15, split paid contract charges inside[0,30) into earned days before15 and future days from15. Return contract_id,earned_cents,future_cents sorted 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. At start of day15, split paid contract charges inside[0,30) into earned days before15 and future days from15. Return contract_id,earned_cents,future_cents sorted 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, earned_cents, future_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. At start of day15, split paid contract charges inside[0,30) into earned days before15 and future days from15. Return contract_id,earned_cents,future_cents sorted contract_id.
  2. For the first output record, contract_id = 1; earned_cents = 2400; future_cents = 3000.
  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

Treating the full month bill as earned revenue recognizes service that has not yet occurred.

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 x AS (SELECT *,MAX(start_day,trial_end,0) s,MIN(end_day,COALESCE(cancel_day,30),30) e FROM contracts) SELECT id contract_id,MAX(0,MIN(e,15)-s)*seats*daily_cents earned_cents,MAX(0,e-MAX(s,15))*seats*daily_cents future_cents FROM x ORDER BY id;
"contract_id","earned_cents","future_cents"
"1","2400","3000"
"2","1500","0"
"3","720","3600"
"4","1600","0"
"5","0","1200"
"6","750","750"
Reference SQL · Complete expected CSV · Empty answer CSV

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