Products With Three or More Orders in Two Consecutive Years - Problem
Products With Three or More Orders in Two Consecutive Years
Imagine you're working as a data analyst for an e-commerce company, and your manager wants to identify which products have been consistently popular over time. Specifically, they want to find products that were ordered at least 3 times in two consecutive years.
You have access to an
Goal: Write a SQL query to find all
Example: If a product was ordered 3 times in 2019 and 4 times in 2020, it qualifies. If it was ordered 5 times in 2021 and 2 times in 2022, it doesn't qualify (since 2022 has only 2 orders).
Return the result in any order.
Imagine you're working as a data analyst for an e-commerce company, and your manager wants to identify which products have been consistently popular over time. Specifically, they want to find products that were ordered at least 3 times in two consecutive years.
You have access to an
Orders table with the following structure:| Column Name | Type |
|---|---|
| order_id | int |
| product_id | int |
| quantity | int |
| purchase_date | date |
Goal: Write a SQL query to find all
product_ids that were ordered 3 or more times in at least one pair of consecutive years.Example: If a product was ordered 3 times in 2019 and 4 times in 2020, it qualifies. If it was ordered 5 times in 2021 and 2 times in 2022, it doesn't qualify (since 2022 has only 2 orders).
Return the result in any order.
Input & Output
example_1.sql โ Basic Case
$
Input:
Orders table:\n| order_id | product_id | quantity | purchase_date |\n|----------|------------|----------|---------------|\n| 1 | 1 | 5 | 2019-02-17 |\n| 2 | 1 | 6 | 2019-03-01 |\n| 3 | 1 | 2 | 2019-03-20 |\n| 4 | 1 | 4 | 2020-02-17 |\n| 5 | 1 | 3 | 2020-05-12 |\n| 6 | 1 | 2 | 2020-06-13 |
โบ
Output:
| product_id |\n|------------|\n| 1 |
๐ก Note:
Product 1 was ordered 3 times in 2019 and 3 times in 2020 (consecutive years), meeting our criteria of 3+ orders in both years.
example_2.sql โ Multiple Products
$
Input:
Orders table:\n| order_id | product_id | quantity | purchase_date |\n|----------|------------|----------|---------------|\n| 1 | 1 | 1 | 2019-01-01 |\n| 2 | 1 | 1 | 2019-02-01 |\n| 3 | 1 | 1 | 2019-03-01 |\n| 4 | 1 | 1 | 2020-01-01 |\n| 5 | 1 | 1 | 2020-02-01 |\n| 6 | 1 | 1 | 2020-03-01 |\n| 7 | 2 | 1 | 2020-01-01 |\n| 8 | 2 | 1 | 2020-02-01 |\n| 9 | 2 | 1 | 2021-01-01 |\n| 10 | 2 | 1 | 2021-02-01 |\n| 11 | 2 | 1 | 2021-03-01 |
โบ
Output:
| product_id |\n|------------|\n| 1 |
๐ก Note:
Product 1 has 3 orders in 2019 and 3 orders in 2020 (consecutive years with 3+ each). Product 2 has 2 orders in 2020 and 3 orders in 2021, but doesn't qualify because 2020 has only 2 orders.
example_3.sql โ Edge Case - No Qualifying Products
$
Input:
Orders table:\n| order_id | product_id | quantity | purchase_date |\n|----------|------------|----------|---------------|\n| 1 | 1 | 1 | 2019-01-01 |\n| 2 | 1 | 1 | 2019-02-01 |\n| 3 | 1 | 1 | 2021-01-01 |\n| 4 | 1 | 1 | 2021-02-01 |\n| 5 | 1 | 1 | 2021-03-01 |
โบ
Output:
| product_id |\n|------------|\n(empty result)
๐ก Note:
Product 1 has 2 orders in 2019 and 3 orders in 2021, but these are not consecutive years. No product meets the criteria.
Constraints
- 1 โค order_id โค 106
- 1 โค product_id โค 104
- 1 โค quantity โค 100
- purchase_date is between 2017-01-01 and 2030-12-31
- All order_id values are unique
Visualization
Tap to expand
Understanding the Visualization
1
Collect Annual Data
Count how many times each product was ordered per year
2
Compare Adjacent Years
Use LAG window function to check if consecutive years both have 3+ orders
3
Identify Winners
Return products that maintained popularity across consecutive years
Key Takeaway
๐ฏ Key Insight: Window functions eliminate the need for expensive self-joins by allowing us to access previous row data within the same query, making consecutive year comparisons efficient and elegant.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code