When you need it
"Monthly retention by signup cohort", "what share of users from January were still active in March", "how many weeks after signup do people buy", "churn". Words that signal it: "cohort", "retention", "returned", "still active", "N days after", "by signup month".
A cohort is a group defined by when something first happened. Retention asks how big that group still is at each later point.
The shape of the query
with cohort as (
select user_id, date_trunc('month', signed_up_at)::date as cohort_month
from users
),
activity as (
select distinct user_id, date_trunc('month', occurred_at)::date as active_month
from events
)
select c.cohort_month,
(extract(year from age(a.active_month, c.cohort_month)) * 12
+ extract(month from age(a.active_month, c.cohort_month)))::int as months_since,
count(distinct c.user_id) as active_users
from cohort c
join activity a on a.user_id = c.user_id and a.active_month >= c.cohort_month
group by 1, 2;Step by step:
- Assign each user one cohort, from their first date.
- Reduce activity to one row per user per period.
distinctstops a user who acts ten times counting ten times. - Join them, and compute how many periods separate the cohort from the activity.
- Count distinct users per cohort and offset. Divide by the cohort size for a rate.
with cohort as (
select user_id, date_trunc('month', signed_up_at)::date as cohort_month
from users
),
activity as (
select distinct user_id, date_trunc('month', occurred_at)::date as active_month
from events
),
sizes as (
select cohort_month, count(*) as cohort_size from cohort group by 1
)
select c.cohort_month,
((extract(year from a.active_month) - extract(year from c.cohort_month)) * 12
+ extract(month from a.active_month) - extract(month from c.cohort_month))::int as months_since,
count(distinct c.user_id) as active_users,
round(100.0 * count(distinct c.user_id) / s.cohort_size, 1) as pct_of_cohort
from cohort c
join sizes s on s.cohort_month = c.cohort_month
join activity a on a.user_id = c.user_id and a.active_month >= c.cohort_month
group by c.cohort_month, s.cohort_size, months_since
order by c.cohort_month, months_since;Month 0 is the signup month itself. Its percent should be close to 100 if signing up counts as activity, and lower if the event table does not include the signup.
Traps interviewers look for
- Counting events, not users. Use
count(distinct user_id). Otherwise heavy users inflate retention. - Wrong denominator. The percent is out of the cohort size, not out of the users active that month. Compute sizes separately.
- Month arithmetic. Subtracting two dates gives days, not months. Use year and month parts, or
DATEDIFFin the warehouse you are given. - Immature cohorts. A user who signed up last week has not had a chance to be active a month later. Leave out cells that lie in the future, or the latest cohorts will look like churn.
- Defining "active". Any event, a purchase or an app open. The prompt must say. Different definitions give very different curves.
- Users with no later activity vanish. An inner join drops periods with zero actives. If the report needs explicit zeros, cross join cohorts with offsets and left join the counts.
- Cohort drift. Taking the cohort from the first event, not from the signup date, can place a user in a different month. Use the field the prompt names.
Variations
- Day N retention. "Came back exactly 7 days after signup" compares
date(activity) - date(signup) = 7. - Rolling retention. Active on day N or any later day, so use
>=on the offset. - First purchase cohort. Cohort by
min(purchased_at)per user instead of signup. - Revenue cohorts. Sum amounts instead of counting users.
- Churn. One minus retention, or users active last period who are not active this period (an anti-join).
In other warehouses
- Month difference. Snowflake, SQL Server and Redshift use
DATEDIFF(month, start, end). BigQuery usesDATE_DIFF(end, start, MONTH). PostgreSQL has no single function, so use the year and month arithmetic shown above. DATE_TRUNCargument order differs in BigQuery. See the dates lesson.DATEDIFF(month, ...)in Snowflake and SQL Server counts calendar boundaries crossed, not full months, which is what you want for month cohorts.