#1 India's Top IT Training Institute

On Demand Courses

9 programs

×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →

SQL Cohort Analysis
PDF Downloads

Download the SQL cohort analysis guide PDF from Uncodemy: define cohorts, build retention tables and read the results, with tested queries.

Prepared by: Uncodemy trainer team Reviewed by: Uncodemy Last reviewed: 27 Oct 2026
Five Steps of a Cohort Query
1
Find the cohortEach customer's first order month.
2
Find activityThe months in which each customer was active.
3
Calculate the period numberMonths between activity and the cohort month.
4
Count active customersDistinct customers by cohort and period.
5
Divide by cohort sizeThis gives the retention rate.

Quick Answer

This page offers Uncodemy's SQL cohort analysis guide as a PDF download. A cohort is a group of customers who share a starting point, such as the month of their first order. Cohort analysis tracks how each group behaves over time, which shows retention more honestly than a single overall average. The guide builds the full SQL step by step.

Download

SQL Cohort Analysis PDF

Download the guide below. Each download is its own file, and everything in it is also explained on this page.

1 PDF Available
PDF 01 Main Guide

SQL Cohort Analysis Guide

Key terms, the five steps of a cohort query, the full retention table in SQL, how to read the retention triangle, patterns to interpret, cohort variations and common mistakes, with a practice task.

PDF Free Oct 2026
Download PDF

A short form asks for your details before the download starts. Your details are used only to share study material and course information.

01

Why cohorts

An overall retention or repeat-purchase figure can hide what is really happening. If a business is growing quickly, most customers are new and the overall average looks healthy even if each new group is leaving faster than the last. Cohort analysis separates customers by when they started and follows each group on its own timeline, so you can ask whether customers acquired in March behave differently from those acquired in June, and whether a change such as a new onboarding flow actually improved retention. It is a standard tool in subscription, e-commerce and app analytics.

The examples use these tables: customers(customer_id, name, city, signup_date), orders(order_id, customer_id, order_date, status, order_total), order_items(order_id, product_id, quantity, unit_price) and products(product_id, product_name, category). Dates are text in YYYY-MM-DD form, so the examples run in SQLite and need small changes in other databases.

02

Key terms

TermMeaning
CohortA group of customers who started in the same period, such as the month of first order
Cohort monthThe month in which the customer first purchased
Period numberMonths since the cohort month (0 for the first month, 1 for the next, and so on)
Retention rateShare of the cohort that was active in a given period
Active customerA customer with at least one qualifying order in the period
03

The five steps of a cohort query

  1. Find each customer's first order month: this is the cohort.
  2. Find the months in which each customer was active.
  3. Calculate the period number as the gap in months between activity and the cohort month.
  4. Count distinct active customers by cohort and period.
  5. Divide by the cohort size to get the retention rate.
04

Step 1 and 2: cohorts and activity

Each customer's first order month and every month in which they ordered.

WITH first_order AS (
  SELECT customer_id,
         MIN(strftime('%Y-%m', order_date)) AS cohort_month
  FROM orders
  WHERE status <> 'cancelled'
  GROUP BY customer_id
),
activity AS (
  SELECT DISTINCT customer_id,
         strftime('%Y-%m', order_date) AS active_month
  FROM orders
  WHERE status <> 'cancelled'
)
SELECT f.cohort_month, a.active_month, COUNT(*) AS customers
FROM first_order f
JOIN activity a ON a.customer_id = f.customer_id
GROUP BY f.cohort_month, a.active_month
ORDER BY f.cohort_month, a.active_month;
05

The full retention table

Adds the period number and the retention rate. The month difference is computed from the year and month parts, which works in SQLite; other databases have date-difference functions.

WITH first_order AS (
  SELECT customer_id, MIN(strftime('%Y-%m', order_date)) AS cohort_month
  FROM orders WHERE status <> 'cancelled'
  GROUP BY customer_id
),
activity AS (
  SELECT DISTINCT customer_id, strftime('%Y-%m', order_date) AS active_month
  FROM orders WHERE status <> 'cancelled'
),
joined AS (
  SELECT f.cohort_month, a.customer_id,
         (CAST(SUBSTR(a.active_month, 1, 4) AS INTEGER) * 12 + CAST(SUBSTR(a.active_month, 6, 2) AS INTEGER))
       - (CAST(SUBSTR(f.cohort_month, 1, 4) AS INTEGER) * 12 + CAST(SUBSTR(f.cohort_month, 6, 2) AS INTEGER)) AS period
  FROM first_order f
  JOIN activity a ON a.customer_id = f.customer_id
),
cohort_size AS (
  SELECT cohort_month, COUNT(*) AS size FROM first_order GROUP BY cohort_month
)
SELECT j.cohort_month, j.period,
       COUNT(DISTINCT j.customer_id) AS active_customers,
       ROUND(COUNT(DISTINCT j.customer_id) * 100.0 / s.size, 1) AS retention_pct
FROM joined j
JOIN cohort_size s ON s.cohort_month = j.cohort_month
GROUP BY j.cohort_month, j.period, s.size
ORDER BY j.cohort_month, j.period;
06

How to read the result

Arrange the output as a triangle: cohorts down the side, periods across the top and retention percentages in the cells. Period zero is always one hundred percent because every customer in the cohort ordered in their first month.

Read across a row to see how one cohort fades; read down a column to compare cohorts at the same age. A column that improves for later cohorts suggests that something you changed helped. A sudden drop in one calendar month across all cohorts points to an external event, such as a stock-out or an outage, rather than to customer behaviour.

07

Interpreting patterns

Pattern in the tablePossible meaningNext question
Steep fall after period 1Many one-time buyersWhat happens in the first purchase experience?
Flat tail after period 3A loyal core remainsWho are they and how do we find more?
Newer cohorts retain betterImprovement in product or onboardingWhich change coincided with it?
Newer cohorts retain worseLower-quality acquisition or product issueWhich channel or offer changed?
Same dip in one calendar monthExternal eventCheck operations and marketing records
08

Cohort variations — and common cohort mistakes

Cohort variations

  • Weekly cohorts for fast-moving apps; yearly cohorts for slow businesses.
  • Cohorts by acquisition channel to compare quality of customers.
  • Revenue retention: sum spending by period rather than counting customers.
  • Rolling retention: active in period n or later.

Common cohort mistakes

  • Counting orders instead of distinct customers.
  • Including incomplete current months, which makes the latest period look weak.
  • Tiny cohorts that produce noisy percentages.
  • Mixing definitions of active between reports.
  • Ignoring cancelled or refunded orders when the business rules say to exclude them.
09

Practice

Build the table on any orders dataset. Then change the definition of active, for example to require at least two orders in the month, and see how retention changes. Write a short summary with the key pattern and one recommended action. Being able to explain a retention triangle clearly is a strong interview skill.

Learn with a trainer

Want to build retention analysis the way analysts do at work? Explore Uncodemy's Data Analytics course and learn from industry trainers.

View Course
About these PDFs: These PDFs are study material for learning and revision. They are not a certificate, a degree or a promise of a job, and technology changes, so check official documentation for the latest details. Spot an error? Write to info@uncodemy.com and the trainer team will review it.
FAQ

Frequently Asked Questions

What is cohort analysis?
Cohort analysis groups customers by a shared starting point, such as the month of their first purchase, and tracks each group's behaviour over time.
How do you calculate retention in SQL?
Find each customer's cohort month, find their active months, compute the period number, count distinct active customers per cohort and period and divide by the cohort size.
Why is cohort retention better than overall retention?
Overall figures mix customers of different ages and can hide whether newer groups behave better or worse.
What does period 0 mean in a cohort table?
Period 0 is the cohort's first month. It is always 100 percent because each customer made a purchase then.
What are common cohort analysis mistakes?
Counting orders instead of customers, including incomplete months, using tiny cohorts and inconsistent definitions of an active customer.
Are these PDFs free to download?
Yes, the SQL cohort analysis PDF is free. A short form asks for your details before the download starts.