Shopping Spree Streak
Beginner Mode
Objective
Write a SQL query to identify users who went on a shopping spree, defined as making purchases on 3 or more consecutive days.
Table Schema:
- purchases
| Column | Type | Description |
|---|---|---|
| purchase_id | INTEGER | Unique identifier for each purchase |
| user_id | INTEGER | ID of the customer |
| purchase_date | DATE | Date of the purchase |
| amount | DECIMAL | Purchase amount |
Task Requirements:
- A shopping spree is 3 or more consecutive calendar days with at least one purchase each day
- Multiple purchases on the same day count as a single day
- A user qualifies if they have at least one such streak, regardless of how many streaks they have
- Return distinct user IDs sorted in ascending order
Output columns: user_id
Examples
Example 1:
Output:
Input:
| purchases | |||
|---|---|---|---|
| amount | purchase_date | purchase_id | user_id |
| 45.99 | 2023-01-05 | 1 | 1 |
| 32.5 | 2023-01-06 | 2 | 1 |
| 18.75 | 2023-01-07 | 3 | 1 |
| 67.2 | 2023-01-10 | 4 | 2 |
| 54.3 | 2023-01-12 | 5 | 2 |
| 89.99 | 2023-01-14 | 6 | 2 |
| 23.4 | 2023-01-01 | 7 | 3 |
| 71.8 | 2023-01-02 | 8 | 3 |
| 15.6 | 2023-01-03 | 9 | 3 |
| 42.25 | 2023-01-04 | 10 | 3 |
| user_id |
|---|
| 1 |
| 3 |
Code Environment
Sign in or try as guest to run your code.
Track
| Question | Difficulty | Company | Access |
|---|
Need more practice in this area? Explore more questions →
Walmart