Customers With Strictly Increasing Purchases - Problem

In the competitive world of e-commerce, understanding customer purchasing patterns is crucial for business growth. Your task is to identify loyal customers with consistently increasing spending habits - those golden customers who increase their yearly purchases without fail!

You're given an Orders table with the following structure:

Column NameType
order_idint
customer_idint
order_datedate
priceint

Your mission: Find customers whose total yearly purchases are strictly increasing from their first order year to their last order year.

Key Rules:

  • If a customer didn't order anything in a year within their active period, consider their spending as 0 for that year
  • The analysis period starts from the year of their first order and ends at the year of their last order
  • "Strictly increasing" means each year must have higher spending than the previous year (no equal amounts allowed)

Input & Output

basic_case.sql โ€” Basic Example
$ Input: Orders table: | order_id | customer_id | order_date | price | |----------|-------------|------------|-------| | 1 | 1 | 2019-07-01 | 1000 | | 2 | 2 | 2018-11-07 | 800 | | 3 | 3 | 2019-05-03 | 1500 | | 4 | 1 | 2019-11-02 | 1500 | | 5 | 2 | 2018-11-30 | 700 | | 6 | 3 | 2020-06-18 | 1800 | | 7 | 1 | 2020-05-25 | 2000 | | 8 | 2 | 2019-01-11 | 900 |
โ€บ Output: | customer_id | |-------------| | 1 |
๐Ÿ’ก Note: Customer 1: 2019($2500) < 2020($2000) โŒ Not strictly increasing. Customer 2: 2018($1500) < 2019($900) โŒ Decreasing. Customer 3: Only one year of data. Customer 1 has strictly increasing purchases: 2019: $2500, 2020: $2000 - Wait, this should be โŒ. Let me recalculate: Customer 1: 2019: $2500 (orders 1+4), 2020: $2000 (order 7) - Not increasing!
gap_year_case.sql โ€” Gap Year Handling
$ Input: Orders table: | order_id | customer_id | order_date | price | |----------|-------------|------------|-------| | 1 | 1 | 2018-01-01 | 1000 | | 2 | 1 | 2020-01-01 | 2000 | | 3 | 2 | 2018-01-01 | 500 | | 4 | 2 | 2019-01-01 | 1000 | | 5 | 2 | 2020-01-01 | 1500 |
โ€บ Output: | customer_id | |-------------| | 2 |
๐Ÿ’ก Note: Customer 1: 2018($1000) > 2019($0) - not increasing due to gap year with $0. Customer 2: 2018($500) < 2019($1000) < 2020($1500) - strictly increasing every year.
edge_case.sql โ€” Single Year Customers
$ Input: Orders table: | order_id | customer_id | order_date | price | |----------|-------------|------------|-------| | 1 | 1 | 2019-01-01 | 1000 | | 2 | 1 | 2019-06-01 | 500 | | 3 | 2 | 2018-01-01 | 2000 | | 4 | 3 | 2020-01-01 | 1000 | | 5 | 3 | 2021-01-01 | 2000 |
โ€บ Output: | customer_id | |-------------| | 3 |
๐Ÿ’ก Note: Customer 1: Only 2019 data - excluded (need multiple years). Customer 2: Only 2018 data - excluded. Customer 3: 2020($1000) < 2021($2000) - strictly increasing across two years.

Visualization

Tap to expand
Customer Purchase Trend AnalysisCustomer A (Valid โœ…)2019$10002020$15002021$2000Customer B (Invalid โŒ)2018$20002019: $02020$1000Processing Steps:1. AggregateGroup by customer& year, SUM(price)2. Fill GapsCreate completeyear sequences3. CompareUse LAG() to checkyear-over-year4. FilterKeep strictlyincreasing onlyKey Rules:โ€ข Gap years count as $0 spendingโ€ข Need multiple years to qualifyโ€ข Must be STRICTLY increasing (no equal amounts)โ€ข Analysis from first order year to last order yearโ€ข Each year must exceed previous year's total๐Ÿ’ก Key Insight: Use window functions for efficient single-pass validationLAG() + recursive CTE handles gaps and comparisons simultaneously
Understanding the Visualization
1
Collect Purchase Data
Gather all orders and group by customer and year, calculating yearly totals
2
Fill Timeline Gaps
For each customer, create complete year sequence from first to last order year, filling gaps with $0
3
Compare Year-over-Year
Use LAG() window function to compare each year's spending with the previous year
4
Filter Increasing Patterns
Select only customers where every year shows higher spending than the previous year
Key Takeaway
๐ŸŽฏ Key Insight: Use SQL window functions with LAG() to compare consecutive years efficiently, combined with recursive CTEs to handle missing years as $0 spending.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Single pass with sorting for window functions

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space for window function calculations and CTEs

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค orders.length โ‰ค 104
  • 1 โ‰ค customer_id โ‰ค 103
  • 1 โ‰ค price โ‰ค 106
  • order_date is a valid date between 2010-01-01 and 2023-12-31
  • All order_id values are unique
Asked in
Amazon 45 Google 38 Meta 32 Netflix 28
21.1K Views
Medium-High Frequency
~25 min Avg. Time
892 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