Product Sales Analysis III - Problem

Imagine you're a business analyst working for an e-commerce company. You have access to a comprehensive Sales table that records every product sale across multiple years. Your task is to identify the breakthrough moment for each product - finding all sales that occurred during the very first year each product entered the market.

The Sales Table Structure:

Column NameTypeDescription
sale_idintUnique identifier for each sale
product_idintProduct identifier
yearintYear when the sale occurred
quantityintNumber of units sold
priceintPrice per unit

Your Mission: For each product, determine its launch year (earliest year it appears in sales) and return all sales records from that inaugural year. This helps identify which products had strong initial performance and understand launch patterns.

Output Format: Return a table with columns product_id, first_year, quantity, and price for all first-year sales.

Input & Output

example_1.sql โ€” Basic Sales Data
$ Input: Sales table:\n| sale_id | product_id | year | quantity | price |\n|---------|------------|------|----------|-------|\n| 1 | 100 | 2019 | 5 | 200 |\n| 2 | 100 | 2020 | 3 | 250 |\n| 3 | 200 | 2018 | 7 | 300 |\n| 4 | 200 | 2019 | 2 | 320 |
โ€บ Output: | product_id | first_year | quantity | price |\n|------------|------------|----------|-------|\n| 100 | 2019 | 5 | 200 |\n| 200 | 2018 | 7 | 300 |
๐Ÿ’ก Note: Product 100 first appeared in 2019 with one sale (5 units at $200). Product 200 first appeared in 2018 with one sale (7 units at $300). We return all sales from each product's inaugural year.
example_2.sql โ€” Multiple First-Year Sales
$ Input: Sales table:\n| sale_id | product_id | year | quantity | price |\n|---------|------------|------|----------|-------|\n| 1 | 100 | 2019 | 5 | 200 |\n| 2 | 100 | 2019 | 3 | 180 |\n| 3 | 100 | 2020 | 8 | 250 |\n| 4 | 200 | 2020 | 4 | 150 |
โ€บ Output: | product_id | first_year | quantity | price |\n|------------|------------|----------|-------|\n| 100 | 2019 | 5 | 200 |\n| 100 | 2019 | 3 | 180 |\n| 200 | 2020 | 4 | 150 |
๐Ÿ’ก Note: Product 100 had TWO sales in its first year (2019), so both records are returned. Product 200 only has sales in 2020, which becomes its first year, so that single sale is returned.
example_3.sql โ€” Single Product Edge Case
$ Input: Sales table:\n| sale_id | product_id | year | quantity | price |\n|---------|------------|------|----------|-------|\n| 1 | 500 | 2021 | 10 | 100 |
โ€บ Output: | product_id | first_year | quantity | price |\n|------------|------------|----------|-------|\n| 500 | 2021 | 10 | 100 |
๐Ÿ’ก Note: When a product has only one sale record, that automatically becomes its first year sale. This is the simplest case where first_year equals the only year available.

Constraints

  • 1 โ‰ค sale_id โ‰ค 106
  • 1 โ‰ค product_id โ‰ค 104
  • 2000 โ‰ค year โ‰ค 2030
  • 1 โ‰ค quantity โ‰ค 1000
  • 1 โ‰ค price โ‰ค 104
  • (sale_id, year) combination is unique
  • Each product appears in at least one sales record

Visualization

Tap to expand
Product Sales Timeline - Finding First Year Performance20182019202020212022Product 100 Timeline:Launch: 2019 (First sales)Product 200 Timeline:Launch: 2018 (First sales)Product 300 Timeline:Launch: 2020 (First sales)Solution: Extract All Highlighted First-Year Salesโ€ข Product 100: All 2019 sales (launch year)โ€ข Product 200: All 2018 sales (launch year)โ€ข Product 300: All 2020 sales (launch year)๐ŸŽฏ The algorithm identifies each product's launch year, then retrievesall sales performance data from that crucial first year in market.
Understanding the Visualization
1
Identify Launch Years
For each product, find the earliest year it appears in sales records - this is the launch year
2
Create Launch Registry
Build a lookup table mapping each product to its launch year using MIN aggregation
3
Collect Launch Performance
Join back with original data to get ALL sales from each product's launch year
4
Analyze Results
The result shows complete first-year performance for every product
Key Takeaway
๐ŸŽฏ Key Insight: By using MIN aggregation with self-join, we efficiently identify each product's market entry year and capture its complete first-year performance in a single optimized database operation.
Asked in
Amazon 45 Google 38 Meta 32 Microsoft 28 Apple 22
42.0K 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