You are given three tables: Customers, Orders, and Products. The Customers table contains customer information, the Orders table tracks all orders with dates and product details, and the Products table contains product information including names and prices.
Write a SQL query to find the most recent order(s) for each product. If multiple orders for the same product have the same most recent date, include all of them.
Return the result ordered by:
product_namein ascending order- In case of a tie, by
product_idin ascending order - If there's still a tie, by
order_idin ascending order
Table Schema
| Column Name | Type | Description |
|---|---|---|
customer_id
PK
|
int | Unique identifier for each customer |
name
|
varchar | Customer name |
| Column Name | Type | Description |
|---|---|---|
order_id
PK
|
int | Unique identifier for each order |
order_date
|
date | Date when the order was placed |
customer_id
|
int | Foreign key referencing customer |
product_id
|
int | Foreign key referencing product |
| Column Name | Type | Description |
|---|---|---|
product_id
PK
|
int | Unique identifier for each product |
product_name
|
varchar | Name of the product |
price
|
int | Price of the product |
Input & Output
| customer_id | name |
|---|---|
| 1 | Alice |
| 2 | Bob |
| 3 | Charlie |
| order_id | order_date | customer_id | product_id |
|---|---|---|---|
| 1 | 2022-01-25 | 1 | 1 |
| 2 | 2022-01-01 | 2 | 2 |
| 3 | 2022-01-25 | 3 | 1 |
| 4 | 2022-01-11 | 2 | 2 |
| 5 | 2022-01-08 | 1 | 3 |
| product_id | product_name | price |
|---|---|---|
| 1 | Bush's Best Original Baked Beans | 2 |
| 2 | Arrowhead Mills Cereal | 3 |
| 3 | Kraft Real Mayo | 5 |
| order_id | order_date | customer_id | product_id | product_name | price |
|---|---|---|---|---|---|
| 4 | 2022-01-11 | 2 | 2 | Arrowhead Mills Cereal | 3 |
| 1 | 2022-01-25 | 1 | 1 | Bush's Best Original Baked Beans | 2 |
| 5 | 2022-01-08 | 1 | 3 | Kraft Real Mayo | 5 |
For product 1 (Bush's Best Original Baked Beans), the most recent order is order_id 1 and 3 both on 2022-01-25, but order_id 1 comes first when ordered by order_id. For product 2 (Arrowhead Mills Cereal), the most recent order is order_id 4 on 2022-01-11. For product 3 (Kraft Real Mayo), there's only one order (order_id 5). Results are ordered by product_name alphabetically.
| customer_id | name |
|---|---|
| 1 | Alice |
| 2 | Bob |
| order_id | order_date | customer_id | product_id |
|---|---|---|---|
| 1 | 2022-01-25 | 1 | 1 |
| 2 | 2022-01-25 | 2 | 1 |
| 3 | 2022-01-20 | 1 | 1 |
| product_id | product_name | price |
|---|---|---|
| 1 | Laptop | 1000 |
| order_id | order_date | customer_id | product_id | product_name | price |
|---|---|---|---|---|---|
| 1 | 2022-01-25 | 1 | 1 | Laptop | 1000 |
| 2 | 2022-01-25 | 2 | 1 | Laptop | 1000 |
Product 1 (Laptop) has two orders on the most recent date (2022-01-25): order_id 1 and 2. Both are included in the result since they share the same most recent date. They are ordered by order_id (1 comes before 2).
Constraints
-
1 ≤ customer_id, order_id, product_id ≤ 1000 -
There will be no product ordered by the same user more than once in one day -
order_dateis a valid date -
product_nameand customernameconsist of English letters and spaces