Join Order Lines to the List Price Effective at Order Time

Last updated: November 29, 2026

Quick Overview

Given order_lines (order_id, asin, order_ts, quantity) and a type 2 slowly changing price_history table (asin, list_price, valid_from, valid_to, with valid_to NULL for the current row), attach to each order line the list price that was in effect when the order was placed.

Amazon
Data Manipulation (SQL/Python)
Data Engineer
Amazon
November 29, 2026
Data Engineer
Technical Screen
Data Manipulation (SQL/Python)
Medium

0

0

0 solved


Given order_lines (order_id, asin, order_ts, quantity) and a type 2 slowly changing price_history table (asin, list_price, valid_from, valid_to, with valid_to NULL for the current row), attach to each order line the list price that was in effect when the order was placed.

Amazon's data lake keeps history tables for prices, seller attributes and product hierarchies, and analysts constantly need the value as of an event time rather than today's value. This screen checks range joins on SCD tables, half-open validity intervals and handling of the open-ended current row.

What the Interviewer Expects
  • Join on the key plus a range condition on valid_from and valid_to
  • Use half-open intervals so a boundary timestamp matches exactly one version
  • Treat NULL valid_to as open-ended (or COALESCE to a far-future date)
  • Use LEFT JOIN and flag lines with no matching version
  • Explain why joining on the current price is wrong for historical analysis
Key Topics to Cover
slowly changing dimensions
range join
point-in-time correctness
half-open intervals
LEFT JOIN
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
  • How would you detect overlapping or gapped validity ranges in price_history?
  • How would you make this join efficient on billions of order lines?
  • How would you build price_history from a daily snapshot table?
  • What changes if order_ts is in UTC but valid_from was written in local time?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice SQL Problems
Sample Answer
Clarify the Data
  • price_history: one row per ASIN per price version, [valid_from, valid_to), current version has valid_to IS NULL. Versions for an ASIN should not overlap; I would check that separately.
  • `orde...
Approach

A plain equality join on asin returns every version and multiplies lines. The join needs the range predicate valid_from <= order_ts < valid_to, with the open-ended current row handled by `OR valid...


Submit Your Answer
Markdown supported