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 NameType
product_idint (Primary Key)
product_namevarchar
unit_priceint

The Sales table records every sale transaction:

Column NameType
seller_idint
product_idint (Foreign Key)
buyer_idint
sale_datedate
quantityint
priceint

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
๐Ÿ•ต๏ธ Detective's EvidenceQ1 2019 Salesโ€ข Product A: Jan 15โ€ข Product B: Feb 20, Mar 10๐Ÿ” Alibi CheckNon-Q1 Salesโ€ข Product A: โŒ Noneโ€ข Product C: May 5โš–๏ธ Final VerdictQ1-Only Productsโ€ข Product A โœ…โ€ข Product B โœ…SQL Investigation QuerySELECT p.product_id, p.product_name FROM Product pJOIN Sales s ON p.product_id = s.product_idWHERE s.sale_date BETWEEN '2019-01-01' AND '2019-03-31'๐ŸŽฏ Key Insight: Use NOT EXISTS to eliminate products with sales outside Q1AND NOT EXISTS (SELECT 1 FROM Sales s2 WHERE s2.product_id = p.product_id AND s2.sale_date NOT BETWEEN '2019-01-01' AND '2019-03-31')
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.
Asked in
Amazon 45 Microsoft 38 Google 32 Meta 28
87.5K Views
High Frequency
~15 min Avg. Time
1.9K 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