Reading history

Cohort and Retention Analysis in SQL

4 min read 0 views

Build a cohort retention table in SQL: find each user's first month, count months since joining, and turn active-user counts into retention rates.

An app gains 1,000 new users in January. How many are still active in February? In March? Grouping users by the month they joined and tracking them over time is called cohort analysis, and the share who come back is the retention rate. It is one of the most common questions data analysts answer, and it combines several SQL skills: CTEs, date functions, joins and conditional aggregation.

In this lesson you will learn

  • What a cohort and a retention table are
  • Finding each user’s first month
  • Calculating “months since joining”
  • Building the cohort table with conditional counts
  • Turning counts into retention percentages

You should know CTEs, date functions and pivoting with CASE.

The Idea

A cohort is a group of users who started in the same period, for example everyone whose first login was in January. For each cohort we count how many were active in month 0 (the joining month), month 1, month 2 and so on.

cohortusersmonth 0month 1month 22026-013100%33.3%66.7%2026-022100%50%not yet2026-031100%not yetnot yetDarker = more of the cohort still active. Read each row left to right.

Building the Cohort Table

CREATE TABLE logins (user_id INTEGER, login_date TEXT);
INSERT INTO logins VALUES
 (1,'2026-01-03'),(1,'2026-02-10'),(1,'2026-03-05'),
 (2,'2026-01-15'),(2,'2026-03-20'),
 (3,'2026-01-22'),
 (4,'2026-02-02'),(4,'2026-03-14'),
 (5,'2026-02-18'),(5,'2026-02-25'),
 (6,'2026-03-08');

-- Step 1: each user's cohort = month of first login
WITH firsts AS (
  SELECT user_id, strftime('%Y-%m', MIN(login_date)) AS cohort
  FROM logins GROUP BY user_id
),
-- Step 2: months since joining for every login
activity AS (
  SELECT f.cohort, l.user_id,
         (CAST(strftime('%Y', l.login_date) AS INTEGER) * 12 + CAST(strftime('%m', l.login_date) AS INTEGER))
       - (CAST(substr(f.cohort,1,4) AS INTEGER) * 12 + CAST(substr(f.cohort,6,2) AS INTEGER)) AS month_no
  FROM logins AS l JOIN firsts AS f ON f.user_id = l.user_id
)
-- Step 3: count distinct active users per cohort and month
SELECT cohort,
       COUNT(DISTINCT CASE WHEN month_no = 0 THEN user_id END) AS m0,
       COUNT(DISTINCT CASE WHEN month_no = 1 THEN user_id END) AS m1,
       COUNT(DISTINCT CASE WHEN month_no = 2 THEN user_id END) AS m2
FROM activity
GROUP BY cohort
ORDER BY cohort;

Output

+---------+----+----+----+
| cohort  | m0 | m1 | m2 |
+---------+----+----+----+
| 2026-01 | 3  | 1  | 2  |
| 2026-02 | 2  | 1  | 0  |
| 2026-03 | 1  | 0  | 0  |
+---------+----+----+----+
(3 rows)
  1. firsts: MIN(login_date) per user gives their first login, and strftime('%Y-%m', …) turns it into a cohort label such as 2026-01.
  2. activity: each login gets month_no, the number of months since joining. Converting both dates to “year × 12 + month” and subtracting works across year boundaries too.
  3. Final step: COUNT(DISTINCT CASE WHEN month_no = 1 THEN user_id END) counts each user once, even if they logged in several times that month (user 5 logged in twice in February).

Reading the table: the January cohort had 3 users. Only 1 came back in month 1 (February), but 2 were active in month 2 (March), because user 2 skipped February and returned. The February cohort kept 1 of 2 users in March.

Retention Percentages

Divide each month by the cohort size (month 0). Wrap the previous query in a CTE named cohorts:

SELECT cohort, m0 AS users,
       ROUND(100.0 * m1 / m0, 1) AS m1_pct,
       ROUND(100.0 * m2 / m0, 1) AS m2_pct
FROM cohorts;

For January this gives 33.3% in month 1 and 66.7% in month 2. Note that recent cohorts have not yet had time to reach month 2, so their 0 really means “not yet known”. Real reports show those cells as blank.

Common Mistakes

  1. Counting logins instead of users. Use COUNT(DISTINCT user_id).
  2. Subtracting month numbers only. December to January gives −11; include the year.
  3. Treating future months as zero retention. A March cohort cannot have May data yet.
  4. Integer division in percentages. Use 100.0 *.

Practice Exercises

Exercise 1. Write a query that returns each user’s cohort and the number of distinct months they were active.

Show answer
SELECT user_id,
       strftime('%Y-%m', MIN(login_date)) AS cohort,
       COUNT(DISTINCT strftime('%Y-%m', login_date)) AS active_months
FROM logins
GROUP BY user_id;

User 1 was active in 3 months; users 2 and 4 in 2 months; users 3, 5 and 6 in 1 month.

Exercise 2. What is the month-1 retention rate of the February cohort?

Show answer

1 of 2 users returned in March, so 50%.

Frequently Asked Questions

Can cohorts be weekly instead of monthly?

Yes. Use the week of the first activity as the cohort, for example strftime('%Y-%W', …) in SQLite, and count weeks since joining as the difference in day numbers divided by 7.

Can I measure revenue instead of active users?

Yes. Replace the distinct count with SUM(CASE WHEN month_no = 1 THEN amount END) to see how much each cohort spends over time.

Summary

  • A cohort groups users by when they started.
  • Find each user’s first period, then the periods since then for each activity.
  • Count distinct users per cohort and period with conditional aggregation.
  • Retention = active users ÷ cohort size × 100.

Next: Data Quality Checks in SQL.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *