Interview Questions/SQL/Monthly Cohort Retention Analysis

Monthly Cohort Retention Analysis

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Description

You are given a UserActivity table recording which months each user was active. A user's cohort is defined as the first month they were ever active. Write a SQL query to compute, for each cohort month and each months_since_join offset (0, 1, 2, ...): the total number of users in that cohort (total_users), the number of those users who were still active at that offset (retained_users), and the retention percentage rounded to 1 decimal place. Return the result ordered by cohort_month, then months_since_join. Table: UserActivity

Column NameTypeDescription
user_idINTID of the user
activity_monthVARCHARMonth the user was active (YYYY-MM)

Database Schema (Inferred)

UserActivity

Column NameExample Value
user_id1
activity_month2023-01

Example

UserActivity

user_idactivity_month
12023-01
12023-02
12023-03
22023-01
22023-02
32023-01
42023-02
42023-03
52023-01
52023-03

Output

cohort_monthmonths_since_jointotal_usersretained_usersretention_pct
2023-01044100
2023-0114250
2023-0124250
2023-02011100
2023-02111100

Explanation:

January cohort: users 1,2,3,5 (4 users). At month 0, all 4 active. At month 1, users 1 and 2 were active in Feb (50%). At month 2, users 1 and 5 were active in Mar (50%). February cohort: only user 4 joined in Feb. Active at +0 and +1 (Mar).

Approach hint

Start with a simple approach, explain the trade-off, then move toward a cleaner or more scalable solution.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

SQL Editor
Loading...

UserActivity

user_idactivity_month
12023-01
12023-02
12023-03
22023-01
22023-02
32023-01
42023-02
42023-03
52023-01
52023-03

Output

cohort_monthmonths_since_jointotal_usersretained_usersretention_pct
2023-01044100
2023-0114250
2023-0124250
2023-02011100
2023-02111100