Skip to content

Commit afe5c82

Browse files
authored
Create the-most-recent-orders-for-each-product.sql
1 parent d25d0f2 commit afe5c82

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Time: O(nlogn)
2+
# Space: O(n)
3+
4+
SELECT p.product_name,
5+
p.product_id,
6+
o.order_id,
7+
o.order_date
8+
FROM Products p
9+
INNER JOIN
10+
(SELECT product_id,
11+
MAX(order_date) AS first_order_date
12+
FROM Orders
13+
GROUP BY product_id) tmp
14+
ON tmp.product_id = p.product_id
15+
INNER JOIN Orders o
16+
ON p.product_id = o.product_id AND tmp.first_order_date = o.order_date
17+
ORDER BY p.product_name,
18+
p.product_id,
19+
o.order_id;

0 commit comments

Comments
 (0)