Identifying Users with Purchase Frequency Spikes
SQL coding challenge · Difficulty: hard · Topic: Subquery vs CTE Patterns · +200 XP
Table: purchases
+---------------+---------+
| Column | Type |
+---------------+---------+
| purchase_id | INT | | user_id | INT | | purchase_date | DATE |
+---------------+---------+
Problem
-------
Find users whose purchase count in their
MOST RECENT month is AT LEAST DOUBLE the
average count of their previous 2 months.
Only consider users with purchases in at
least 3 distinct months.
Return: user_id, increase_period (YYYY-MM)
Order: user_id ASC
Step-by-Step Approach
---------------------
1. Group purchases by user_id + YYYY-MM
2. Rank months per user (latest = rank 1)
3. Keep only top 3 months per user
4. Compare: rank=1 count >= 2 × avg(rank 2,3)
Example Input
-------------
+----+---------+---------------+
| id | user_id | purchase_date |
+----+---------+---------------+
| 1 | 101 | 2024-01-05 | | 2 | 101 | 2024-01-10 |← Jan: 2 | 3 | 101 | 2024-02-10 |← Feb: 2 | 4 | 101 | 2024-02-22 | | 5 | 101 | 2024-03-01 | | 6 | 101 | 2024-03-08 |← Mar: 6 | 7 | 101 | 2024-03-12 | | 8 | 101 | 2024-03-19 | | 9 | 101 | 2024-03-24 | | 10 | 101 | 2024-03-28 |
+----+---------+---------------+
Calculation for user 101:
Latest (Mar): 6 purchases
Prev 2 avg: (2 + 2) / 2 = 2.0
6 >= 2 × 2.0? YES → SPIKE!
Expected Output
+---------+-----------------+
| user_id | increase_period |
+---------+-----------------+
| 101 | 2024-03 |
+---------+-----------------+
What this SQL challenge teaches you
“Identifying Users with Purchase Frequency Spikes” is a hard-level SQL challenge focused on Subquery vs CTE Patterns. Working through it gives you hands-on practice with Common Table Expressions, Subqueries, Analytics — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.
Concepts covered
- Common Table Expressions
- Subqueries
- Analytics
How to approach it
If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:
- Your result should return: user_id, increase_period (YYYY-MM).
- Compare your output to the Expected Output — the columns, values and row order must match exactly.
- WITH monthly AS ( SELECT user_id, DATE_FORMAT(purchase_date,'%Y-%m') AS period, COUNT(*) AS cnt FROM purchases GROUP BY user_id, period )
Where this comes up
Variations of this problem have been reported in interviews at Amazon, Flipkart, Razorpay. Interviewers use it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.
How to practise it on PySpark.in
Open the challenge, write your SQL query in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.
Related SQL challenges
- Top 3 Products per Category
- Running Total Revenue
- Find Duplicate Emails
- Median Salary per Department
- Second Highest Salary
- Latest Order Per Customer
Frequently asked questions
Do I need to install Spark or a database to solve this?
No. The SQL environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.
Is this challenge free?
Yes - the problem, the sample dataset, the hints and unlimited test runs are free.
What level is it?
It is rated hard and covers Subquery vs CTE Patterns.