Full answer
sql
CREATE TABLE users (id INTEGER PRIMARY KEY, signup TEXT NOT NULL);
CREATE TABLE activity (user_id INTEGER, day TEXT);
INSERT INTO users VALUES (1,'2026-01-10'),(2,'2026-01-20'),(3,'2026-02-03'),(4,'2026-02-15'),(5,'2026-02-20');
INSERT INTO activity VALUES
(1,'2026-01-10'),(1,'2026-02-05'),(1,'2026-03-02'),
(2,'2026-01-20'),
(3,'2026-02-03'),(3,'2026-03-10'),
(4,'2026-02-15'),(4,'2026-03-01'),(4,'2026-04-02'),
(5,'2026-02-20');sql
WITH c AS (
SELECT id, substr(signup, 1, 7) AS cohort,
substr(date(substr(signup, 1, 7) || '-01', '+1 month'), 1, 7) AS next_month
FROM users
)
SELECT cohort, COUNT(*) AS users,
SUM(EXISTS (SELECT 1 FROM activity a WHERE a.user_id = c.id AND substr(a.day, 1, 7) = c.next_month)) AS retained,
ROUND(100.0 * SUM(EXISTS (SELECT 1 FROM activity a WHERE a.user_id = c.id AND substr(a.day, 1, 7) = c.next_month)) / COUNT(*), 1) AS pct
FROM c GROUP BY cohort ORDER BY cohort;| cohort | users | retained | pct |
|---|---|---|---|
| 2026-01 | 2 | 1 | 50.0 |
| 2026-02 | 3 | 2 | 66.7 |
January's cohort has users 1 and 2; only user 1 was active in February, so retention is 50 percent. February's cohort has users 3, 4 and 5; users 3 and 4 were active in March, giving 66.7 percent.
The next month is computed with date(month || '-01', '+1 month'), which starts from the first of the month and so avoids month-end surprises.
Design questions an interviewer expects you to raise: does signup-day activity count, should the window be "next calendar month" or "days 30 to 60 after signup", and should the latest cohort be shown when its following month has not finished yet?
sql
CREATE TABLE users (id INTEGER PRIMARY KEY, signup TEXT NOT NULL);
CREATE TABLE activity (user_id INTEGER, day TEXT);
INSERT INTO users VALUES (1,'2026-01-10'),(2,'2026-01-20'),(3,'2026-02-03'),(4,'2026-02-15'),(5,'2026-02-20');
INSERT INTO activity VALUES
(1,'2026-01-10'),(1,'2026-02-05'),(1,'2026-03-02'),
(2,'2026-01-20'),
(3,'2026-02-03'),(3,'2026-03-10'),
(4,'2026-02-15'),(4,'2026-03-01'),(4,'2026-04-02'),
(5,'2026-02-20');
WITH c AS (
SELECT id, substr(signup, 1, 7) AS cohort,
substr(date(substr(signup, 1, 7) || '-01', '+1 month'), 1, 7) AS next_month
FROM users
)
SELECT cohort, COUNT(*) AS users,
SUM(EXISTS (SELECT 1 FROM activity a WHERE a.user_id = c.id AND substr(a.day, 1, 7) = c.next_month)) AS retained,
ROUND(100.0 * SUM(EXISTS (SELECT 1 FROM activity a WHERE a.user_id = c.id AND substr(a.day, 1, 7) = c.next_month)) / COUNT(*), 1) AS pct
FROM c GROUP BY cohort ORDER BY cohort;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- Cohort = signup month
- EXISTS checks for activity in the following month
- Retained / cohort size, with non-integer division
- State the definition of 'active' and the window
Common mistakes
- Counting activity rows instead of distinct users
- Including incomplete recent cohorts without a note
Follow-ups
- How would you build a full cohort matrix for months 1 to 6?
- How would you define rolling 30-day retention?
Tips
- Clarify definitions before writing SQL