TGViewer
Data Analytics Data Analytics @sqlspecialist ยท 111K subscribers
Post #3106 2.81K
๐Ÿš€ Data Analyst Roadmap โ€” Part 20

๐Ÿง  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:
  • โค 6
More from @sqlspecialist
  1. Oct 4, 20269๏ธโƒฃ How would you calculate month-over-month growth? Sample Answer: โ€œI would first retrievโ€ฆ
  2. Oct 4, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 3 Guys, let's continue our Data Analyst Interviewโ€ฆ
  3. Sep 29, 2026๐Ÿ”Ÿ How would you find duplicate records in SQL? Sample Answer: "I would first identify theโ€ฆ
  4. Sep 29, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 2 Guys, let's continue our Data Analyst Interviewโ€ฆ
  5. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
  6. Sep 29, 2026"After identifying duplicates, I investigate whether they are genuine duplicate records orโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’