1549. The Most Recent Orders for Each Product

https://leetcode.com/problems/the-most-recent-orders-for-each-product

Description

Table: Customers

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| customer\_id   | int     |
| name          | varchar |
+---------------+---------+
customer\_id is the primary key for this table.
This table contains information about the customers.

Table: Orders

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| order\_id      | int     |
| order\_date    | date    |
| customer\_id   | int     |
| product\_id    | int     |
+---------------+---------+
order\_id is the primary key for this table.
This table contains information about the orders made by customer\_id.
There will be no product ordered by the same user **more than once** in one day.

Table: Products

Write an SQL query to find the most recent order(s) of each product.

Return the result table sorted by product_name in ascending order and in case of a tie by the product_id in ascending order. If there still a tie, order them by the order_id in ascending order.

The query result format is in the following example:

ac

Last updated

Was this helpful?