← SQL and spreadsheet practice

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

  1. Effective start dates are inclusive and unique per SKU. A price remains valid until a later start.
  2. If no price existed on the order date, return NULL for unit_cents and line_cents. Keep the order.
  3. 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 TEXTsku TEXTordered_on TEXTunits INTEGER
H1MUG2026-10-152
H2MUG2026-11-013
H3BAG2026-10-011
H4BAG2026-11-104
H5PEN2026-11-105

prices

4 source rows · Download prices CSV

sku TEXTeffective_from TEXTunit_cents INTEGER
MUG2026-10-01900
MUG2026-11-011000
MUG2026-12-011200
BAG2026-10-052500

Plan the transformation

  1. Match a price only when its start date is not after the order date.
  2. Select the greatest eligible start per order, rather than the greatest price value.
  3. 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_idunit_centsline_cents
H19001800
H210003000
H3NULLNULL
H4250010000
H5NULLNULL

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