Build 30-day retention cohort table
IntuitData Scientist · Technical ScreenLast updated
This problem tests your ability to aggregate user activity data to calculate 30-day retention rates, requiring joins, date arithmetic, and windowing functions.
Retention Cohort Analysis
You are given two tables representing user activity on an Intuit financial application:
userstable:user_id,signup_date(Date)activitiestable:user_id,activity_date(Date)
Define the '30-day retention' for a daily cohort (users who signed up on a specific date) as the percentage of users who performed at least one activity exactly 30 days after their signup_date.
Write a SQL query to generate a table showing:
signup_datecohort_size(total users who signed up that day)retained_users(users who performed an activity exactly 30 days later)retention_rate(percentage of retained users)
Assumptions
- An activity on day 30 is defined as any record in the
activitiestable whereactivity_date = signup_date + INTERVAL '30 days'. - A user is only counted once per signup date cohort.
Example Input
users table:
| user_id | signup_date |
|---|---|
| 1 | 2023-01-01 |
| 2 | 2023-01-01 |
activities table:
| user_id | activity_date |
|---|---|
| 1 | 2023-01-31 |
Expected Output
| signup_date | cohort_size | retained_users | retention_rate |
|---|---|---|---|
| 2023-01-01 | 2 | 1 | 0.50 |
Constraints
- Ensure you handle cases where a cohort might have zero retained users.
- Assume
signup_dateis the primary partitioning key.
Hints
Hint 1
First, create a daily cohort count by grouping the users table by signup_date.
Hint 2
Perform a LEFT JOIN between the users table and the activities table using both user_id and a date filter condition (activity_date = signup_date + 30).
Hint 3
Calculate the retention rate by dividing the count of matched activities by the total count of users in that cohort, ensuring you handle potential division by zero.
Follow-up questions
- How would you modify this query to calculate 'rolling' retention, where we look for activity on ANY day within the 30-day window rather than exactly on day 30?
- If the dataset grows to billions of rows, what indexing strategy would you propose to keep this query performant?
- How would you account for users who have signed up in the last 29 days, as they haven't had the chance to be 'retained' yet?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.