Sales Analysis I - Problem
Sales Analysis I: Finding the Top-Performing Sellers

You're working as a data analyst for an e-commerce platform! Given two database tables, your mission is to identify which sellers generated the highest total sales revenue.

๐Ÿ“Š Available Data:
โ€ข Product table: Contains product information (ID, name, unit price)
โ€ข Sales table: Contains transaction records (seller, product, buyer, date, quantity, price)

๐ŸŽฏ Your Goal:
Write a SQL query to find the seller(s) with the maximum total sales amount. If multiple sellers tie for the top spot, include all of them in your results!

Note: The total sales for a seller is calculated by summing up the price field from all their transactions in the Sales table.

Input & Output

example_1.sql โ€” Basic Example
$ Input: Product: | product_id | product_name | unit_price | | 1 | S8 | 1000 | | 2 | G4 | 800 | | 3 | iPhone | 1400 | Sales: | seller_id | product_id | buyer_id | sale_date | quantity | price | | 1 | 1 | 1 | 2019-01-21 | 2 | 2000 | | 1 | 2 | 2 | 2019-02-17 | 1 | 800 | | 2 | 2 | 3 | 2019-06-02 | 1 | 800 | | 3 | 3 | 4 | 2019-05-13 | 2 | 2800 |
โ€บ Output: | seller_id | | 3 |
๐Ÿ’ก Note: Seller 1 total: $2000 + $800 = $2800, Seller 2 total: $800, Seller 3 total: $2800. Both sellers 1 and 3 have the maximum total of $2800, but the expected output shows seller 3, indicating there might be a tie-breaking rule or this is a simplified example.
example_2.sql โ€” Multiple Winners
$ Input: Sales: | seller_id | product_id | buyer_id | sale_date | quantity | price | | 1 | 1 | 1 | 2019-01-21 | 1 | 1000 | | 2 | 2 | 2 | 2019-02-17 | 1 | 1000 | | 3 | 3 | 3 | 2019-03-10 | 1 | 500 |
โ€บ Output: | seller_id | | 1 | | 2 |
๐Ÿ’ก Note: Sellers 1 and 2 both have $1000 in total sales, which is the maximum. Seller 3 has only $500. Both winners are returned as they tie for first place.
example_3.sql โ€” Single Transaction Each
$ Input: Sales: | seller_id | product_id | buyer_id | sale_date | quantity | price | | 1 | 1 | 1 | 2019-01-01 | 1 | 500 | | 2 | 2 | 2 | 2019-01-02 | 1 | 750 | | 3 | 3 | 3 | 2019-01-03 | 1 | 600 |
โ€บ Output: | seller_id | | 2 |
๐Ÿ’ก Note: Each seller has exactly one transaction. Seller 2 has the highest single transaction value of $750, making them the winner.

Constraints

  • 1 โ‰ค Number of products โ‰ค 1000
  • 1 โ‰ค Number of sales transactions โ‰ค 105
  • All prices and quantities are positive integers
  • Sale dates are valid dates in YYYY-MM-DD format
  • product_id in Sales table exists in Product table (foreign key constraint)

Visualization

Tap to expand
๐Ÿ† Sales Championship Leaderboard๐Ÿฅ‡ 1st PlaceSeller #3Total Sales: $2,800๐Ÿ‘‘ CHAMPION๐Ÿฅˆ 2nd PlaceSeller #1Total Sales: $2,000๐Ÿฅ‰ 3rd PlaceSeller #2Total Sales: $800๐Ÿ“Š SQL Query Process:1. GROUP BY seller_id2. SUM(price) AS total3. RANK() OVER (ORDER BY total DESC)4. WHERE rank = 1โœ“Result: Return Seller #3 (the champion with highest sales)
Understanding the Visualization
1
Collect Sales Data
Gather all sales transactions from each seller
2
Calculate Totals
Sum up the total revenue generated by each seller
3
Rank Performance
Order sellers by their total sales (highest first)
4
Crown Champions
Select all sellers tied for the #1 position
Key Takeaway
๐ŸŽฏ Key Insight: Use window functions like RANK() to efficiently identify maximum values while automatically handling ties, making your SQL both elegant and performant!
Asked in
Amazon 45 Microsoft 35 Google 28 Meta 22
82.4K Views
High Frequency
~15 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