Case 3: Customer retention with repeat rate and cohorts

Winning new customers costs money, as case 2 showed. Whether it pays off is decided at the second purchase. You measure how many customers come back and compare months fairly using cohorts.

In short: count per customer first, then analyze. A WITH query produces the number of orders or the first order date for each customer, and the outer query groups by channel or cohort. EXISTS checks whether a customer bought again inside the window, so each customer counts at most once.

The situation

The CRM team has a hunch: paid social customers buy once and disappear, while email and organic customers are more likely to return. Before any budget moves, SQL should show whether that is true, in a way that compares older and newer customers fairly.

The data

You need orders (customer_id, order_date, status) and, for the channel, customers. Only completed orders count. Here is how many orders customers have placed:

WITH per_customer AS (
  SELECT customer_id, COUNT(*) AS n
  FROM orders
  WHERE status = 'completed'
  GROUP BY customer_id
)
SELECT n AS orders, COUNT(*) AS customers
FROM per_customer
GROUP BY n;
orderscustomers
177
239
39
44
55

77 of 134 customers bought exactly once; the other 57 are repeat buyers. This query is the first step of both tasks: one row per customer, and only then counting across those rows. That is what a CTE is for.

Task 1: repeat rate by channel

Task How many customers buy a second time? For each channel (acquisition channel from customers), show the number of customers with at least one completed order, the number of repeat_buyers (at least two completed orders) and the repeat_rate as a percentage, rounded to one decimal place.

