Skip to content

Commit c72315a

Browse files
authored
Create 1596-the-most-frequently-ordered-products-for-each-customer.sql
1 parent 0f56698 commit c72315a

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Write your MySQL query statement below
2+
SELECT customer_id, product_id, product_name
3+
FROM (
4+
SELECT O.customer_id, O.product_id, P.product_name,
5+
RANK() OVER (PARTITION BY customer_id ORDER BY COUNT(O.product_id) DESC) AS rnk
6+
FROM Orders O
7+
JOIN Products P
8+
ON O.product_id = P.product_id
9+
GROUP BY customer_id, product_id
10+
) temp
11+
WHERE rnk = 1
12+
ORDER BY customer_id, product_id;

0 commit comments

Comments
 (0)