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.

ColumnTypeDescription
player_id (key)INTEGERUnique id
joined_onTEXTJoin date, 'YYYY-MM-DD'
platformTEXT'ios' or 'android'

play_sessions — One row per game session.

ColumnTypeDescription
session_id (key)INTEGERUnique id
player_idINTEGERPlayer who played
started_atTEXTSession start, 'YYYY-MM-DD HH:MM:SS' (never before the join date)

Example

players

player_idjoined_onplatform
12026-01-05ios
22026-01-20android
32026-01-31ios
42026-02-02android
52026-02-14ios

play_sessions

session_idplayer_idstarted_at
112026-01-05 19:00:00
212026-02-03 08:10:00
312026-02-17 21:00:00
422026-03-01 12:00:00
532026-02-01 07:30:00
632026-03-30 22:00:00
742026-03-15 18:00:00
842026-03-16 18:00:00
952026-05-02 09:00:00

Expected output

cohort_monthcohort_sizemonth1_pctmonth2_pct
2026-01366.766.7
2026-022500

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

Normalize both dates to the first day of their month with date(x, 'start of month'). Then month +1 is just date(cohort_start, '+1 month').
Reduce sessions to distinct (player, active month) pairs first, so a player who played ten times still counts once.
LEFT JOIN the players to those pairs and use COUNT(DISTINCT CASE WHEN ... THEN player_id END) for each retention month.
Divide by the cohort size using 100.0 so the percentage is not truncated.

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.

Practice next

Next Post Previous Post
No Comment
Add Comment
comment url