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 Orders table with the following structure:

Column NameType
order_idint
product_idint
quantityint
purchase_datedate

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
Raw OrdersDaily transactionsAll productsYearly CountsAggregated byproduct & yearLAG ComparisonCompare withprevious yearResultsProducts withsustained successProduct A2019: 5 orders2020: 4 ordersโœ“ QualifiesProduct B2019: 2 orders2020: 6 ordersโœ— Doesn't qualifyProduct C2020: 3 orders2021: 3 ordersโœ“ Qualifies
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.
Asked in
Amazon 45 Google 38 Meta 32 Microsoft 28
42.4K Views
Medium Frequency
~18 min Avg. Time
1.8K 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