SQL temporal lookup · Original fictional case · Free files and answers
Find the price effective on each order date
Sales operations must verify historical prices. Pick the last price that started on or before each order date, without leaking a future price into an older sale.
Suggested time: 15–25 minutes. All records, companies, rates and work policies are invented for this exercise.
Your output contract
- Effective start dates are inclusive and unique per SKU. A price remains valid until a later start.
- If no price existed on the order date, return NULL for unit_cents and line_cents. Keep the order.
- Return order_id,unit_cents,line_cents sorted by order_id.
Required columns: order_id, unit_cents, line_cents. Row order is part of the contract. Empty output CSV fields represent NULL; zero remains 0.
Original source tables
orders
5 source rows · Download orders CSV
| order_id TEXT | sku TEXT | ordered_on TEXT | units INTEGER |
|---|---|---|---|
| H1 | MUG | 2026-10-15 | 2 |
| H2 | MUG | 2026-11-01 | 3 |
| H3 | BAG | 2026-10-01 | 1 |
| H4 | BAG | 2026-11-10 | 4 |
| H5 | PEN | 2026-11-10 | 5 |
prices
4 source rows · Download prices CSV
| sku TEXT | effective_from TEXT | unit_cents INTEGER |
|---|---|---|
| MUG | 2026-10-01 | 900 |
| MUG | 2026-11-01 | 1000 |
| MUG | 2026-12-01 | 1200 |
| BAG | 2026-10-05 | 2500 |
Plan the transformation
- Match a price only when its start date is not after the order date.
- Select the greatest eligible start per order, rather than the greatest price value.
- Keep H3 and H5 as missing-price exceptions and never fill them with zero.
Watch for this mistake
Looking up only SKU applies today’s price to all orders. Sorting by price instead of effective date fails when a later price decreases.
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
Three priced orders total 14,800 cents. Two orders require historical-price review.
WITH x AS (SELECT o.*,p.unit_cents,ROW_NUMBER() OVER(PARTITION BY order_id ORDER BY effective_from DESC) n FROM orders o LEFT JOIN prices p ON o.sku=p.sku AND p.effective_from<=o.ordered_on) SELECT order_id,unit_cents,unit_cents*units line_cents FROM x WHERE n=1 ORDER BY order_id;
| order_id | unit_cents | line_cents |
|---|---|---|
| H1 | 900 | 1800 |
| H2 | 1000 | 3000 |
| H3 | NULL | NULL |
| H4 | 2500 | 10000 |
| H5 | NULL | NULL |
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