Sales Analysis III - Problem
You are a data analyst at a retail company working with two important tables: Product and Sales. Your task is to identify products with a very specific sales pattern.
The Product table contains information about each product:
| Column Name | Type |
|---|---|
| product_id | int (Primary Key) |
| product_name | varchar |
| unit_price | int |
The Sales table records every sale transaction:
| Column Name | Type |
|---|---|
| seller_id | int |
| product_id | int (Foreign Key) |
| buyer_id | int |
| sale_date | date |
| quantity | int |
| price | int |
Your Mission: Find all products that were sold exclusively during the first quarter of 2019 (January 1 to March 31, 2019). This means:
- โ The product must have at least one sale in Q1 2019
- โ The product must NOT have any sales outside Q1 2019 (before Jan 1, 2019 or after March 31, 2019)
Return the product_id and product_name of qualifying products in any order.
Input & Output
example_1.sql โ Basic Case
$
Input:
Product table:
| product_id | product_name | unit_price |
|------------|-------------|------------|
| 1 | S8 | 1000 |
| 2 | G4 | 800 |
| 3 | iPhone | 1400 |
Sales table:
| 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 | 3 | 3 | 2019-06-02 | 1 | 1400 |
| 3 | 3 | 4 | 2019-05-13 | 2 | 2800 |
โบ
Output:
| product_id | product_name |
|------------|-------------|
| 1 | S8 |
| 2 | G4 |
๐ก Note:
Product S8 (ID: 1) was only sold on 2019-01-21 (within Q1 2019). Product G4 (ID: 2) was only sold on 2019-02-17 (within Q1 2019). Product iPhone (ID: 3) was sold in May and June 2019 (outside Q1), so it's excluded.
example_2.sql โ Mixed Sales Periods
$
Input:
Product table:
| product_id | product_name | unit_price |
|------------|-------------|------------|
| 1 | Laptop | 1200 |
| 2 | Mouse | 50 |
| 3 | Keyboard | 100 |
Sales table:
| seller_id | product_id | buyer_id | sale_date | quantity | price |
|-----------|------------|----------|------------|----------|-------|
| 1 | 1 | 1 | 2019-01-15 | 1 | 1200 |
| 1 | 1 | 2 | 2019-04-10 | 1 | 1200 |
| 2 | 2 | 3 | 2019-02-28 | 5 | 250 |
| 3 | 3 | 4 | 2018-12-25 | 2 | 200 |
โบ
Output:
| product_id | product_name |
|------------|-------------|
| 2 | Mouse |
๐ก Note:
Only Mouse (ID: 2) qualifies because it was sold exclusively in Q1 2019 (2019-02-28). Laptop was sold in both Q1 and Q2 2019, so it's excluded. Keyboard was sold in December 2018 (before Q1 2019), so it's excluded.
example_3.sql โ Edge Case - No Q1 Sales
$
Input:
Product table:
| product_id | product_name | unit_price |
|------------|-------------|------------|
| 1 | Tablet | 600 |
| 2 | Charger | 30 |
Sales table:
| seller_id | product_id | buyer_id | sale_date | quantity | price |
|-----------|------------|----------|------------|----------|-------|
| 1 | 1 | 1 | 2018-11-15 | 1 | 600 |
| 1 | 1 | 2 | 2019-05-20 | 1 | 600 |
| 2 | 2 | 3 | 2019-07-10 | 3 | 90 |
โบ
Output:
| product_id | product_name |
|------------|-------------||
(empty result)
๐ก Note:
No products qualify because none were sold exclusively in Q1 2019. Tablet was sold in November 2018 and May 2019. Charger was sold only in July 2019. Both are outside Q1 2019 range.
Constraints
- 1 โค Product table rows โค 1000
- 1 โค Sales table rows โค 104
- All product_id values in Sales table exist in Product table
- sale_date is in YYYY-MM-DD format
- Q1 2019 range is 2019-01-01 to 2019-03-31 (inclusive)
Visualization
Tap to expand
Understanding the Visualization
1
Collect Q1 Evidence
Gather all products that have sales receipts from Q1 2019 (Jan-Mar)
2
Check for Alibis
For each suspect product, verify it has NO sales outside Q1 2019
3
Build Final Case
Products with Q1 sales AND no other sales are the culprits
Key Takeaway
๐ฏ Key Insight: The optimal solution uses NOT EXISTS to ensure products have ZERO sales outside Q1 2019, making it a true "exclusive Q1" filter rather than just "includes Q1" filter.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code