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
- Include inbound ETA on or before 2026-11-12 and active reservations only.
- Aggregate reservations and inbound independently per SKU; calculate on-hand + eligible inbound - reserved.
- 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 TEXT | units INTEGER |
|---|---|
| INK | 15 |
| PAPER | 40 |
| TAPE | 3 |
| BOX | 0 |
reservations
4 source rows · Download reservations CSV
| reservation_id TEXT | sku TEXT | units INTEGER | status TEXT |
|---|---|---|---|
| Q1 | INK | 5 | active |
| Q2 | INK | 3 | active |
| Q3 | PAPER | 10 | cancelled |
| Q4 | TAPE | 6 | active |
inbound
4 source rows · Download inbound CSV
| shipment_id TEXT | sku TEXT | units INTEGER | eta TEXT |
|---|---|---|---|
| I1 | INK | 4 | 2026-11-12 |
| I2 | INK | 9 | 2026-11-13 |
| I3 | PAPER | 20 | 2026-11-12 |
| I4 | BOX | 10 | 2026-11-11 |
Plan the transformation
- Calculate INK reservations as 8 and eligible inbound as 4.
- Aggregate each many-side table before joining the SKU master.
- 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;
| sku | available_units |
|---|---|
| BOX | 10 |
| INK | 11 |
| PAPER | 60 |
| 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