customers 140 rows, 6 columns
customer_idnamecitysignup_datechannelcampaign_id
1Sophia CampbellLeeds2025-01-16Paid Search1
2Aiden AllenLeeds2025-01-24OrganicNULL
3Aiden MillerAustin2025-01-28OrganicNULL
4Isla WrightGlasgow2025-02-01Affiliate4
5Henry GarciaGlasgow2025-02-07Email3
6Ryan ThomasPortland2025-02-14OrganicNULL
7Priya MurphyBristol2025-02-25OrganicNULL
8Chloe WilsonLondon2025-03-03Paid Social2
9Amelia EvansManchester2025-03-06Paid Social2
10Jack WilsonSeattle2025-03-15Paid Social2
11Grace CampbellAustin2025-03-17OrganicNULL
12Ava LewisAustin2025-03-29Email3
13Ruby MurphyLeeds2025-03-30Affiliate4
14Samuel KhanAtlanta2025-04-05Affiliate4
15Ruby MillerLeeds2025-04-12Email3
16Noah ThomasLondon2025-04-19Email3
17Leo DaviesDenver2025-04-26OrganicNULL
18Leo WrightChicago2025-05-03OrganicNULL
19Mia TaylorGlasgow2025-05-08OrganicNULL
20Liam RobinsonSeattle2025-05-12Email3
21Ella EvansDenver2025-05-17Paid Social2
22Daniel ScottBoston2025-05-18OrganicNULL
23Amelia WrightManchester2025-05-25Paid Search1
24Freya DaviesAtlanta2025-06-01OrganicNULL
25Noah TaylorBoston2025-06-09Email3
26Ava MillerSeattle2025-06-15Paid Social6
27Lucas WilsonAustin2025-06-16Email3
28Liam KhanPortland2025-06-18Paid Social6
29Lucas WrightGlasgow2025-06-25Paid Social6
30Noah MurphyDenver2025-07-06OrganicNULL
31Grace KhanBristol2025-07-08Paid Search5
32Liam DaviesSeattle2025-07-10Paid Search5
33Mason HallSeattle2025-07-14Paid Social6
34Ethan KhanPortland2025-07-24Paid Social6
35Jack ClarkeLeeds2025-07-28Paid Social6
36Ethan CampbellSeattle2025-08-01Paid Social6
37Harper KingBoston2025-08-06OrganicNULL
38Freya ScottManchester2025-08-09OrganicNULL
39Daniel JohnsonManchester2025-08-14Paid Social6
40Ruby WilsonAtlanta2025-08-19Affiliate4
41Zoe KhanAtlanta2025-08-29OrganicNULL
42Oliver YoungAustin2025-08-29Email3
43Leo KingGlasgow2025-09-03OrganicNULL
44Noah AllenBristol2025-09-06Paid Search5
45Oliver LewisAustin2025-09-15OrganicNULL
46James WalkerAustin2025-09-18Paid Search5
47Ava DaviesSeattle2025-09-19OrganicNULL
48Leo AllenLondon2025-09-29OrganicNULL
49James HallSeattle2025-09-29Paid Search5
50Aiden ThomasBoston2025-10-08OrganicNULL
51Oliver AllenAustin2025-10-11Paid Search5
52Daniel ClarkeGlasgow2025-10-14Paid Search5
53Liam HarrisSeattle2025-10-21Email3
54Lucas CampbellManchester2025-10-24OrganicNULL
55Freya BrownLondon2025-10-29Paid Search5
56Freya YoungSeattle2025-11-04OrganicNULL
57Sophia LewisPortland2025-11-05Paid Search5
58Freya EvansBristol2025-11-18OrganicNULL
59Leo PatelLondon2025-11-23Paid Search5
60Ruby ThomasGlasgow2025-11-23Paid Social8
61Amelia TaylorManchester2025-11-25Email3
62Liam MurphyDenver2025-12-01Paid Social8
63Daniel WalkerChicago2025-12-08OrganicNULL
64Zoe KingManchester2025-12-11OrganicNULL
65James ClarkeLeeds2025-12-15Paid Search5
66Samuel KingGlasgow2025-12-19Paid Search5
67Leo MillerDenver2025-12-25OrganicNULL
68Sophia EvansPortland2025-12-26OrganicNULL
69Sophia MurphyAustin2026-01-06Paid Social10
70Olivia KhanAustin2026-01-06Email3
71James ScottLeeds2026-01-16Paid Search9
72Noah SmithChicago2026-01-17Paid Search9
73Liam ScottLeeds2026-01-23Paid Social10
74Priya ScottPortland2026-01-26OrganicNULL
75Jack DaviesAustin2026-02-02Paid Search9
76Priya CampbellGlasgow2026-02-06Affiliate4
77Olivia AllenSeattle2026-02-07OrganicNULL
78Lucas YoungAustin2026-02-13OrganicNULL
79Emma GarciaBristol2026-02-15OrganicNULL
80Grace BrownLondon2026-02-20Affiliate4
81Zoe TaylorSeattle2026-02-25Paid Search9
82Ava BrownManchester2026-03-01OrganicNULL
83Ryan WilsonSeattle2026-03-07OrganicNULL
84Liam WrightAustin2026-03-11Email3
85Olivia TaylorSeattle2026-03-12Affiliate4
86Ethan EvansBoston2026-03-17OrganicNULL
87Ella ScottBoston2026-03-19Paid Search9
88Daniel HallGlasgow2026-04-01Affiliate4
89Chloe ThomasSeattle2026-04-05Paid Search9
90Harper TaylorDenver2026-04-05Paid Search9
91Ethan KingAustin2026-04-11Paid Search9
92Leo CampbellAustin2026-04-14OrganicNULL
93Noah YoungAtlanta2026-04-19OrganicNULL
94Noah JohnsonDenver2026-04-24Paid Search9
95Daniel EvansGlasgow2026-04-28Affiliate4
96Ella WilsonLondon2026-04-30Paid Search9
97Grace AllenBristol2026-05-10Affiliate4
98Jack SmithBristol2026-05-11Paid Search9
99Grace EvansPortland2026-05-13Affiliate4
100Isla ClarkeLondon2026-05-18Paid Search9
101Daniel AllenSeattle2026-05-19Affiliate4
102Zoe BrownLondon2026-05-25OrganicNULL
103Daniel LewisAtlanta2026-06-01OrganicNULL
104Isla MillerSeattle2026-06-04OrganicNULL
105Daniel WrightManchester2026-06-08OrganicNULL
106Zoe WrightGlasgow2026-06-09OrganicNULL
107Chloe WalkerAustin2026-06-16Affiliate4
108Chloe ScottAustin2026-06-22Paid Search9
109Henry EvansGlasgow2026-06-26Affiliate4
110Ryan ScottGlasgow2026-06-26OrganicNULL
111Ruby GarciaBoston2026-07-02OrganicNULL
112Ryan YoungManchester2026-07-04OrganicNULL
113Priya WalkerDenver2026-07-09Affiliate4
114Chloe HallChicago2026-07-14OrganicNULL
115Jack CampbellManchester2026-07-19OrganicNULL
116Harper ScottManchester2026-07-27Paid Search12
117Freya WrightChicago2026-07-28OrganicNULL
118Jack HallGlasgow2026-08-03OrganicNULL
119Chloe MillerManchester2026-08-07Email3
120Oliver GarciaBoston2026-08-09OrganicNULL
121Daniel KingPortland2026-08-15OrganicNULL
122Ruby HarrisChicago2026-08-18OrganicNULL
123Ruby LewisPortland2026-08-25Paid Search12
124Henry HarrisManchester2026-08-27Paid Search12
125Ethan SmithBristol2026-08-31Email3
126Emma LewisBristol2026-09-03Email3
127Zoe EvansBristol2026-09-07OrganicNULL
128Ethan JohnsonBristol2026-09-13OrganicNULL
129Ella LewisDenver2026-09-19Paid Social13
130Olivia RobinsonSeattle2026-09-20OrganicNULL
131Amelia KingBoston2026-09-22Email3
132Oliver TaylorGlasgow2026-09-26Paid Search12
133James AllenSeattle2026-10-04Paid Social13
134Henry KhanAtlanta2026-10-05Paid Social13
135Mia WrightManchester2026-10-10Paid Search12
136Mia JohnsonLondon2026-10-16Affiliate4
137Grace DaviesLeeds2026-10-20Email3
138Liam PatelBristol2026-10-22Paid Social13
139Oliver WrightSeattle2026-10-27OrganicNULL
140Henry AllenSeattle2026-10-29Email3
orders 239 rows, 6 columns
order_idcustomer_idorder_datecampaign_idstatusamount
500112025-01-161completed29.8
500222025-01-24NULLcompleted139.7
500332025-01-28NULLcompleted49.9
500442025-02-014completed69.8
500552025-02-073completed89.9
500662025-02-14NULLcompleted24.9
500772025-02-25NULLcancelled24.9
500882025-03-032completed139.7
500992025-03-062completed34.9
501062025-03-13NULLcompleted24.9
5011102025-03-152completed19.9
5012112025-03-17NULLcompleted49.9
5013122025-03-293cancelled89.9
5014132025-03-304completed24.9
501522025-04-033completed24.9
5016142025-04-054completed79.9
501752025-04-063completed34.9
5018102025-04-10NULLcompleted89.9
5019152025-04-123completed114.8
5020162025-04-193completed99.9
5021172025-04-26NULLcancelled79.9
502272025-04-30NULLcompleted29.9
5023182025-05-03NULLcompleted34.9
5024192025-05-08NULLcompleted89.9
5025202025-05-123completed19.9
5026212025-05-172cancelled39.9
5027222025-05-18NULLcompleted79.9
5028142025-05-241completed89.9
5029232025-05-251completed29.8
5030162025-06-013cancelled27.9
5031242025-06-01NULLcompleted19.9
5032252025-06-093completed79.9
5033262025-06-156cancelled34.9
5034272025-06-163completed24.9
5035282025-06-186completed29.8
5036292025-06-256completed19.9
5037302025-07-06NULLcompleted19.8
5038312025-07-085completed24.9
5039322025-07-105completed119.7
5040332025-07-146completed49.9
5041342025-07-246completed34.9
5042352025-07-286cancelled89.9
5043242025-07-293completed34.9
5044362025-08-016completed104.8
504552025-08-053completed29.9
5046372025-08-06NULLcancelled119
504722025-08-073completed89.9
5048382025-08-09NULLcompleted9.9
5049392025-08-146completed19.9
5050402025-08-194completed89.9
5051412025-08-29NULLcompleted89.9
5052422025-08-293completed12.9
5053432025-09-03NULLcompleted24.9
5054442025-09-065completed34.9
5055232025-09-075completed99.9
5056382025-09-123completed25.8
5057452025-09-15NULLcompleted19.9
5058462025-09-185completed99.9
5059412025-09-19NULLcompleted34.9
5060472025-09-19NULLcompleted9.9
506152025-09-24NULLcompleted24.9
5062482025-09-29NULLcompleted79.9
5063492025-09-295completed29.9
5064502025-10-08NULLcompleted129
5065142025-10-10NULLcompleted86.7
5066512025-10-115completed119.7
5067162025-10-125completed99.9
5068522025-10-145completed119.7
5069342025-10-19NULLcompleted54.8
5070212025-10-20NULLcompleted49.9
5071422025-10-205completed16.9
5072532025-10-213completed89.9
5073542025-10-24NULLcompleted54.8
5074552025-10-295completed99.9
5075562025-11-04NULLcompleted34.9
5076572025-11-055completed114.8
5077272025-11-073completed12.9
5078312025-11-07NULLcompleted59.9
5079322025-11-07NULLcompleted27.9
5080582025-11-18NULLcompleted19.9
5081592025-11-235completed89.9
5082602025-11-238completed25.8
5083122025-11-247completed47.92
5084152025-11-248completed91.84
5085482025-11-247completed55.76
5086512025-11-247completed23.92
508712025-11-257completed31.92
508872025-11-25NULLcompleted47.92
5089612025-11-253completed23.92
509062025-11-267completed39.92
5091312025-11-268completed23.92
5092562025-11-268completed10.32
5093132025-11-277cancelled91.84
5094302025-11-277completed31.92
5095382025-11-287completed103.2
5096552025-11-288completed63.92
509792025-11-298completed27.92
509852025-11-308completed15.84
5099422025-11-303completed7.92
5100442025-11-307completed48.56
5101462025-11-307completed38.24
5102472025-11-307completed31.92
5103242025-12-017completed90.24
5104622025-12-018completed15.92
5105632025-12-08NULLcompleted19.8
5106642025-12-11NULLcompleted114.8
5107142025-12-12NULLcompleted29.9
5108652025-12-155completed89.7
5109662025-12-195completed104.8
5110672025-12-25NULLcompleted34.9
5111682025-12-26NULLcompleted59.9
5112422026-01-04NULLcompleted79.9
5113692026-01-0610completed16.9
5114702026-01-063completed19.9
511522026-01-099completed89.9
5116712026-01-169completed25.8
5117722026-01-179completed89.9
5118502026-01-18NULLcompleted19.9
5119732026-01-2310completed59.9
5120742026-01-26NULLcompleted19.8
5121752026-02-029completed119
5122762026-02-064completed24.9
5123772026-02-07NULLcompleted79.9
5124782026-02-13NULLcompleted9.9
5125792026-02-15NULLcompleted104.8
5126742026-02-18NULLcompleted49.9
5127802026-02-204completed19.9
5128682026-02-24NULLcompleted12.9
5129812026-02-259completed29.9
5130522026-02-28NULLcompleted59.9
5131382026-03-013completed89.9
5132822026-03-01NULLcompleted19.9
513342026-03-02NULLcompleted29.8
5134672026-03-0311completed39.9
513522026-03-059completed44.9
5136172026-03-05NULLcompleted79.9
5137312026-03-06NULLcompleted136.7
5138832026-03-07NULLcompleted49.9
5139702026-03-0811completed47.8
5140842026-03-113completed49.8
514172026-03-12NULLcompleted19.9
5142532026-03-1211completed19.9
5143852026-03-124completed22.8
514452026-03-13NULLcancelled104.8
5145162026-03-149completed34.9
5146572026-03-15NULLcompleted107.8
5147142026-03-1611completed24.9
5148322026-03-1611completed34.9
5149542026-03-17NULLcompleted34.9
5150862026-03-17NULLcompleted25.8
5151272026-03-18NULLcompleted9.9
5152872026-03-199completed119
5153722026-03-2211completed24.9
5154882026-04-014completed29.9
5155892026-04-059completed49.9
5156902026-04-059completed102.8
5157742026-04-063completed24.9
5158912026-04-119completed29.9
5159612026-04-12NULLcompleted79.9
5160922026-04-14NULLcompleted29.9
5161932026-04-19NULLcompleted29.9
5162942026-04-249completed29.9
5163952026-04-284completed44.9
5164962026-04-309completed129
5165922026-05-07NULLcompleted55.7
5166972026-05-104completed54.8
5167982026-05-119completed14.9
5168992026-05-134completed34.9
51691002026-05-189completed129
51701012026-05-194completed148.7
5171632026-05-233cancelled24.9
51721022026-05-25NULLcompleted44.9
5173782026-06-01NULLcancelled153.9
51741032026-06-01NULLcompleted99.9
5175422026-06-029completed129
51761042026-06-04NULLcompleted49.9
51771052026-06-08NULLcompleted19.9
5178502026-06-09NULLcompleted102.8
51791062026-06-09NULLcompleted119.7
51801072026-06-164cancelled29.7
51811082026-06-229completed12.9
5182792026-06-233completed99.9
51831092026-06-264completed129
51841102026-06-26NULLcancelled24.9
5185812026-06-28NULLcompleted99.9
51861112026-07-02NULLcompleted158.9
5187772026-07-04NULLcompleted114.7
51881122026-07-04NULLcompleted39.9
51891132026-07-094cancelled19.9
51901142026-07-14NULLcompleted49.9
5191832026-07-15NULLcompleted148.7
5192762026-07-17NULLcompleted34.9
5193842026-07-17NULLcompleted34.9
51941152026-07-19NULLcompleted79.9
5195742026-07-233completed33.8
5196922026-07-2712completed19.9
51971162026-07-2712completed27.9
51981172026-07-28NULLcompleted99.9
5199672026-07-303completed29.9
52001182026-08-03NULLcompleted119
52011192026-08-073completed102.8
52021202026-08-09NULLcompleted89.9
5203932026-08-153completed47.8
52041212026-08-15NULLcompleted27.9
52051222026-08-18NULLcompleted44.9
52061022026-08-243completed89.8
52071232026-08-2512completed142.7
52081242026-08-2712completed113.7
5209952026-08-2812completed99.9
5210922026-08-313completed29.8
52111252026-08-313completed19.9
52121102026-09-01NULLcompleted42.7
52131262026-09-033completed29.9
52141272026-09-07NULLcompleted29.8
52151282026-09-13NULLcompleted24.9
52161292026-09-1913completed89.9
52171302026-09-20NULLcompleted19.9
52181172026-09-213completed96.8
52191312026-09-223completed89.9
5220502026-09-23NULLcompleted44.9
5221912026-09-23NULLcompleted89.9
5222922026-09-233completed89.9
52231322026-09-2612completed89.9
52241152026-10-03NULLcompleted25.8
52251332026-10-0413cancelled49.9
5226632026-10-05NULLcompleted24.9
52271342026-10-0513completed27.9
52281062026-10-08NULLcompleted29.9
52291192026-10-08NULLcompleted24.9
52301352026-10-1012completed24.9
5231952026-10-13NULLcompleted24.9
52321022026-10-13NULLcompleted49.9
5233982026-10-14NULLcompleted14.9
52341362026-10-164completed24.9
52351372026-10-203completed34.9
52361382026-10-2213completed34.9
52371392026-10-27NULLcompleted89.8
52381402026-10-293completed19.9
52391122026-10-31NULLcompleted104.8

