← SQL and spreadsheet practice

SQL inventory · Original fictional case · Free files and answers

Calculate stock available after reservations

A store needs an availability file for tomorrow morning. Combine on-hand stock, active reservations and eligible inbound units without multiplying rows.

Suggested time: 15–25 minutes. All records, companies, rates and work policies are invented for this exercise.

Your output contract

  1. Include inbound ETA on or before 2026-11-12 and active reservations only.
  2. Aggregate reservations and inbound independently per SKU; calculate on-hand + eligible inbound - reserved.
  3. Keep negative availability as a shortage signal. Return sku,available_units sorted by sku.

Required columns: sku, available_units. Row order is part of the contract. Empty output CSV fields represent NULL; zero remains 0.

Original source tables

onhand

4 source rows · Download onhand CSV

sku TEXTunits INTEGER
INK15
PAPER40
TAPE3
BOX0

reservations

4 source rows · Download reservations CSV

reservation_id TEXTsku TEXTunits INTEGERstatus TEXT
Q1INK5active
Q2INK3active
Q3PAPER10cancelled
Q4TAPE6active

inbound

4 source rows · Download inbound CSV

shipment_id TEXTsku TEXTunits INTEGEReta TEXT
I1INK42026-11-12
I2INK92026-11-13
I3PAPER202026-11-12
I4BOX102026-11-11

Plan the transformation

  1. Calculate INK reservations as 8 and eligible inbound as 4.
  2. Aggregate each many-side table before joining the SKU master.
  3. Keep TAPE at -3 so the replenishment team can see the oversell.

Watch for this mistake

Clamping shortages to zero hides the magnitude of the reservation problem.

Run and check your SQLite result

The database runs locally in a worker. Use one SELECT or WITH query. Your query is saved on this device when storage is available. Download your SQL before clearing browser data.

Or work in Excel or another spreadsheet, then upload the final CSV with exactly the required columns.

Worked SQL and complete expected output

Total availability is 78 units. The negative TAPE row must remain.

WITH r AS (SELECT sku,SUM(units) n FROM reservations WHERE status='active' GROUP BY sku), i AS (SELECT sku,SUM(units) n FROM inbound WHERE eta<='2026-11-12' GROUP BY sku) SELECT o.sku,o.units+COALESCE(i.n,0)-COALESCE(r.n,0) available_units FROM onhand o LEFT JOIN r USING(sku) LEFT JOIN i USING(sku) ORDER BY o.sku;
skuavailable_units
BOX10
INK11
PAPER60
TAPE-3

Download reference SQL · Download complete expected CSV · Download an empty answer CSV · Download SQLite setup

What to pass to a colleague

Keep the checked output and SQL or spreadsheet result. Explain the exception policy, the cross-check above and any held records. The checker confirms this fixed case’s output; it does not evaluate arbitrary private business data.

SQLite SELECT, joins and aggregation reference · Continue with the larger workplace projects

Try another case