Product Sales Analysis IV - Problem

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
ColumnType
sale_idint (unique)
product_idint
user_idint
quantityint
Product Table
ColumnType
product_idint (unique)
priceint

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

example_1.sql โ€” Basic Case
$ Input: Sales: [(1,1,1,2), (2,2,1,1), (3,3,2,3)], Product: [(1,10), (2,25), (3,15)]
โ€บ Output: [(1,2), (2,3)]
๐Ÿ’ก Note: User 1 spent $20 on product 1 and $25 on product 2 (maximum). User 2 spent $45 on product 3 (only product). Results show each user's highest spending product.
example_2.sql โ€” Tie Case
$ Input: Sales: [(1,1,1,5), (2,2,1,2), (3,1,2,1)], Product: [(1,10), (2,25)]
โ€บ Output: [(1,1), (1,2), (2,1)]
๐Ÿ’ก Note: User 1 spent $50 on product 1 and $50 on product 2 (tied for maximum). User 2 spent $10 on product 1. Both tied products for User 1 are included in results.
example_3.sql โ€” Multiple Sales Same Product
$ Input: Sales: [(1,1,1,3), (2,1,1,2), (3,2,1,1)], Product: [(1,20), (2,30)]
โ€บ Output: [(1,1)]
๐Ÿ’ก Note: User 1 has multiple sales for product 1: total spending = (3+2)*$20 = $100 on product 1, vs $30 on product 2. Product 1 has maximum spending.

Visualization

Tap to expand
Customer ReceiptsSales + ProductPrice LookupSpending SummaryGroup by User& ProductRanking SystemRANK() bySpending DESCTop ProductsFilter Rank = 1Include TiesUser 1User 2User 3Laptop: $500Phone: $300Tablet: $400Mouse: $50Monitor: $200Keyboard: $100Result: Laptop (Rank 1)Result: Tablet (Rank 1)Result: Monitor (Rank 1)
Understanding the Visualization
1
Collect All Receipts
JOIN sales data with product prices to get complete purchase information
2
Calculate Customer Totals
Sum up how much each customer spent on each product type
3
Rank by Spending
For each customer, rank their products from highest to lowest spending
4
Extract Top Products
Select only the products with rank 1 (highest spending) for each customer
Key Takeaway
๐ŸŽฏ Key Insight: Window functions with PARTITION BY allow us to rank within groups efficiently, avoiding expensive correlated subqueries while handling ties automatically.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Single pass through data with sorting for ranking within groups

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space for intermediate results and ranking

n
2n
โšก Linearithmic Space

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
Asked in
Amazon 45 Google 38 Meta 32 Microsoft 28
67.4K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen