๐ง SQL Level 10 โ Cohort Analysis, Retention & Customer Analytics
Now we're moving from writing SQL queries to using SQL for real analytical problems.
Customer analytics is one of the most important areas because businesses want to know:
๐ฅ Who are our customers?
๐ When did they first purchase?
๐ Do they come back?
๐ When do they stop returning?
๐ฐ Which customers generate most revenue?
๐ How does behavior change over time?
One of the most powerful techniques for this is Cohort Analysis.
๐น 1. What Is Cohort Analysis?
A cohort is a group who share a common starting point.
โข Jan 2026 Cohort = first purchase in Jan 2026
โข Feb 2026 Cohort = first purchase in Feb 2026
Instead of mixing everyone, we track each group over time.
๐น 2. Why It Matters
Suppose total monthly customers are increasing.
That sounds positive.
But what if new customers are increasing while existing customers stop returning?
A simple monthly report may hide this problem.
Cohort analysis separates New vs Returning customers.
This makes retention problems much easier to identify.
๐น 3. Step 1 โ Find Each Customer's First Purchase
SELECT
Customer_ID,
MIN(Order_Date) AS First_Order_Date
FROM Orders
GROUP BY Customer_ID;
This gives us the first purchase date for every customer.
Customer | First Order
C101 | 2026-01-10
C102 | 2026-01-18
C103 | 2026-02-05
๐น 4. Step 2 โ Assign a Cohort Month
We can convert the first purchase into a month-level cohort.
First Purchase Date โ Cohort Month
C101 โ 2026-01
C103 โ 2026-02
The exact month-truncation syntax varies between SQL databases.
๐น 5. Step 3 โ Join Cohort Back to Orders
Now we need both:
Customer's cohort
and
Customer's subsequent activity
WITH Customer_Cohorts AS (
SELECT Customer_ID, MIN(Order_Date) AS First_Order_Date
FROM Orders GROUP BY Customer_ID
)
SELECT o.Customer_ID, c.First_Order_Date, o.Order_Date, o.Sales
FROM Orders o
JOIN Customer_Cohorts c ON o.Customer_ID = c.Customer_ID;
Now every transaction knows which cohort the customer belongs to.
๐น 6. Cohort Month vs Activity Month
โข Cohort Month: When first purchased
โข Activity Month: When purchase happened
Customer | Cohort | Activity
C101 | Jan | Jan
C101 | Jan | Feb
C101 | Jan | Mar
๐น 7. Measuring Retention
Retention measures how many customers from a cohort remain active in later periods.
Retention = Active in Period / Original Cohort * 100
โข Jan cohort: 100 customers
โข Feb: 60 active โ 60%
โข Mar: 40 active โ 40%
๐น 8. Retention Month
Months Since Cohort = Activity - Cohort
Eg:
Customer | Cohort | Activity | Months_Since_Cohort
C101 | Jan | Jan | 0
C101 | Jan | Feb | 1
C101 | Jan | Mar | 2
๐น 9. Cohort Retention Matrix
Conceptually, the final result may look like:
Cohort | Month0 | Month1 | Month2 | Month3
Jan | 100% | 60% | 40% | 30%
Feb | 100% | 65% | 45% | โ
Mar | 100% | 70% | โ | โ
This is often called a cohort retention matrix.
It immediately shows whether newer customer cohorts are retaining better or worse.
๐น 10. Customer Lifetime Value (CLV)
Another important customer metric is Customer Lifetime Value (CLV/LTV).
A simplified version can be based on:
Total Revenue Generated by Customer
A more advanced business model may consider:
โข Revenue
โข Gross margin
โข Purchase frequency
โข Retention
โข Customer lifespan
โข Acquisition cost
๐น 11. Average Order Value (AOV)
A basic customer metric is:
Average Order Value = Total Sales รท Number of Orders
In SQL: