Product Price at a Given Date

4pts
Wayfair

Problem Statement

Wayfair maintains a price history log for all products. Every product starts with an initial price of 10. When a price is updated, a new record is added to the products table.

Write an SQL query to find the prices of all products on 2019-08-16.

Rules:

  • If a product has no price changes on or before 2019-08-16, its price is 10.
  • If a product has multiple changes before the date, use the one with the latest change_date.
  • The result table should have columns product_id and price.
  • Results must be sorted by product_id in descending order.
Tests your understanding of
Basic SQL, Window Functions, Subquery, COALESCE and RANK

Input Tables

products
product_id(INTEGER)new_price(INTEGER)change_date(DATE)
1202019-08-14
2502019-08-14
1302019-08-15
1352019-08-16
2652019-08-17
3202019-08-18
4402019-08-10
51002019-08-01
51102019-08-20
6252019-08-16
7992019-09-01
852019-08-15

Expected Output

product_id(INTEGER)price(INTEGER)
85
710
625
5100
440
310
250
135

Tags

MediumBasic SQLWindow FunctionsSubqueryCOALESCERANK
20-25 min
48%

Hints