Price at the time of the order
An SQL exercise from Querydrill. Solve it with a SQL editor in your browser, checked on the rows it returns and again on changed data, and it is checked the moment you submit.
Try it yourself
price_history holds the price of a product from each valid_from date onward. For every order line of a product that has a price on the order date, return the order id, the product id and the price that was valid on the order date. A price counts from its valid_from date itself.
orders (first 12 of 14 rows)
| id | customer_id | order_date | amount | status |
|---|---|---|---|---|
| 101 | 1 | 2025-01-03 | 120 | paid |
| 102 | 1 | 2025-01-20 | 80.5 | paid |
| 103 | 2 | 2025-01-22 | 200 | paid |
| 104 | 3 | 2025-02-02 | 45 | refunded |
| 105 | 3 | 2025-02-15 | 310 | paid |
| 106 | 4 | 2025-02-18 | 150 | pending |
| 107 | 5 | 2025-03-01 | 60 | paid |
| 108 | 6 | 2025-03-09 | 95 | paid |
| 109 | 6 | 2025-03-30 | 130 | paid |
| 110 | 6 | 2025-04-11 | 75 | paid |
| 111 | 7 | 2025-04-12 | 210 | paid |
| 112 | 1 | 2025-04-25 | 40 | paid |
order_items (first 12 of 18 rows)
| order_id | product_id | qty |
|---|---|---|
| 101 | 3 | 4 |
| 101 | 4 | 1 |
| 102 | 7 | 2 |
| 103 | 2 | 1 |
| 104 | 6 | 1 |
| 105 | 5 | 2 |
| 105 | 4 | 3 |
| 106 | 1 | 2 |
| 107 | 3 | 6 |
| 108 | 5 | 1 |
| 109 | 7 | 1 |
| 109 | 6 | 2 |
price_history (6 rows)
| product_id | price | valid_from |
|---|---|---|
| 3 | 4 | 2024-01-01 |
| 3 | 4.5 | 2025-03-01 |
| 5 | 89 | 2024-01-01 |
| 5 | 99 | 2025-02-15 |
| 5 | 95 | 2025-05-01 |
| 6 | 60 | 2024-06-01 |
Free, no account needed. PostgreSQL runs in your browser and your answer is checked again on changed data.
What this exercise teaches
This is a point-in-time join. Joining to the current price rewrites history, and joining to every earlier price gives several rows per line. LATERAL with ORDER BY valid_from DESC LIMIT 1 picks the one price that was in force on the order date.
A hint if you get stuck
For each order line, take the newest price row with valid_from on or before the order date.
Background you need
Common mistakes on this exercise
- With < an order placed on the day of a price change gets the old price.
- Returns one row for every earlier price, not only the price in force.
Each of these returns a result that looks plausible. The checker catches them because it runs your query on changed data as well.
Keep going on the practice page
Your first 8 exercises on Querydrill are free, with no account. The practice page has the full editor, timed mode and all the exercises in order. A free account shows 3 reference solutions a day; Pro unlocks every exercise, every solution, timed mode and the certificate.
Open this exerciseMore practice on the same topic
More exercises in Advanced
- Above the average salary
- Rank within department
- Running revenue total
- Running total per customer
- Top earner per department
- Second highest salary
- Big spenders
- Levels in the reporting chain
- Dense rank
- Change from the previous order
- Days until the next order
- Moving average
- Share of revenue
- Share of the customer's total
- Each customer's first order
- Month over month
- Top two per category
- Orders above the customer's average
- Compared to the first order
- Salary quartiles
- Cumulative share of revenue
- Latest order per customer (DISTINCT ON)
- Two largest paid orders per customer (LATERAL)
- Most orders, ties included
- Last event per user, with a tie-break
- Year-over-year growth
- Read fields out of JSON
- One row per array element