Skip to content

Cohort Analysis in SQL: How to Build a Retention Table

A triangular grid of retention cells fading in color as customers become less active over time.

A cohort analysis groups customers by when they started, then tracks what each group does over time. It answers a question that overall averages hide: are people who join today sticking around as well as people who joined last year?

What a cohort is

A cohort is a group of users who share a starting point, most often the month of their first purchase or sign-up. Instead of asking what share of all customers are active, you ask what share of the January cohort is still active one month later, two months later, and so on. Comparing cohorts shows whether retention is improving or getting worse.

The retention table

The standard output is a grid. Each row is a cohort, each column is the number of months since the cohort started, and each cell is the number or share of that cohort still active. Reading across a row shows how one cohort decays, and reading down a column shows whether newer cohorts retain better than older ones at the same age.

Building it in SQL

The query has three steps: find each customer’s first month, record the months in which they were active, and count customers by cohort and age. The version below uses PostgreSQL syntax and a customers and orders setup, and other databases need small changes to the date functions.

WITH first_orders AS (
    SELECT customer_id,
           DATE_TRUNC('month', MIN(order_date)) AS cohort_month
    FROM orders
    GROUP BY customer_id
),
activity AS (
    SELECT o.customer_id,
           f.cohort_month,
           DATE_TRUNC('month', o.order_date) AS active_month
    FROM orders o
    JOIN first_orders f ON f.customer_id = o.customer_id
)
SELECT cohort_month,
       (EXTRACT(YEAR FROM AGE(active_month, cohort_month)) * 12
        + EXTRACT(MONTH FROM AGE(active_month, cohort_month))) AS months_since,
       COUNT(DISTINCT customer_id) AS active_customers
FROM activity
GROUP BY cohort_month, months_since
ORDER BY cohort_month, months_since;

The row where months_since is 0 is the cohort’s size. Divide each later value by that number to get the retention rate. The structure uses named steps, as explained in SQL CTEs explained, and a join, as in SQL joins explained. If you want the first order per customer without a grouped minimum, window functions are an alternative.

Common mistakes

  • Counting rows instead of customers. Use a distinct count, or a customer with five orders in a month counts five times.
  • Reading incomplete cohorts as bad ones. The newest cohort has had less time, so its later columns are empty or partial. Do not compare it to older cohorts at ages it has not reached.
  • Vague definitions. Decide what counts as active, such as any order, a login or a payment, and use the same rule for every cohort.
  • Mixing time zones. A midnight timestamp can land in different months depending on the zone, so truncate dates consistently.

Using the result

A drop-off after the first month points to onboarding problems, and a slow, steady decline is a different problem from a sharp cliff. When you change something, such as a pricing page or a welcome email, compare the cohorts before and after, but remember that is an observation, not an experiment. A controlled comparison needs a proper test, as in A/B testing explained. Getting consistent source data for this kind of analysis is the job of a marketing data pipeline.

A quick checklist

  1. Is the cohort defined clearly, such as month of first order?
  2. Does “active” mean the same thing for every cohort?
  3. Are you counting distinct customers?
  4. Are you ignoring ages the newest cohort has not reached?
  5. Are dates truncated consistently across time zones?

If you want retention and cohort dashboards built on your data, see my analytics dashboards service.

FAQ

What is cohort analysis?
It is a way of grouping users by when they started and tracking each group’s behavior over time, which shows whether newer users behave differently from older ones.

How do you calculate retention rate?
Divide the number of customers from a cohort who are active in a given period by the size of that cohort at the start, then compare across periods and cohorts.

Why do the newest cohorts have empty cells?
They have not existed long enough to reach those ages yet, so the cells are blank rather than zero.

Related posts

Leave a comment

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