Monthly Cohort Retention
A mobile puzzle game groups players into cohorts by the calendar month they joined. The growth team wants to know what share of each cohort came back in the following calendar months.
A player is retained in month +1 if they have at least one play session in the calendar month right after their join month (players who joined any day in January count if they played any day in February). Month +2 is the month after that. Sessions in other months do not matter, and a player does not need month +1 to count for month +2.
Return cohort_month ('YYYY-MM'), cohort_size, month1_pct and month2_pct (percentage of the cohort retained, rounded to 1 decimal; 0.0 if nobody). One row per month in which someone joined, ordered by cohort_month. No players means no rows.
Tables
players — One row per player who installed the game and created a profile.
| Column | Type | Description |
|---|---|---|
player_id (key) | INTEGER | Unique id |
joined_on | TEXT | Join date, 'YYYY-MM-DD' |
platform | TEXT | 'ios' or 'android' |
play_sessions — One row per game session.
| Column | Type | Description |
|---|---|---|
session_id (key) | INTEGER | Unique id |
player_id | INTEGER | Player who played |
started_at | TEXT | Session start, 'YYYY-MM-DD HH:MM:SS' (never before the join date) |
Example
players
| player_id | joined_on | platform |
|---|---|---|
| 1 | 2026-01-05 | ios |
| 2 | 2026-01-20 | android |
| 3 | 2026-01-31 | ios |
| 4 | 2026-02-02 | android |
| 5 | 2026-02-14 | ios |
play_sessions
| session_id | player_id | started_at |
|---|---|---|
| 1 | 1 | 2026-01-05 19:00:00 |
| 2 | 1 | 2026-02-03 08:10:00 |
| 3 | 1 | 2026-02-17 21:00:00 |
| 4 | 2 | 2026-03-01 12:00:00 |
| 5 | 3 | 2026-02-01 07:30:00 |
| 6 | 3 | 2026-03-30 22:00:00 |
| 7 | 4 | 2026-03-15 18:00:00 |
| 8 | 4 | 2026-03-16 18:00:00 |
| 9 | 5 | 2026-05-02 09:00:00 |
Expected output
| cohort_month | cohort_size | month1_pct | month2_pct |
|---|---|---|---|
| 2026-01 | 3 | 66.7 | 66.7 |
| 2026-02 | 2 | 50 | 0 |
Return the rows in exactly the order described. Your query also runs against hidden datasets with tricky edge cases.
SQL Path · Dates & Time · problem 50 of 70 · SQLite
The idea
Cohort analysis is mostly about putting every date on the same monthly grid. date(x, 'start of month') maps a join date and a session time to the first day of their month, and SQLite's '+1 month' and '+2 months' modifiers handle year rollover, so a December cohort correctly looks at January and February.
Next, collapse the sessions into distinct (player_id, active_month) pairs. Retention is about players, not sessions, and deduplicating early keeps the counts honest.
Then LEFT JOIN players to their active months and group by cohort. The cohort size is COUNT(DISTINCT player_id) (the join can repeat a player), and each retention figure is a conditional distinct count: the players with an active month equal to the cohort month plus one or two. The LEFT JOIN keeps players who never returned, so they count in the denominator, and a cohort with no returns gets 0.0, not NULL.
The alternative computes two 0/1 flags per player with EXISTS and averages them per cohort: the average of a 0/1 flag is a proportion.
Solution
WITH cohorts AS (
SELECT player_id,
date(joined_on, 'start of month') AS cohort_start
FROM players
),
active_months AS (
SELECT DISTINCT player_id,
date(started_at, 'start of month') AS active_month
FROM play_sessions
)
SELECT strftime('%Y-%m', c.cohort_start) AS cohort_month,
COUNT(DISTINCT c.player_id) AS cohort_size,
ROUND(100.0 * COUNT(DISTINCT CASE WHEN a.active_month = date(c.cohort_start, '+1 month')
THEN c.player_id END)
/ COUNT(DISTINCT c.player_id), 1) AS month1_pct,
ROUND(100.0 * COUNT(DISTINCT CASE WHEN a.active_month = date(c.cohort_start, '+2 months')
THEN c.player_id END)
/ COUNT(DISTINCT c.player_id), 1) AS month2_pct
FROM cohorts AS c
LEFT JOIN active_months AS a ON a.player_id = c.player_id
GROUP BY c.cohort_start
ORDER BY c.cohort_start;
Another way
WITH flags AS (
SELECT p.player_id,
strftime('%Y-%m', p.joined_on) AS cohort_month,
EXISTS (SELECT 1 FROM play_sessions AS s
WHERE s.player_id = p.player_id
AND strftime('%Y-%m', s.started_at)
= strftime('%Y-%m', p.joined_on, 'start of month', '+1 month')) AS m1,
EXISTS (SELECT 1 FROM play_sessions AS s
WHERE s.player_id = p.player_id
AND strftime('%Y-%m', s.started_at)
= strftime('%Y-%m', p.joined_on, 'start of month', '+2 months')) AS m2
FROM players AS p
)
SELECT cohort_month,
COUNT(*) AS cohort_size,
ROUND(AVG(m1) * 100, 1) AS month1_pct,
ROUND(AVG(m2) * 100, 1) AS month2_pct
FROM flags
GROUP BY cohort_month
ORDER BY cohort_month;
Why common answers fail
Adds 1 to the month number, which breaks at year end (December + 1 is 13, not January) and also matches the same month in a later year.
SELECT strftime('%Y-%m', p.joined_on) AS cohort_month,
COUNT(DISTINCT p.player_id) AS cohort_size,
ROUND(100.0 * COUNT(DISTINCT CASE WHEN CAST(strftime('%m', s.started_at) AS INTEGER)
= CAST(strftime('%m', p.joined_on) AS INTEGER) + 1
THEN p.player_id END) / COUNT(DISTINCT p.player_id), 1) AS month1_pct,
ROUND(100.0 * COUNT(DISTINCT CASE WHEN CAST(strftime('%m', s.started_at) AS INTEGER)
= CAST(strftime('%m', p.joined_on) AS INTEGER) + 2
THEN p.player_id END) / COUNT(DISTINCT p.player_id), 1) AS month2_pct
FROM players AS p
LEFT JOIN play_sessions AS s ON s.player_id = p.player_id
GROUP BY cohort_month
ORDER BY cohort_month;
Counts sessions rather than distinct players, so a player with several sessions inflates retention (even above 100%), and a cohort with no sessions gets NULL.
SELECT strftime('%Y-%m', p.joined_on) AS cohort_month,
COUNT(DISTINCT p.player_id) AS cohort_size,
ROUND(100.0 * SUM(strftime('%Y-%m', s.started_at) = strftime('%Y-%m', p.joined_on, 'start of month', '+1 month'))
/ COUNT(DISTINCT p.player_id), 1) AS month1_pct,
ROUND(100.0 * SUM(strftime('%Y-%m', s.started_at) = strftime('%Y-%m', p.joined_on, 'start of month', '+2 months'))
/ COUNT(DISTINCT p.player_id), 1) AS month2_pct
FROM players AS p
LEFT JOIN play_sessions AS s ON s.player_id = p.player_id
GROUP BY cohort_month
ORDER BY cohort_month;
Uses 30-day windows after each join date instead of calendar months, which is a different (and here wrong) definition of retention.
SELECT strftime('%Y-%m', p.joined_on) AS cohort_month,
COUNT(DISTINCT p.player_id) AS cohort_size,
ROUND(100.0 * COUNT(DISTINCT CASE WHEN julianday(s.started_at) - julianday(p.joined_on) BETWEEN 30 AND 59.99999
THEN p.player_id END) / COUNT(DISTINCT p.player_id), 1) AS month1_pct,
ROUND(100.0 * COUNT(DISTINCT CASE WHEN julianday(s.started_at) - julianday(p.joined_on) BETWEEN 60 AND 89.99999
THEN p.player_id END) / COUNT(DISTINCT p.player_id), 1) AS month2_pct
FROM players AS p
LEFT JOIN play_sessions AS s ON s.player_id = p.player_id
GROUP BY cohort_month
ORDER BY cohort_month;
Integer arithmetic (100 instead of 100.0) truncates the percentage, so 2 out of 3 becomes 66 instead of 66.7.
WITH cohorts AS (
SELECT player_id, date(joined_on, 'start of month') AS cohort_start FROM players
), active_months AS (
SELECT DISTINCT player_id, date(started_at, 'start of month') AS active_month FROM play_sessions
)
SELECT strftime('%Y-%m', c.cohort_start) AS cohort_month,
COUNT(DISTINCT c.player_id) AS cohort_size,
ROUND(100 * COUNT(DISTINCT CASE WHEN a.active_month = date(c.cohort_start, '+1 month') THEN c.player_id END)
/ COUNT(DISTINCT c.player_id), 1) AS month1_pct,
ROUND(100 * COUNT(DISTINCT CASE WHEN a.active_month = date(c.cohort_start, '+2 months') THEN c.player_id END)
/ COUNT(DISTINCT c.player_id), 1) AS month2_pct
FROM cohorts AS c
LEFT JOIN active_months AS a ON a.player_id = c.player_id
GROUP BY c.cohort_start
ORDER BY c.cohort_start;
What the interviewer is checking
A staple of analytics interviews: it tests calendar arithmetic, deduplication before counting, and keeping non-returning users in the denominator.
Mistakes to avoid
- Month arithmetic on month numbers, which fails across years.
- Counting sessions instead of distinct players.
- Inner-joining sessions, which drops players who never came back from the denominator.
- Integer division in the percentage.
Follow-ups to expect
- Generalize to month +N for N = 1..6 as rows (cross join a small numbers CTE and pivot later).
- Split cohorts by platform (add platform to the GROUP BY).
- Leave a month NULL when it is still in the future relative to the latest session (compare with MAX(started_at)).
In real work
Retention triangles like this are the core of growth reporting for subscription apps and games, and investors read them closely.