Product Sales Analysis V - Problem

Imagine you're building an analytics dashboard for an e-commerce platform! ๐Ÿ“Š You have access to two crucial tables: Sales (tracking every transaction) and Product (containing pricing information).

Your mission is to calculate the total spending for each user by combining sales data with product prices. This is a common business intelligence task that helps companies understand customer purchasing behavior.

Tables:

Sales table contains:

  • sale_id: Unique transaction identifier
  • product_id: References the product bought
  • user_id: The customer who made the purchase
  • quantity: How many units were purchased

Product table contains:

  • product_id: Unique product identifier
  • price: Price per unit of the product

Goal: Calculate each user's total spending (quantity ร— price for all their purchases) and return results ordered by spending (descending), with ties broken by user_id (ascending).

Input & Output

example_1.sql โ€” Basic Sales Analysis
$ Input: Sales: [[1,101,1,2],[2,102,1,1],[3,101,2,3]] | Product: [[101,10],[102,25]]
โ€บ Output: [[1,45],[2,30]]
๐Ÿ’ก Note: User 1 spent: 2ร—10 + 1ร—25 = 45. User 2 spent: 3ร—10 = 30. Results ordered by spending DESC.
example_2.sql โ€” Tie Breaking by User ID
$ Input: Sales: [[1,101,1,3],[2,102,2,1],[3,101,3,3]] | Product: [[101,10],[102,30]]
โ€บ Output: [[1,30],[3,30],[2,30]]
๐Ÿ’ก Note: All users spent 30. Tie-breaking by user_id ASC: user 1, then 3, then 2 (if user 2 also had 30).
example_3.sql โ€” Single User Multiple Products
$ Input: Sales: [[1,101,1,1],[2,102,1,2],[3,103,1,1]] | Product: [[101,15],[102,20],[103,5]]
โ€บ Output: [[1,60]]
๐Ÿ’ก Note: User 1 bought multiple products: 1ร—15 + 2ร—20 + 1ร—5 = 60 total spending.

Visualization

Tap to expand
๐Ÿ“Š Sales DataUser 1: Product A ร— 2User 1: Product B ร— 1User 2: Product A ร— 3User 2: Product C ร— 1๐Ÿ’ฐ PricesProduct A: $10Product B: $25Product C: $15JOIN๐Ÿ”„ ProcessingUser 1: (2ร—$10) + (1ร—$25) = $45User 2: (3ร—$10) + (1ร—$15) = $45GROUP BY user_id, SUM spending๐Ÿ† Final ResultsRank | User | Spending1st | User 1 | $452nd | User 2 | $45Tie broken by user_id ASC๐Ÿ’ก Key InsightJOIN tables to getcomplete information,then GROUP BY toaggregate results
Understanding the Visualization
1
Data Collection
Sales transactions are recorded with product IDs and quantities
2
Price Lookup
JOIN with Product table to get current prices
3
Calculate Spending
Multiply quantity ร— price for each transaction
4
User Aggregation
GROUP BY user_id to sum total spending per customer
5
Ranking
ORDER BY spending DESC, user_id ASC for final results
Key Takeaway
๐ŸŽฏ Key Insight: SQL JOINs allow us to combine related data from multiple tables, while GROUP BY aggregates information per category, making complex business analytics queries both efficient and readable.

Time & Space Complexity

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

JOIN operation is O(nร—m) where n,m are table sizes, plus O(k log k) for sorting where k is number of users

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

Space for k unique users in the result set

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค sales.length โ‰ค 105
  • 1 โ‰ค products.length โ‰ค 104
  • 1 โ‰ค sale_id, product_id, user_id โ‰ค 108
  • 1 โ‰ค quantity โ‰ค 103
  • 1 โ‰ค price โ‰ค 106
  • All product_ids in Sales table exist in Product table
Asked in
Amazon 85 Google 72 Meta 68 Microsoft 54 Apple 41
89.2K Views
High Frequency
~15 min Avg. Time
2.3K 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