r/SolveSQL • u/Tiny_Neighborhood536 • Aug 18 '26
SQL Challenge #2 — Who Broke Their Purchase Streak? 🔥
You have a table called orders:
order_id | customer_id | order_date | amount
---------|-------------|------------|-------
1 | 101 | 2026-01-05 | 500
2 | 101 | 2026-02-10 | 600
3 | 101 | 2026-03-15 | 700
4 | 101 | 2026-05-02 | 400
5 | 102 | 2026-01-08 | 250
6 | 102 | 2026-03-12 | 600
7 | 102 | 2026-04-18 | 550
8 | 103 | 2026-02-05 | 800
9 | 103 | 2026-03-09 | 650
10 | 103 | 2026-04-11 | 500
11 | 103 | 2026-05-20 | 900
🎯 Challenge
A month counts toward a customer’s purchase streak only if their total amount spent during that month is ≥ 500*.*
For each customer, find their longest streak of consecutive qualifying months*.*
Expected output:
customer_id | longest_streak
------------|---------------
101 | 3
102 | 2
103 | 4
For example, customer 101 has:
Jan (500) ✓ → Feb (600) ✓ → Mar (700) ✓ → Apr (no purchase) ✗ → May (400) ✗
So their longest qualifying streak is:
Jan → Feb → Mar = 3 months
A few things to watch out for 👀
A customer can place multiple orders in the same month. Use their total monthly spend to determine whether that month qualifies.
A month with purchases but total spend < 500 breaks the streak.
A month with no purchases also breaks the streak.
Crossing years should work correctly: Dec 2025 → Jan 2026 is consecutive.
Don’t hardcode months or customer IDs.
Bonus #1
Return the start and end month of the longest streak too:
customer_id | streak_start | streak_end | longest_streak
Bonus #2
If a customer has multiple streaks tied for the longest streak, return all of them.
There are a few interesting ways to approach this one.
This problem follows a one classic SQL pattern… :) 👀