How it works

The CTE counts each customer's completed orders. Join it to customers to get the channel. Now every row is a customer, so COUNT(*) counts customers. SUM(CASE WHEN n >= 2 THEN 1 ELSE 0 END) counts the repeat buyers; divided by all customers and multiplied by 100.0, that is the repeat rate.

The hunch holds up: Paid Social sits at 17.6%, Email at 55.6%. But careful, this number is not fair. A customer from October 2026 has had almost no time to buy again; one from January 2025 has had nearly two years.

Task 2: cohorts with a 90-day window

Task Cohort retention: group customers by the month of their first completed order (cohort in the format YYYY-MM). For the cohorts from 2025-07 to 2026-06, show for each cohort the number of customers and how many of them placed another completed order within 90 days of their first one (within_90_days; an order at most 90 days after the first counts).

orders 239 rows, 6 columns
order_idcustomer_idorder_datecampaign_idstatusamount
500112025-01-161completed29.8
500222025-01-24NULLcompleted139.7
500332025-01-28NULLcompleted49.9
500442025-02-014completed69.8
500552025-02-073completed89.9
500662025-02-14NULLcompleted24.9
500772025-02-25NULLcancelled24.9
500882025-03-032completed139.7
500992025-03-062completed34.9
501062025-03-13NULLcompleted24.9
5011102025-03-152completed19.9
5012112025-03-17NULLcompleted49.9
5013122025-03-293cancelled89.9
5014132025-03-304completed24.9
501522025-04-033completed24.9
5016142025-04-054completed79.9
501752025-04-063completed34.9
5018102025-04-10NULLcompleted89.9
5019152025-04-123completed114.8
5020162025-04-193completed99.9
5021172025-04-26NULLcancelled79.9
502272025-04-30NULLcompleted29.9
5023182025-05-03NULLcompleted34.9
5024192025-05-08NULLcompleted89.9
5025202025-05-123completed19.9
5026212025-05-172cancelled39.9
5027222025-05-18NULLcompleted79.9
5028142025-05-241completed89.9
5029232025-05-251completed29.8
5030162025-06-013cancelled27.9
5031242025-06-01NULLcompleted19.9
5032252025-06-093completed79.9
5033262025-06-156cancelled34.9
5034272025-06-163completed24.9
5035282025-06-186completed29.8
5036292025-06-256completed19.9
5037302025-07-06NULLcompleted19.8
5038312025-07-085completed24.9
5039322025-07-105completed119.7
5040332025-07-146completed49.9
5041342025-07-246completed34.9
5042352025-07-286cancelled89.9
5043242025-07-293completed34.9
5044362025-08-016completed104.8
504552025-08-053completed29.9
5046372025-08-06NULLcancelled119
504722025-08-073completed89.9
5048382025-08-09NULLcompleted9.9
5049392025-08-146completed19.9
5050402025-08-194completed89.9
5051412025-08-29NULLcompleted89.9
5052422025-08-293completed12.9
5053432025-09-03NULLcompleted24.9
5054442025-09-065completed34.9
5055232025-09-075completed99.9
5056382025-09-123completed25.8
5057452025-09-15NULLcompleted19.9
5058462025-09-185completed99.9
5059412025-09-19NULLcompleted34.9
5060472025-09-19NULLcompleted9.9
506152025-09-24NULLcompleted24.9
5062482025-09-29NULLcompleted79.9
5063492025-09-295completed29.9
5064502025-10-08NULLcompleted129
5065142025-10-10NULLcompleted86.7
5066512025-10-115completed119.7
5067162025-10-125completed99.9
5068522025-10-145completed119.7
5069342025-10-19NULLcompleted54.8
5070212025-10-20NULLcompleted49.9
5071422025-10-205completed16.9
5072532025-10-213completed89.9
5073542025-10-24NULLcompleted54.8
5074552025-10-295completed99.9
5075562025-11-04NULLcompleted34.9
5076572025-11-055completed114.8
5077272025-11-073completed12.9
5078312025-11-07NULLcompleted59.9
5079322025-11-07NULLcompleted27.9
5080582025-11-18NULLcompleted19.9
5081592025-11-235completed89.9
5082602025-11-238completed25.8
5083122025-11-247completed47.92
5084152025-11-248completed91.84
5085482025-11-247completed55.76
5086512025-11-247completed23.92
508712025-11-257completed31.92
508872025-11-25NULLcompleted47.92
5089612025-11-253completed23.92
509062025-11-267completed39.92
5091312025-11-268completed23.92
5092562025-11-268completed10.32
5093132025-11-277cancelled91.84
5094302025-11-277completed31.92
5095382025-11-287completed103.2
5096552025-11-288completed63.92
509792025-11-298completed27.92
509852025-11-308completed15.84
5099422025-11-303completed7.92
5100442025-11-307completed48.56
5101462025-11-307completed38.24
5102472025-11-307completed31.92
5103242025-12-017completed90.24
5104622025-12-018completed15.92
5105632025-12-08NULLcompleted19.8
5106642025-12-11NULLcompleted114.8
5107142025-12-12NULLcompleted29.9
5108652025-12-155completed89.7
5109662025-12-195completed104.8
5110672025-12-25NULLcompleted34.9
5111682025-12-26NULLcompleted59.9
5112422026-01-04NULLcompleted79.9
5113692026-01-0610completed16.9
5114702026-01-063completed19.9
511522026-01-099completed89.9
5116712026-01-169completed25.8
5117722026-01-179completed89.9
5118502026-01-18NULLcompleted19.9
5119732026-01-2310completed59.9
5120742026-01-26NULLcompleted19.8
5121752026-02-029completed119
5122762026-02-064completed24.9
5123772026-02-07NULLcompleted79.9
5124782026-02-13NULLcompleted9.9
5125792026-02-15NULLcompleted104.8
5126742026-02-18NULLcompleted49.9
5127802026-02-204completed19.9
5128682026-02-24NULLcompleted12.9
5129812026-02-259completed29.9
5130522026-02-28NULLcompleted59.9
5131382026-03-013completed89.9
5132822026-03-01NULLcompleted19.9
513342026-03-02NULLcompleted29.8
5134672026-03-0311completed39.9
513522026-03-059completed44.9
5136172026-03-05NULLcompleted79.9
5137312026-03-06NULLcompleted136.7
5138832026-03-07NULLcompleted49.9
5139702026-03-0811completed47.8
5140842026-03-113completed49.8
514172026-03-12NULLcompleted19.9
5142532026-03-1211completed19.9
5143852026-03-124completed22.8
514452026-03-13NULLcancelled104.8
5145162026-03-149completed34.9
5146572026-03-15NULLcompleted107.8
5147142026-03-1611completed24.9
5148322026-03-1611completed34.9
5149542026-03-17NULLcompleted34.9
5150862026-03-17NULLcompleted25.8
5151272026-03-18NULLcompleted9.9
5152872026-03-199completed119
5153722026-03-2211completed24.9
5154882026-04-014completed29.9
5155892026-04-059completed49.9
5156902026-04-059completed102.8
5157742026-04-063completed24.9
5158912026-04-119completed29.9
5159612026-04-12NULLcompleted79.9
5160922026-04-14NULLcompleted29.9
5161932026-04-19NULLcompleted29.9
5162942026-04-249completed29.9
5163952026-04-284completed44.9
5164962026-04-309completed129
5165922026-05-07NULLcompleted55.7
5166972026-05-104completed54.8
5167982026-05-119completed14.9
5168992026-05-134completed34.9
51691002026-05-189completed129
51701012026-05-194completed148.7
5171632026-05-233cancelled24.9
51721022026-05-25NULLcompleted44.9
5173782026-06-01NULLcancelled153.9
51741032026-06-01NULLcompleted99.9
5175422026-06-029completed129
51761042026-06-04NULLcompleted49.9
51771052026-06-08NULLcompleted19.9
5178502026-06-09NULLcompleted102.8
51791062026-06-09NULLcompleted119.7
51801072026-06-164cancelled29.7
51811082026-06-229completed12.9
5182792026-06-233completed99.9
51831092026-06-264completed129
51841102026-06-26NULLcancelled24.9
5185812026-06-28NULLcompleted99.9
51861112026-07-02NULLcompleted158.9
5187772026-07-04NULLcompleted114.7
51881122026-07-04NULLcompleted39.9
51891132026-07-094cancelled19.9
51901142026-07-14NULLcompleted49.9
5191832026-07-15NULLcompleted148.7
5192762026-07-17NULLcompleted34.9
5193842026-07-17NULLcompleted34.9
51941152026-07-19NULLcompleted79.9
5195742026-07-233completed33.8
5196922026-07-2712completed19.9
51971162026-07-2712completed27.9
51981172026-07-28NULLcompleted99.9
5199672026-07-303completed29.9
52001182026-08-03NULLcompleted119
52011192026-08-073completed102.8
52021202026-08-09NULLcompleted89.9
5203932026-08-153completed47.8
52041212026-08-15NULLcompleted27.9
52051222026-08-18NULLcompleted44.9
52061022026-08-243completed89.8
52071232026-08-2512completed142.7
52081242026-08-2712completed113.7
5209952026-08-2812completed99.9
5210922026-08-313completed29.8
52111252026-08-313completed19.9
52121102026-09-01NULLcompleted42.7
52131262026-09-033completed29.9
52141272026-09-07NULLcompleted29.8
52151282026-09-13NULLcompleted24.9
52161292026-09-1913completed89.9
52171302026-09-20NULLcompleted19.9
52181172026-09-213completed96.8
52191312026-09-223completed89.9
5220502026-09-23NULLcompleted44.9
5221912026-09-23NULLcompleted89.9
5222922026-09-233completed89.9
52231322026-09-2612completed89.9
52241152026-10-03NULLcompleted25.8
52251332026-10-0413cancelled49.9
5226632026-10-05NULLcompleted24.9
52271342026-10-0513completed27.9
52281062026-10-08NULLcompleted29.9
52291192026-10-08NULLcompleted24.9
52301352026-10-1012completed24.9
5231952026-10-13NULLcompleted24.9
52321022026-10-13NULLcompleted49.9
5233982026-10-14NULLcompleted14.9
52341362026-10-164completed24.9
52351372026-10-203completed34.9
52361382026-10-2213completed34.9
52371392026-10-27NULLcompleted89.8
52381402026-10-293completed19.9
52391122026-10-31NULLcompleted104.8

