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:
โข
โข
๐ฏ 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
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
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!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code