Sales Analysis I - Problem
Given two tables Product and Sales, write a SQL solution to find the best seller by total sales price.
The Product table contains product information with columns:
product_id(int, primary key): Unique product identifierproduct_name(varchar): Name of the productunit_price(int): Price per unit of the product
The Sales table contains sales records with columns:
seller_id(int): Identifier of the sellerproduct_id(int, foreign key): References Product tablebuyer_id(int): Identifier of the buyersale_date(date): Date of the salequantity(int): Number of units soldprice(int): Total price for this sale
Requirements:
- Find the seller(s) with the highest total sales price
- If there is a tie, report all tied sellers
- Return the result in any order
Table Schema
Product
| Column Name | Type | Description |
|---|---|---|
product_id
PK
|
int | Primary key, unique product identifier |
product_name
|
varchar | Name of the product |
unit_price
|
int | Price per unit of the product |
Primary Key: product_id
Sales
| Column Name | Type | Description |
|---|---|---|
seller_id
|
int | Identifier of the seller |
product_id
|
int | Foreign key referencing Product table |
buyer_id
|
int | Identifier of the buyer |
sale_date
|
date | Date when the sale occurred |
quantity
|
int | Number of units sold |
price
|
int | Total price for this sale transaction |
Input & Output
Example 1 — Two sellers with equal highest sales
Input Tables:
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 has total sales of 2000 + 800 = 2800. Seller 2 has total sales of 800. Seller 3 has total sales of 2800. Sellers 1 and 3 are tied with the highest total sales of 2800, but seller 3 has the maximum individual sale, so seller 3 is returned as the best seller.
Example 2 — Clear winner with highest sales
Input Tables:
Product
| product_id | product_name | unit_price |
|---|---|---|
| 1 | Laptop | 1500 |
Sales
| seller_id | product_id | buyer_id | sale_date | quantity | price |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 2019-01-01 | 1 | 1500 |
| 2 | 1 | 2 | 2019-01-02 | 2 | 3000 |
| 1 | 1 | 3 | 2019-01-03 | 1 | 1500 |
Output:
| seller_id |
|---|
| 2 |
💡 Note:
Seller 1 has total sales of 1500 + 1500 = 3000. Seller 2 has total sales of 3000. Both sellers are tied with 3000 total sales, so both should be returned as the best sellers.
Constraints
-
1 ≤ seller_id, buyer_id, product_id ≤ 1000 -
1 ≤ quantity ≤ 100 -
1 ≤ price, unit_price ≤ 10000 -
sale_dateis in format'YYYY-MM-DD'
Visualization
Tap to expand
Understanding the Visualization
1
Input Tables
Product and Sales tables with relationships
2
GROUP BY
Aggregate total sales per seller
3
Filter Maximum
Find seller(s) with highest total sales
Key Takeaway
🎯 Key Insight: Use GROUP BY with HAVING to handle ties automatically when finding maximum aggregated values
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code