Shopping Spree Streak
Walmart ☯️ Medium SQL
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:

Input:

purchases
amountpurchase_datepurchase_iduser_id
45.992023-01-0511
32.52023-01-0621
18.752023-01-0731
67.22023-01-1042
54.32023-01-1252
89.992023-01-1462
23.42023-01-0173
71.82023-01-0283
15.62023-01-0393
42.252023-01-04103

Output:
user_id
1
3
Quick Solution

Code Environment

Sign in or try as guest to run your code.

Sign In

Track

Question Difficulty Company Access
Need more practice in this area? Explore more questions →