This exercise is part of Premium.

Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.

See Premium Already Premium? Log in

The first exercise in this lesson is free.

How it works

A cohort is everyone who bought for the first time in the same month. Every customer gets the same window: 90 days after their first order. When they became a customer no longer matters. The CTE returns MIN(order_date) per customer, and substr(first_order, 1, 7) turns it into a YYYY-MM month.

For the window, date(first_order, '+90 days') works out the end date (see the date functions lesson). EXISTS then asks one thing per customer: is there another completed order after the first one and no more than 90 days later? The answer is true or false, and the CASE turns it into 1 or 0. The cohorts stop at June 2026 because the June customers' window closes at the end of September; later cohorts have not been watched long enough.

The result shows something the repeat rate hid. In the August and September 2025 and January 2026 cohorts, half or more bought again within 90 days. The February, March, May and June 2026 cohorts have no repeat purchase in that window at all. With five to nine customers per cohort these are small numbers. Treat them as a first signal for the CRM team, not as proof.

Common mistakes

Greater-or-equal instead of greater

With o.order_date >= f.first_order the check finds the first order itself, and every customer looks like a repeat buyer. The exercise says: "Columns and row count match, but not every value."

JOIN without DISTINCT instead of EXISTS

A LEFT JOIN to the follow-up orders with COUNT(o.customer_id) counts a customer with two follow-up orders twice. COUNT(DISTINCT o.customer_id) fixes that; EXISTS never has the problem.

