← SQL workplace practice

Explainable business reporting · Original fictional data · Free files and full answers

Calculate contribution margin after physical returns

Per period/sku assume returned units refund both revenue and cost at the supplied per-unit amounts. Return period,sku,net_units,net_revenue,net_cost,margin_cents sorted period,sku.

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. Per period/sku assume returned units refund both revenue and cost at the supplied per-unit amounts. Return period,sku,net_units,net_revenue,net_cost,margin_cents sorted period,sku.
  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 period, sku, net_units, net_revenue, net_cost, margin_cents. Sort order follows the task. Output has 7 rows; an empty CSV field means NULL.

Source data

sales

7 source rows · Download sales CSV

id INTEGERsku TEXTchannel TEXTperiod INTEGERqty INTEGERprice INTEGERcost INTEGERreturned INTEGER
1Asearch110100601
2Bemail152001500
3Csocial143002001
4Asearch212120652
5Bemail241901501
6Csearch262802100
7Dsocial235004500

touches

6 source rows · Download touches CSV

customer TEXTminute INTEGERchannel TEXT
U10search
U10email
U15social
U21social
U23search
U32email

funnel

10 source rows · Download funnel CSV

user TEXTminute INTEGERstep TEXT
U10visit
U12cart
U15buy
U20buy
U22visit
U24cart
U31visit
U31buy
U41visit
U42cart
Field dictionary and schema
sales:
  id INTEGER — Stable source record identifier; not a row position.
  sku TEXT — Literal business key or category label. Match exactly and retain NULL where present.
  channel TEXT — Literal business key or category label. Match exactly and retain NULL where present.
  period INTEGER — Reporting period label1 or2, not an elapsed-time measurement.
  qty INTEGER — Physical quantity; multiply through assembly paths when the task requires it.
  price INTEGER — Selling price in integer cents per physical unit.
  cost INTEGER — Acquisition cost in integer cents per physical unit.
  returned INTEGER — Number of physically returned units.

touches:
  customer TEXT — Literal business key or category label. Match exactly and retain NULL where present.
  minute INTEGER — Elapsed UTC minute from the fictional scenario origin.
  channel TEXT — Literal business key or category label. Match exactly and retain NULL where present.

funnel:
  user TEXT — Literal business key or category label. Match exactly and retain NULL where present.
  minute INTEGER — Elapsed UTC minute from the fictional scenario origin.
  step TEXT — Literal business key or category label. Match exactly and retain NULL where present.

Download complete SQLite setup

A method you can reuse

  1. Per period/sku assume returned units refund both revenue and cost at the supplied per-unit amounts. Return period,sku,net_units,net_revenue,net_cost,margin_cents sorted period,sku.
  2. For the first output record, period = 1; sku = A; net_units = 9; net_revenue = 900; net_cost = 540; margin_cents = 360.
  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

Subtracting refunds from revenue while keeping cost of returned units unchanged implements a different inventory-return assumption.

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
SELECT period,sku,SUM(qty-returned) net_units,SUM((qty-returned)*price) net_revenue,SUM((qty-returned)*cost) net_cost,SUM((qty-returned)*(price-cost)) margin_cents FROM sales GROUP BY period,sku ORDER BY period,sku;
"period","sku","net_units","net_revenue","net_cost","margin_cents"
"1","A","9","900","540","360"
"1","B","5","1000","750","250"
"1","C","3","900","600","300"
"2","A","10","1200","650","550"
"2","B","3","570","450","120"
"2","C","6","1680","1260","420"
"2","D","3","1500","1350","150"
Reference SQL · Complete expected CSV · Empty answer CSV

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