Growth, Retention & Cohort Analysis
Key metrics
- MoM growth %:
(this − last) / last × 100→lag() - Running total / YTD:
sum() OVER (ORDER BY month) - Active users per month (MAU):
count(DISTINCT user_id)grouped by month - Retention: of the users active in month X, how many are still active in month X+1?
- Churn: the opposite of retention
Cohort analysis ⭐
A cohort is a group of users who started in the same period (signup month or first activity month).
Steps:
- Cohort month per user = month of first activity:
min(date_trunc('month', spent_on)) - Activity months per user: distinct months they were active
- Months since start = difference between activity month and cohort month
- Count active users per (cohort, months since start)
- Divide by the cohort size → retention %
- Pivot months-since-start into columns for the classic cohort triangle
Months between two dates:
(EXTRACT(year FROM age(b, a)) * 12 + EXTRACT(month FROM age(b, a)))::int
(or compute year * 12 + month for each and subtract)
Reading a cohort table
| cohort | size | M0 | M1 | M2 |
|---|---|---|---|---|
| Sep | 4 | 100% | 100% | 100% |
| Each row is a group of users; each column shows how many were still active N months later. Look for drop-offs after onboarding (M1) — that's where product improvements help most. |
Common interview variants
- Users active in consecutive months (self-join on month = month + 1)
- 7-day / 30-day retention after signup
- New vs returning users per month
- Rolling 3-month active users
Example
-- 1. Monthly totals, growth % and running total
WITH m AS (
SELECT date_trunc('month', spent_on)::date AS month,
count(DISTINCT user_id) AS active_users,
sum(amount) AS total
FROM expenses GROUP BY 1
)
SELECT to_char(month, 'Mon') AS month_label, active_users, total,
round(100.0 * (total - lag(total) OVER (ORDER BY month)) / lag(total) OVER (ORDER BY month), 1) AS growth_pct,
sum(total) OVER (ORDER BY month) AS running_total
FROM m ORDER BY m.month;
-- 2. Add some users who started later so the cohorts differ
INSERT INTO expenses (user_id, title, amount, spent_on) VALUES
(5, 'First coffee', 200, '2026-10-04'), (5, 'Books', 900, '2026-11-12'),
(6, 'Bus', 50, '2026-10-20'); -- Hari: active in October only
-- 3. Cohort retention table (cohort = first active month)
WITH activity AS (
SELECT DISTINCT user_id, date_trunc('month', spent_on)::date AS month FROM expenses
), cohorts AS (
SELECT user_id, min(month) AS cohort FROM activity GROUP BY user_id
), joined AS (
SELECT c.cohort, a.user_id,
((EXTRACT(year FROM a.month) * 12 + EXTRACT(month FROM a.month))
- (EXTRACT(year FROM c.cohort) * 12 + EXTRACT(month FROM c.cohort)))::int AS month_n
FROM activity a JOIN cohorts c USING (user_id)
)
SELECT to_char(cohort, 'Mon') AS cohort,
count(DISTINCT user_id) FILTER (WHERE month_n = 0) AS size,
round(100.0 * count(DISTINCT user_id) FILTER (WHERE month_n = 1)
/ count(DISTINCT user_id) FILTER (WHERE month_n = 0)) AS m1_pct,
round(100.0 * count(DISTINCT user_id) FILTER (WHERE month_n = 2)
/ count(DISTINCT user_id) FILTER (WHERE month_n = 0)) AS m2_pct
FROM joined
GROUP BY cohort ORDER BY joined.cohort;
-- Sep | 4 | 100 | 100
-- Oct | 2 | 50 | 0 (their month 2 = December hasn't happened yet)
-- 4. Month-to-month retention: active in October AND in November
SELECT count(*) AS retained,
(SELECT count(DISTINCT user_id) FROM expenses WHERE spent_on >= '2026-10-01' AND spent_on < '2026-11-01') AS oct_active
FROM (SELECT DISTINCT user_id FROM expenses WHERE spent_on >= '2026-10-01' AND spent_on < '2026-11-01') o
WHERE EXISTS (SELECT 1 FROM expenses e WHERE e.user_id = o.user_id AND e.spent_on >= '2026-11-01');
-- 5 of 6 October users came back in November (Hari didn't)
-- 5. New vs returning users per month
WITH firsts AS (SELECT user_id, min(date_trunc('month', spent_on)) AS first_month FROM expenses GROUP BY user_id)
SELECT to_char(date_trunc('month', e.spent_on), 'Mon') AS month,
count(DISTINCT e.user_id) FILTER (WHERE date_trunc('month', e.spent_on) = f.first_month) AS new_users,
count(DISTINCT e.user_id) FILTER (WHERE date_trunc('month', e.spent_on) > f.first_month) AS returning_users
FROM expenses e JOIN firsts f USING (user_id)
GROUP BY date_trunc('month', e.spent_on) ORDER BY date_trunc('month', e.spent_on);