You're building a customer analytics dashboard for an e-commerce platform! Given two database tables - Sales and Product - you need to identify each customer's most valuable product purchases.
Your mission: For each user, find the product(s) they spent the most money on. If a user spent equal amounts on multiple products, include all of them in your results.
| Sales Table | |
|---|---|
| Column | Type |
| sale_id | int (unique) |
| product_id | int |
| user_id | int |
| quantity | int |
| Product Table | |
|---|---|
| Column | Type |
| product_id | int (unique) |
| price | int |
Example: If User 1 spent $100 on laptops and $100 on phones (their highest amounts), both products should appear in the results.
Goal: Write a SQL query that returns user_id and product_id pairs representing each user's top spending products.
Input & Output
Visualization
Time & Space Complexity
Single pass through data with sorting for ranking within groups
Space for intermediate results and ranking
Constraints
- 1 โค sale_id, product_id, user_id โค 1000
- 1 โค quantity โค 100
- 1 โค price โค 1000
- Each sale_id and product_id is unique in their respective tables
- At least one sale exists for each user in the result