Day-1 retention
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 retained on day 1 when they have an event on the calendar day right after their first active day. Every user counts in the total, also a user whose first day is the last day in the data. Return one row: the number of users, the number retained on day 1, and the retention rate as a percentage rounded to 1 decimal.
events (first 12 of 38 rows)
| id | user_id | event | event_time |
|---|---|---|---|
| 1 | 1 | visit | 2025-03-01 09:00:00 |
| 2 | 1 | signup | 2025-03-01 09:05:00 |
| 3 | 1 | purchase | 2025-03-01 09:20:00 |
| 4 | 2 | visit | 2025-03-01 10:00:00 |
| 5 | 1 | visit | 2025-03-01 15:00:00 |
| 6 | 1 | visit | 2025-03-02 10:00:00 |
| 7 | 3 | visit | 2025-03-02 12:00:00 |
| 8 | 3 | visit | 2025-03-02 12:40:00 |
| 9 | 1 | visit | 2025-03-03 08:00:00 |
| 10 | 1 | purchase | 2025-03-03 08:10:00 |
| 11 | 3 | signup | 2025-03-03 09:00:00 |
| 12 | 2 | visit | 2025-03-03 11:00:00 |
Free, no account needed. PostgreSQL runs in your browser and your answer is checked again on changed data.
What this exercise teaches
Day-1 retention looks at exactly one day: the day after the first. The LEFT JOIN keeps users who did not come back, so they still count in the total, and counting the joined column counts only the ones who did. In a real report you would leave out users whose first day is the last day of the data, because they have not had the chance to return yet.
A hint if you get stuck
First active day per user, then a LEFT JOIN to the activity on first day + 1.
Background you need
Common mistakes on this exercise
- Counts a return on any later day, not only on the day after the first.
- Uses the 24 hours after the first event, so a second event on the first day already counts as retention.
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
- Retention curve
- Sessions per user (30-minute rule)
- Rolling 7-day active users
- Longest streak of active days
- Monthly churn rate
- Reactivated subscribers
- Merge bookings into occupied periods
- Peak number of bookings at once