Reactivated subscribers
An SQL exercise from Querydrill. Solve it with a SQL editor in your browser, checked on the rows it returns and again on changed data, and it is checked the moment you submit.
Try it yourself
The end date is the first day without the subscription, so a new subscription that starts on the previous end date is a plan change with no gap. A reactivation is a subscription whose start date is later than the end date of the same user's previous subscription. A user's subscriptions never overlap. Return the user_id, the start date of each reactivation, and the gap in days: that start date minus the previous end date.
subscriptions (10 rows)
| id | user_id | plan | start_date | end_date |
|---|---|---|---|---|
| 1 | 1 | basic | 2025-01-10 | 2025-03-10 |
| 2 | 2 | pro | 2025-01-15 | NULL |
| 3 | 3 | basic | 2025-01-20 | 2025-02-20 |
| 4 | 4 | pro | 2025-02-01 | 2025-04-01 |
| 5 | 3 | pro | 2025-03-15 | NULL |
| 6 | 5 | basic | 2025-02-10 | 2025-03-10 |
| 7 | 6 | basic | 2025-03-01 | NULL |
| 8 | 1 | pro | 2025-03-10 | NULL |
| 9 | 7 | pro | 2025-03-20 | 2025-03-25 |
| 10 | 5 | basic | 2025-04-05 | 2025-05-05 |
Free, no account needed. PostgreSQL runs in your browser and your answer is checked again on changed data.
What this exercise teaches
LAG looks one subscription back for the same user. If that one ended before this one starts, the user was away and came back. A plan change on the same day has an end date equal to the next start date, so it is not a reactivation.
A hint if you get stuck
LAG(end_date) puts the previous subscription's end next to each row.
Background you need
Common mistakes on this exercise
- Counts a plan change on the same day as a reactivation.
- Measures the gap from the previous start date instead of the previous end date.
Each of these returns a result that looks plausible. The checker catches them because it runs your query on changed data as well.
Keep going on the practice page
Your first 8 exercises on Querydrill are free, with no account. The practice page has the full editor, timed mode and all the exercises in order. A free account shows 3 reference solutions a day; Pro unlocks every exercise, every solution, timed mode and the certificate.
Open this exerciseMore practice on the same topic
More exercises in Interview classics
- Third highest salary
- Second highest salary per department
- Earns more than the manager
- Departments above average
- Salary against the department average
- Pivot the statuses
- Repeat purchase rate
- Median salary
- Orders in consecutive months
- Runs of the same status
- Cohort retention
- Products bought together
- First order not paid
- A month with no sales still shows up (generate_series)
- Daily active users
- Monthly active users
- Funnel: users at each step
- Signup to purchase conversion
- New and returning users per day
- Day-1 retention
- Retention curve
- Sessions per user (30-minute rule)
- Rolling 7-day active users
- Longest streak of active days
- Monthly churn rate
- Merge bookings into occupied periods
- Peak number of bookings at once