Top 3 orders per customer
SQL
Medium
3 views
Problem Description
Return top 3 orders by total_amount for each customer.
Sample Test Case
Input:
Orders(customer_id, total_amount)
Output:
Up to 3 per customer
Constraints
Use ROW_NUMBER window
Official Solution
SELECT * FROM (SELECT order_id, customer_id, total_amount, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY total_amount DESC) AS rn FROM Orders) t WHERE rn <= 3;
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!