Lessons › SQL › Go further in SQL: Business Analytics Patterns
SQL lesson
Cohort retention analysis
Cohort retention analysis groups users by when they first showed up, then tracks what percentage of each group came back in later periods.
Try this lesson free →Example
Cohort retention
WITH first_seen AS (
SELECT customer_id, MIN(order_date) AS first_at
FROM orders
GROUP BY customer_id
)
SELECT DATE_TRUNC('month', f.first_at) AS cohort,
DATE_TRUNC('month', t.order_date) AS active_month,
COUNT(DISTINCT t.customer_id)
FROM orders t
JOIN first_seen f USING (customer_id)
GROUP BY cohort, active_month
ORDER BY cohort, active_month| cohort | active_month | distinct_customers |
|---|---|---|
| 2026-01 | 2026-01 | 2 |
| 2026-01 | 2026-02 | 1 |
| 2026-01 | 2026-03 | 1 |
| 2026-02 | 2026-02 | 1 |
Step by step
- WITH lets you name a sub-query so the main query can reference it.
WITH first_seen AS (SELECT customer_id, MIN(order_date) AS first_at FROM orders GROUP BY customer_id) - AS gives a column a friendlier name in the result.
WITH first_seen AS (SELECT customer_id, MIN(order_date) AS first_at FROM orders GROUP BY customer_id) SELECT DATE_TRUNC('month', f.first_at) AS cohort, DATE_TRUNC('month', t.order_date) AS active_month, COUNT(DISTINCT t.customer_id) - FROM tells SQL which table to pull the data from.
WITH first_seen AS (SELECT customer_id, MIN(order_date) AS first_at FROM orders GROUP BY customer_id) SELECT DATE_TRUNC('month', f.first_at) AS cohort, DATE_TRUNC('month', t.order_date) AS active_month, COUNT(DISTINCT t.customer_id) FROM orders t - JOIN combines rows from two tables that share a key.
WITH first_seen AS (SELECT customer_id, MIN(order_date) AS first_at FROM orders GROUP BY customer_id) SELECT DATE_TRUNC('month', f.first_at) AS cohort, DATE_TRUNC('month', t.order_date) AS active_month, COUNT(DISTINCT t.customer_id) FROM orders t JOIN first_seen f USING (customer_id)
Practice
1. Using orders, show each customer_id with the month of their first order, called first_month.
SELECT customer_id, DATE_TRUNC('month', MIN(order_date)) AS first_month FROM orders GROUP BY customer_id ORDER BY customer_id2. Using orders, count new customers by the month of their first order. Show first_month and new_customers.
WITH f AS (SELECT customer_id, DATE_TRUNC('month', MIN(order_date)) AS first_month FROM orders GROUP BY customer_id) SELECT first_month, COUNT(*) AS new_customers FROM f GROUP BY first_month ORDER BY first_month3. Using orders, build a cohort retention table: a customer's cohort is the month of their first order. For each cohort and each month its customers ordered in, count the customers. Name the columns cohort, active_month and customers.
WITH f AS (SELECT customer_id, DATE_TRUNC('month', MIN(order_date)) AS cohort FROM orders GROUP BY customer_id) SELECT f.cohort, DATE_TRUNC('month', o.order_date) AS active_month, COUNT(DISTINCT o.customer_id) AS customers FROM orders o JOIN f ON o.customer_id = f.customer_id GROUP BY f.cohort, DATE_TRUNC('month', o.order_date) ORDER BY f.cohort, active_month4. Using visits, build the same retention table for app users: a user's cohort is the month of their first visit. Name the columns cohort, active_month and users.
WITH f AS (SELECT customer_id, DATE_TRUNC('month', MIN(visit_date)) AS cohort FROM visits GROUP BY customer_id) SELECT f.cohort, DATE_TRUNC('month', v.visit_date) AS active_month, COUNT(DISTINCT v.customer_id) AS users FROM visits v JOIN f ON v.customer_id = f.customer_id GROUP BY f.cohort, DATE_TRUNC('month', v.visit_date) ORDER BY f.cohort, active_month5. Using orders, for each cohort (the month of a customer's first order), count how many of its customers came back and ordered in a later month. Name the columns cohort and returning_customers.
WITH f AS (SELECT customer_id, DATE_TRUNC('month', MIN(order_date)) AS cohort FROM orders GROUP BY customer_id) SELECT f.cohort, COUNT(DISTINCT o.customer_id) AS returning_customers FROM orders o JOIN f ON o.customer_id = f.customer_id WHERE DATE_TRUNC('month', o.order_date) > f.cohort GROUP BY f.cohort ORDER BY f.cohortLearn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.
Try this lesson free →