Monthly churn rate
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
A user is active on a day when one of their subscriptions started on or before that day and has no end date or an end date after that day. Return one row for March 2025: the number of users active on 2025-03-01, how many of them were no longer active on 2025-04-01, and that share as a percentage rounded to 1 decimal.
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
Churn is about people, not subscription rows. A user who changes plan has a subscription that ends but is still a customer. Comparing who is active at the start with who is active at the end handles that correctly.
A hint if you get stuck
Build the set of active users on each of the two dates, then LEFT JOIN the first set to the second.
Background you need
Common mistakes on this exercise
- Counts every subscription that ended in March as churn, also for a user who only changed plan or who joined and left within the month.
- Treats the end date itself as still active, so a subscription that ends on 1 April is not counted as churned.
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
- Reactivated subscribers
- Merge bookings into occupied periods
- Peak number of bookings at once