Signup date instead of the first completed order

signup_date is the day of the first order, even if that order was cancelled. Using it puts customers whose start was a cancellation into the wrong cohort.

Including every cohort

Without the June 2026 limit the query returns 22 rows instead of 12, and the newest cohorts look worse than they are.

Check your result

  • Task 1: 134 customers and 57 repeat buyers in total. Organic has 53 customers, 26 repeat buyers, 49.1%.
  • Task 2: 12 cohorts with 80 customers between them. 2025-09: 7 customers, 4 of them bought again. 2026-04: 9 customers, 1 bought again.

In MySQL, PostgreSQL and SQL Server

The end of the window is DATE_ADD(first_order, INTERVAL 90 DAY) in MySQL, DATEADD(day, 90, first_order) in SQL Server and first_order + interval '90 days' in PostgreSQL. For the gap in days, MySQL has DATEDIFF(later, earlier) and SQL Server DATEDIFF(day, earlier, later), with the arguments the other way round. In PostgreSQL, date - date returns the number of days directly.

FAQ

Why 90 days?

The right window depends on what you sell. Consumables call for shorter windows, bikes for longer ones. What matters is that every cohort gets the same window. In the query you only change one number.

How is this different from churn rate?

Churn rate counts how many active customers drop off in a period. It suits subscriptions and regular orders. In online retail without subscriptions, "does the customer come back?" is usually the more useful question.

Can I show the cohorts as a matrix?

Yes. Instead of one column for 90 days, build several, for example 30, 60 and 90 days, each with its own CASE WHEN EXISTS. The pattern stays the same.

Next up, case 4: RFM segments

How long since the last purchase, how often, how much? From three numbers per customer you build scores and segments.

Start case 4

Write to us

Choose language

Select your preferred language