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;
| orders | customers |
|---|---|
| 1 | 77 |
| 2 | 39 |
| 3 | 9 |
| 4 | 4 |
| 5 | 5 |
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_id | name | city | signup_date | channel | campaign_id |
|---|---|---|---|---|---|
| 1 | Sophia Campbell | Leeds | 2025-01-16 | Paid Search | 1 |
| 2 | Aiden Allen | Leeds | 2025-01-24 | Organic | NULL |
| 3 | Aiden Miller | Austin | 2025-01-28 | Organic | NULL |
| 4 | Isla Wright | Glasgow | 2025-02-01 | Affiliate | 4 |
| 5 | Henry Garcia | Glasgow | 2025-02-07 | 3 | |
| 6 | Ryan Thomas | Portland | 2025-02-14 | Organic | NULL |
| 7 | Priya Murphy | Bristol | 2025-02-25 | Organic | NULL |
| 8 | Chloe Wilson | London | 2025-03-03 | Paid Social | 2 |
| 9 | Amelia Evans | Manchester | 2025-03-06 | Paid Social | 2 |
| 10 | Jack Wilson | Seattle | 2025-03-15 | Paid Social | 2 |
| 11 | Grace Campbell | Austin | 2025-03-17 | Organic | NULL |
| 12 | Ava Lewis | Austin | 2025-03-29 | 3 | |
| 13 | Ruby Murphy | Leeds | 2025-03-30 | Affiliate | 4 |
| 14 | Samuel Khan | Atlanta | 2025-04-05 | Affiliate | 4 |
| 15 | Ruby Miller | Leeds | 2025-04-12 | 3 | |
| 16 | Noah Thomas | London | 2025-04-19 | 3 | |
| 17 | Leo Davies | Denver | 2025-04-26 | Organic | NULL |
| 18 | Leo Wright | Chicago | 2025-05-03 | Organic | NULL |
| 19 | Mia Taylor | Glasgow | 2025-05-08 | Organic | NULL |
| 20 | Liam Robinson | Seattle | 2025-05-12 | 3 | |
| 21 | Ella Evans | Denver | 2025-05-17 | Paid Social | 2 |
| 22 | Daniel Scott | Boston | 2025-05-18 | Organic | NULL |
| 23 | Amelia Wright | Manchester | 2025-05-25 | Paid Search | 1 |
| 24 | Freya Davies | Atlanta | 2025-06-01 | Organic | NULL |
| 25 | Noah Taylor | Boston | 2025-06-09 | 3 | |
| 26 | Ava Miller | Seattle | 2025-06-15 | Paid Social | 6 |
| 27 | Lucas Wilson | Austin | 2025-06-16 | 3 | |
| 28 | Liam Khan | Portland | 2025-06-18 | Paid Social | 6 |
| 29 | Lucas Wright | Glasgow | 2025-06-25 | Paid Social | 6 |
| 30 | Noah Murphy | Denver | 2025-07-06 | Organic | NULL |
| 31 | Grace Khan | Bristol | 2025-07-08 | Paid Search | 5 |
| 32 | Liam Davies | Seattle | 2025-07-10 | Paid Search | 5 |
| 33 | Mason Hall | Seattle | 2025-07-14 | Paid Social | 6 |
| 34 | Ethan Khan | Portland | 2025-07-24 | Paid Social | 6 |
| 35 | Jack Clarke | Leeds | 2025-07-28 | Paid Social | 6 |
| 36 | Ethan Campbell | Seattle | 2025-08-01 | Paid Social | 6 |
| 37 | Harper King | Boston | 2025-08-06 | Organic | NULL |
| 38 | Freya Scott | Manchester | 2025-08-09 | Organic | NULL |
| 39 | Daniel Johnson | Manchester | 2025-08-14 | Paid Social | 6 |
| 40 | Ruby Wilson | Atlanta | 2025-08-19 | Affiliate | 4 |
| 41 | Zoe Khan | Atlanta | 2025-08-29 | Organic | NULL |
| 42 | Oliver Young | Austin | 2025-08-29 | 3 | |
| 43 | Leo King | Glasgow | 2025-09-03 | Organic | NULL |
| 44 | Noah Allen | Bristol | 2025-09-06 | Paid Search | 5 |
| 45 | Oliver Lewis | Austin | 2025-09-15 | Organic | NULL |
| 46 | James Walker | Austin | 2025-09-18 | Paid Search | 5 |
| 47 | Ava Davies | Seattle | 2025-09-19 | Organic | NULL |
| 48 | Leo Allen | London | 2025-09-29 | Organic | NULL |
| 49 | James Hall | Seattle | 2025-09-29 | Paid Search | 5 |
| 50 | Aiden Thomas | Boston | 2025-10-08 | Organic | NULL |
| 51 | Oliver Allen | Austin | 2025-10-11 | Paid Search | 5 |
| 52 | Daniel Clarke | Glasgow | 2025-10-14 | Paid Search | 5 |
| 53 | Liam Harris | Seattle | 2025-10-21 | 3 | |
| 54 | Lucas Campbell | Manchester | 2025-10-24 | Organic | NULL |
| 55 | Freya Brown | London | 2025-10-29 | Paid Search | 5 |
| 56 | Freya Young | Seattle | 2025-11-04 | Organic | NULL |
| 57 | Sophia Lewis | Portland | 2025-11-05 | Paid Search | 5 |
| 58 | Freya Evans | Bristol | 2025-11-18 | Organic | NULL |
| 59 | Leo Patel | London | 2025-11-23 | Paid Search | 5 |
| 60 | Ruby Thomas | Glasgow | 2025-11-23 | Paid Social | 8 |
| 61 | Amelia Taylor | Manchester | 2025-11-25 | 3 | |
| 62 | Liam Murphy | Denver | 2025-12-01 | Paid Social | 8 |
| 63 | Daniel Walker | Chicago | 2025-12-08 | Organic | NULL |
| 64 | Zoe King | Manchester | 2025-12-11 | Organic | NULL |
| 65 | James Clarke | Leeds | 2025-12-15 | Paid Search | 5 |
| 66 | Samuel King | Glasgow | 2025-12-19 | Paid Search | 5 |
| 67 | Leo Miller | Denver | 2025-12-25 | Organic | NULL |
| 68 | Sophia Evans | Portland | 2025-12-26 | Organic | NULL |
| 69 | Sophia Murphy | Austin | 2026-01-06 | Paid Social | 10 |
| 70 | Olivia Khan | Austin | 2026-01-06 | 3 | |
| 71 | James Scott | Leeds | 2026-01-16 | Paid Search | 9 |
| 72 | Noah Smith | Chicago | 2026-01-17 | Paid Search | 9 |
| 73 | Liam Scott | Leeds | 2026-01-23 | Paid Social | 10 |
| 74 | Priya Scott | Portland | 2026-01-26 | Organic | NULL |
| 75 | Jack Davies | Austin | 2026-02-02 | Paid Search | 9 |
| 76 | Priya Campbell | Glasgow | 2026-02-06 | Affiliate | 4 |
| 77 | Olivia Allen | Seattle | 2026-02-07 | Organic | NULL |
| 78 | Lucas Young | Austin | 2026-02-13 | Organic | NULL |
| 79 | Emma Garcia | Bristol | 2026-02-15 | Organic | NULL |
| 80 | Grace Brown | London | 2026-02-20 | Affiliate | 4 |
| 81 | Zoe Taylor | Seattle | 2026-02-25 | Paid Search | 9 |
| 82 | Ava Brown | Manchester | 2026-03-01 | Organic | NULL |
| 83 | Ryan Wilson | Seattle | 2026-03-07 | Organic | NULL |
| 84 | Liam Wright | Austin | 2026-03-11 | 3 | |
| 85 | Olivia Taylor | Seattle | 2026-03-12 | Affiliate | 4 |
| 86 | Ethan Evans | Boston | 2026-03-17 | Organic | NULL |
| 87 | Ella Scott | Boston | 2026-03-19 | Paid Search | 9 |
| 88 | Daniel Hall | Glasgow | 2026-04-01 | Affiliate | 4 |
| 89 | Chloe Thomas | Seattle | 2026-04-05 | Paid Search | 9 |
| 90 | Harper Taylor | Denver | 2026-04-05 | Paid Search | 9 |
| 91 | Ethan King | Austin | 2026-04-11 | Paid Search | 9 |
| 92 | Leo Campbell | Austin | 2026-04-14 | Organic | NULL |
| 93 | Noah Young | Atlanta | 2026-04-19 | Organic | NULL |
| 94 | Noah Johnson | Denver | 2026-04-24 | Paid Search | 9 |
| 95 | Daniel Evans | Glasgow | 2026-04-28 | Affiliate | 4 |
| 96 | Ella Wilson | London | 2026-04-30 | Paid Search | 9 |
| 97 | Grace Allen | Bristol | 2026-05-10 | Affiliate | 4 |
| 98 | Jack Smith | Bristol | 2026-05-11 | Paid Search | 9 |
| 99 | Grace Evans | Portland | 2026-05-13 | Affiliate | 4 |
| 100 | Isla Clarke | London | 2026-05-18 | Paid Search | 9 |
| 101 | Daniel Allen | Seattle | 2026-05-19 | Affiliate | 4 |
| 102 | Zoe Brown | London | 2026-05-25 | Organic | NULL |
| 103 | Daniel Lewis | Atlanta | 2026-06-01 | Organic | NULL |
| 104 | Isla Miller | Seattle | 2026-06-04 | Organic | NULL |
| 105 | Daniel Wright | Manchester | 2026-06-08 | Organic | NULL |
| 106 | Zoe Wright | Glasgow | 2026-06-09 | Organic | NULL |
| 107 | Chloe Walker | Austin | 2026-06-16 | Affiliate | 4 |
| 108 | Chloe Scott | Austin | 2026-06-22 | Paid Search | 9 |
| 109 | Henry Evans | Glasgow | 2026-06-26 | Affiliate | 4 |
| 110 | Ryan Scott | Glasgow | 2026-06-26 | Organic | NULL |
| 111 | Ruby Garcia | Boston | 2026-07-02 | Organic | NULL |
| 112 | Ryan Young | Manchester | 2026-07-04 | Organic | NULL |
| 113 | Priya Walker | Denver | 2026-07-09 | Affiliate | 4 |
| 114 | Chloe Hall | Chicago | 2026-07-14 | Organic | NULL |
| 115 | Jack Campbell | Manchester | 2026-07-19 | Organic | NULL |
| 116 | Harper Scott | Manchester | 2026-07-27 | Paid Search | 12 |
| 117 | Freya Wright | Chicago | 2026-07-28 | Organic | NULL |
| 118 | Jack Hall | Glasgow | 2026-08-03 | Organic | NULL |
| 119 | Chloe Miller | Manchester | 2026-08-07 | 3 | |
| 120 | Oliver Garcia | Boston | 2026-08-09 | Organic | NULL |
| 121 | Daniel King | Portland | 2026-08-15 | Organic | NULL |
| 122 | Ruby Harris | Chicago | 2026-08-18 | Organic | NULL |
| 123 | Ruby Lewis | Portland | 2026-08-25 | Paid Search | 12 |
| 124 | Henry Harris | Manchester | 2026-08-27 | Paid Search | 12 |
| 125 | Ethan Smith | Bristol | 2026-08-31 | 3 | |
| 126 | Emma Lewis | Bristol | 2026-09-03 | 3 | |
| 127 | Zoe Evans | Bristol | 2026-09-07 | Organic | NULL |
| 128 | Ethan Johnson | Bristol | 2026-09-13 | Organic | NULL |
| 129 | Ella Lewis | Denver | 2026-09-19 | Paid Social | 13 |
| 130 | Olivia Robinson | Seattle | 2026-09-20 | Organic | NULL |
| 131 | Amelia King | Boston | 2026-09-22 | 3 | |
| 132 | Oliver Taylor | Glasgow | 2026-09-26 | Paid Search | 12 |
| 133 | James Allen | Seattle | 2026-10-04 | Paid Social | 13 |
| 134 | Henry Khan | Atlanta | 2026-10-05 | Paid Social | 13 |
| 135 | Mia Wright | Manchester | 2026-10-10 | Paid Search | 12 |
| 136 | Mia Johnson | London | 2026-10-16 | Affiliate | 4 |
| 137 | Grace Davies | Leeds | 2026-10-20 | 3 | |
| 138 | Liam Patel | Bristol | 2026-10-22 | Paid Social | 13 |
| 139 | Oliver Wright | Seattle | 2026-10-27 | Organic | NULL |
| 140 | Henry Allen | Seattle | 2026-10-29 | 3 |
orders 239 rows, 6 columns
| order_id | customer_id | order_date | campaign_id | status | amount |
|---|---|---|---|---|---|
| 5001 | 1 | 2025-01-16 | 1 | completed | 29.8 |
| 5002 | 2 | 2025-01-24 | NULL | completed | 139.7 |
| 5003 | 3 | 2025-01-28 | NULL | completed | 49.9 |
| 5004 | 4 | 2025-02-01 | 4 | completed | 69.8 |
| 5005 | 5 | 2025-02-07 | 3 | completed | 89.9 |
| 5006 | 6 | 2025-02-14 | NULL | completed | 24.9 |
| 5007 | 7 | 2025-02-25 | NULL | cancelled | 24.9 |
| 5008 | 8 | 2025-03-03 | 2 | completed | 139.7 |
| 5009 | 9 | 2025-03-06 | 2 | completed | 34.9 |
| 5010 | 6 | 2025-03-13 | NULL | completed | 24.9 |
| 5011 | 10 | 2025-03-15 | 2 | completed | 19.9 |
| 5012 | 11 | 2025-03-17 | NULL | completed | 49.9 |
| 5013 | 12 | 2025-03-29 | 3 | cancelled | 89.9 |
| 5014 | 13 | 2025-03-30 | 4 | completed | 24.9 |
| 5015 | 2 | 2025-04-03 | 3 | completed | 24.9 |
| 5016 | 14 | 2025-04-05 | 4 | completed | 79.9 |
| 5017 | 5 | 2025-04-06 | 3 | completed | 34.9 |
| 5018 | 10 | 2025-04-10 | NULL | completed | 89.9 |
| 5019 | 15 | 2025-04-12 | 3 | completed | 114.8 |
| 5020 | 16 | 2025-04-19 | 3 | completed | 99.9 |
| 5021 | 17 | 2025-04-26 | NULL | cancelled | 79.9 |
| 5022 | 7 | 2025-04-30 | NULL | completed | 29.9 |
| 5023 | 18 | 2025-05-03 | NULL | completed | 34.9 |
| 5024 | 19 | 2025-05-08 | NULL | completed | 89.9 |
| 5025 | 20 | 2025-05-12 | 3 | completed | 19.9 |
| 5026 | 21 | 2025-05-17 | 2 | cancelled | 39.9 |
| 5027 | 22 | 2025-05-18 | NULL | completed | 79.9 |
| 5028 | 14 | 2025-05-24 | 1 | completed | 89.9 |
| 5029 | 23 | 2025-05-25 | 1 | completed | 29.8 |
| 5030 | 16 | 2025-06-01 | 3 | cancelled | 27.9 |
| 5031 | 24 | 2025-06-01 | NULL | completed | 19.9 |
| 5032 | 25 | 2025-06-09 | 3 | completed | 79.9 |
| 5033 | 26 | 2025-06-15 | 6 | cancelled | 34.9 |
| 5034 | 27 | 2025-06-16 | 3 | completed | 24.9 |
| 5035 | 28 | 2025-06-18 | 6 | completed | 29.8 |
| 5036 | 29 | 2025-06-25 | 6 | completed | 19.9 |
| 5037 | 30 | 2025-07-06 | NULL | completed | 19.8 |
| 5038 | 31 | 2025-07-08 | 5 | completed | 24.9 |
| 5039 | 32 | 2025-07-10 | 5 | completed | 119.7 |
| 5040 | 33 | 2025-07-14 | 6 | completed | 49.9 |
| 5041 | 34 | 2025-07-24 | 6 | completed | 34.9 |
| 5042 | 35 | 2025-07-28 | 6 | cancelled | 89.9 |
| 5043 | 24 | 2025-07-29 | 3 | completed | 34.9 |
| 5044 | 36 | 2025-08-01 | 6 | completed | 104.8 |
| 5045 | 5 | 2025-08-05 | 3 | completed | 29.9 |
| 5046 | 37 | 2025-08-06 | NULL | cancelled | 119 |
| 5047 | 2 | 2025-08-07 | 3 | completed | 89.9 |
| 5048 | 38 | 2025-08-09 | NULL | completed | 9.9 |
| 5049 | 39 | 2025-08-14 | 6 | completed | 19.9 |
| 5050 | 40 | 2025-08-19 | 4 | completed | 89.9 |
| 5051 | 41 | 2025-08-29 | NULL | completed | 89.9 |
| 5052 | 42 | 2025-08-29 | 3 | completed | 12.9 |
| 5053 | 43 | 2025-09-03 | NULL | completed | 24.9 |
| 5054 | 44 | 2025-09-06 | 5 | completed | 34.9 |
| 5055 | 23 | 2025-09-07 | 5 | completed | 99.9 |
| 5056 | 38 | 2025-09-12 | 3 | completed | 25.8 |
| 5057 | 45 | 2025-09-15 | NULL | completed | 19.9 |
| 5058 | 46 | 2025-09-18 | 5 | completed | 99.9 |
| 5059 | 41 | 2025-09-19 | NULL | completed | 34.9 |
| 5060 | 47 | 2025-09-19 | NULL | completed | 9.9 |
| 5061 | 5 | 2025-09-24 | NULL | completed | 24.9 |
| 5062 | 48 | 2025-09-29 | NULL | completed | 79.9 |
| 5063 | 49 | 2025-09-29 | 5 | completed | 29.9 |
| 5064 | 50 | 2025-10-08 | NULL | completed | 129 |
| 5065 | 14 | 2025-10-10 | NULL | completed | 86.7 |
| 5066 | 51 | 2025-10-11 | 5 | completed | 119.7 |
| 5067 | 16 | 2025-10-12 | 5 | completed | 99.9 |
| 5068 | 52 | 2025-10-14 | 5 | completed | 119.7 |
| 5069 | 34 | 2025-10-19 | NULL | completed | 54.8 |
| 5070 | 21 | 2025-10-20 | NULL | completed | 49.9 |
| 5071 | 42 | 2025-10-20 | 5 | completed | 16.9 |
| 5072 | 53 | 2025-10-21 | 3 | completed | 89.9 |
| 5073 | 54 | 2025-10-24 | NULL | completed | 54.8 |
| 5074 | 55 | 2025-10-29 | 5 | completed | 99.9 |
| 5075 | 56 | 2025-11-04 | NULL | completed | 34.9 |
| 5076 | 57 | 2025-11-05 | 5 | completed | 114.8 |
| 5077 | 27 | 2025-11-07 | 3 | completed | 12.9 |
| 5078 | 31 | 2025-11-07 | NULL | completed | 59.9 |
| 5079 | 32 | 2025-11-07 | NULL | completed | 27.9 |
| 5080 | 58 | 2025-11-18 | NULL | completed | 19.9 |
| 5081 | 59 | 2025-11-23 | 5 | completed | 89.9 |
| 5082 | 60 | 2025-11-23 | 8 | completed | 25.8 |
| 5083 | 12 | 2025-11-24 | 7 | completed | 47.92 |
| 5084 | 15 | 2025-11-24 | 8 | completed | 91.84 |
| 5085 | 48 | 2025-11-24 | 7 | completed | 55.76 |
| 5086 | 51 | 2025-11-24 | 7 | completed | 23.92 |
| 5087 | 1 | 2025-11-25 | 7 | completed | 31.92 |
| 5088 | 7 | 2025-11-25 | NULL | completed | 47.92 |
| 5089 | 61 | 2025-11-25 | 3 | completed | 23.92 |
| 5090 | 6 | 2025-11-26 | 7 | completed | 39.92 |
| 5091 | 31 | 2025-11-26 | 8 | completed | 23.92 |
| 5092 | 56 | 2025-11-26 | 8 | completed | 10.32 |
| 5093 | 13 | 2025-11-27 | 7 | cancelled | 91.84 |
| 5094 | 30 | 2025-11-27 | 7 | completed | 31.92 |
| 5095 | 38 | 2025-11-28 | 7 | completed | 103.2 |
| 5096 | 55 | 2025-11-28 | 8 | completed | 63.92 |
| 5097 | 9 | 2025-11-29 | 8 | completed | 27.92 |
| 5098 | 5 | 2025-11-30 | 8 | completed | 15.84 |
| 5099 | 42 | 2025-11-30 | 3 | completed | 7.92 |
| 5100 | 44 | 2025-11-30 | 7 | completed | 48.56 |
| 5101 | 46 | 2025-11-30 | 7 | completed | 38.24 |
| 5102 | 47 | 2025-11-30 | 7 | completed | 31.92 |
| 5103 | 24 | 2025-12-01 | 7 | completed | 90.24 |
| 5104 | 62 | 2025-12-01 | 8 | completed | 15.92 |
| 5105 | 63 | 2025-12-08 | NULL | completed | 19.8 |
| 5106 | 64 | 2025-12-11 | NULL | completed | 114.8 |
| 5107 | 14 | 2025-12-12 | NULL | completed | 29.9 |
| 5108 | 65 | 2025-12-15 | 5 | completed | 89.7 |
| 5109 | 66 | 2025-12-19 | 5 | completed | 104.8 |
| 5110 | 67 | 2025-12-25 | NULL | completed | 34.9 |
| 5111 | 68 | 2025-12-26 | NULL | completed | 59.9 |
| 5112 | 42 | 2026-01-04 | NULL | completed | 79.9 |
| 5113 | 69 | 2026-01-06 | 10 | completed | 16.9 |
| 5114 | 70 | 2026-01-06 | 3 | completed | 19.9 |
| 5115 | 2 | 2026-01-09 | 9 | completed | 89.9 |
| 5116 | 71 | 2026-01-16 | 9 | completed | 25.8 |
| 5117 | 72 | 2026-01-17 | 9 | completed | 89.9 |
| 5118 | 50 | 2026-01-18 | NULL | completed | 19.9 |
| 5119 | 73 | 2026-01-23 | 10 | completed | 59.9 |
| 5120 | 74 | 2026-01-26 | NULL | completed | 19.8 |
| 5121 | 75 | 2026-02-02 | 9 | completed | 119 |
| 5122 | 76 | 2026-02-06 | 4 | completed | 24.9 |
| 5123 | 77 | 2026-02-07 | NULL | completed | 79.9 |
| 5124 | 78 | 2026-02-13 | NULL | completed | 9.9 |
| 5125 | 79 | 2026-02-15 | NULL | completed | 104.8 |
| 5126 | 74 | 2026-02-18 | NULL | completed | 49.9 |
| 5127 | 80 | 2026-02-20 | 4 | completed | 19.9 |
| 5128 | 68 | 2026-02-24 | NULL | completed | 12.9 |
| 5129 | 81 | 2026-02-25 | 9 | completed | 29.9 |
| 5130 | 52 | 2026-02-28 | NULL | completed | 59.9 |
| 5131 | 38 | 2026-03-01 | 3 | completed | 89.9 |
| 5132 | 82 | 2026-03-01 | NULL | completed | 19.9 |
| 5133 | 4 | 2026-03-02 | NULL | completed | 29.8 |
| 5134 | 67 | 2026-03-03 | 11 | completed | 39.9 |
| 5135 | 2 | 2026-03-05 | 9 | completed | 44.9 |
| 5136 | 17 | 2026-03-05 | NULL | completed | 79.9 |
| 5137 | 31 | 2026-03-06 | NULL | completed | 136.7 |
| 5138 | 83 | 2026-03-07 | NULL | completed | 49.9 |
| 5139 | 70 | 2026-03-08 | 11 | completed | 47.8 |
| 5140 | 84 | 2026-03-11 | 3 | completed | 49.8 |
| 5141 | 7 | 2026-03-12 | NULL | completed | 19.9 |
| 5142 | 53 | 2026-03-12 | 11 | completed | 19.9 |
| 5143 | 85 | 2026-03-12 | 4 | completed | 22.8 |
| 5144 | 5 | 2026-03-13 | NULL | cancelled | 104.8 |
| 5145 | 16 | 2026-03-14 | 9 | completed | 34.9 |
| 5146 | 57 | 2026-03-15 | NULL | completed | 107.8 |
| 5147 | 14 | 2026-03-16 | 11 | completed | 24.9 |
| 5148 | 32 | 2026-03-16 | 11 | completed | 34.9 |
| 5149 | 54 | 2026-03-17 | NULL | completed | 34.9 |
| 5150 | 86 | 2026-03-17 | NULL | completed | 25.8 |
| 5151 | 27 | 2026-03-18 | NULL | completed | 9.9 |
| 5152 | 87 | 2026-03-19 | 9 | completed | 119 |
| 5153 | 72 | 2026-03-22 | 11 | completed | 24.9 |
| 5154 | 88 | 2026-04-01 | 4 | completed | 29.9 |
| 5155 | 89 | 2026-04-05 | 9 | completed | 49.9 |
| 5156 | 90 | 2026-04-05 | 9 | completed | 102.8 |
| 5157 | 74 | 2026-04-06 | 3 | completed | 24.9 |
| 5158 | 91 | 2026-04-11 | 9 | completed | 29.9 |
| 5159 | 61 | 2026-04-12 | NULL | completed | 79.9 |
| 5160 | 92 | 2026-04-14 | NULL | completed | 29.9 |
| 5161 | 93 | 2026-04-19 | NULL | completed | 29.9 |
| 5162 | 94 | 2026-04-24 | 9 | completed | 29.9 |
| 5163 | 95 | 2026-04-28 | 4 | completed | 44.9 |
| 5164 | 96 | 2026-04-30 | 9 | completed | 129 |
| 5165 | 92 | 2026-05-07 | NULL | completed | 55.7 |
| 5166 | 97 | 2026-05-10 | 4 | completed | 54.8 |
| 5167 | 98 | 2026-05-11 | 9 | completed | 14.9 |
| 5168 | 99 | 2026-05-13 | 4 | completed | 34.9 |
| 5169 | 100 | 2026-05-18 | 9 | completed | 129 |
| 5170 | 101 | 2026-05-19 | 4 | completed | 148.7 |
| 5171 | 63 | 2026-05-23 | 3 | cancelled | 24.9 |
| 5172 | 102 | 2026-05-25 | NULL | completed | 44.9 |
| 5173 | 78 | 2026-06-01 | NULL | cancelled | 153.9 |
| 5174 | 103 | 2026-06-01 | NULL | completed | 99.9 |
| 5175 | 42 | 2026-06-02 | 9 | completed | 129 |
| 5176 | 104 | 2026-06-04 | NULL | completed | 49.9 |
| 5177 | 105 | 2026-06-08 | NULL | completed | 19.9 |
| 5178 | 50 | 2026-06-09 | NULL | completed | 102.8 |
| 5179 | 106 | 2026-06-09 | NULL | completed | 119.7 |
| 5180 | 107 | 2026-06-16 | 4 | cancelled | 29.7 |
| 5181 | 108 | 2026-06-22 | 9 | completed | 12.9 |
| 5182 | 79 | 2026-06-23 | 3 | completed | 99.9 |
| 5183 | 109 | 2026-06-26 | 4 | completed | 129 |
| 5184 | 110 | 2026-06-26 | NULL | cancelled | 24.9 |
| 5185 | 81 | 2026-06-28 | NULL | completed | 99.9 |
| 5186 | 111 | 2026-07-02 | NULL | completed | 158.9 |
| 5187 | 77 | 2026-07-04 | NULL | completed | 114.7 |
| 5188 | 112 | 2026-07-04 | NULL | completed | 39.9 |
| 5189 | 113 | 2026-07-09 | 4 | cancelled | 19.9 |
| 5190 | 114 | 2026-07-14 | NULL | completed | 49.9 |
| 5191 | 83 | 2026-07-15 | NULL | completed | 148.7 |
| 5192 | 76 | 2026-07-17 | NULL | completed | 34.9 |
| 5193 | 84 | 2026-07-17 | NULL | completed | 34.9 |
| 5194 | 115 | 2026-07-19 | NULL | completed | 79.9 |
| 5195 | 74 | 2026-07-23 | 3 | completed | 33.8 |
| 5196 | 92 | 2026-07-27 | 12 | completed | 19.9 |
| 5197 | 116 | 2026-07-27 | 12 | completed | 27.9 |
| 5198 | 117 | 2026-07-28 | NULL | completed | 99.9 |
| 5199 | 67 | 2026-07-30 | 3 | completed | 29.9 |
| 5200 | 118 | 2026-08-03 | NULL | completed | 119 |
| 5201 | 119 | 2026-08-07 | 3 | completed | 102.8 |
| 5202 | 120 | 2026-08-09 | NULL | completed | 89.9 |
| 5203 | 93 | 2026-08-15 | 3 | completed | 47.8 |
| 5204 | 121 | 2026-08-15 | NULL | completed | 27.9 |
| 5205 | 122 | 2026-08-18 | NULL | completed | 44.9 |
| 5206 | 102 | 2026-08-24 | 3 | completed | 89.8 |
| 5207 | 123 | 2026-08-25 | 12 | completed | 142.7 |
| 5208 | 124 | 2026-08-27 | 12 | completed | 113.7 |
| 5209 | 95 | 2026-08-28 | 12 | completed | 99.9 |
| 5210 | 92 | 2026-08-31 | 3 | completed | 29.8 |
| 5211 | 125 | 2026-08-31 | 3 | completed | 19.9 |
| 5212 | 110 | 2026-09-01 | NULL | completed | 42.7 |
| 5213 | 126 | 2026-09-03 | 3 | completed | 29.9 |
| 5214 | 127 | 2026-09-07 | NULL | completed | 29.8 |
| 5215 | 128 | 2026-09-13 | NULL | completed | 24.9 |
| 5216 | 129 | 2026-09-19 | 13 | completed | 89.9 |
| 5217 | 130 | 2026-09-20 | NULL | completed | 19.9 |
| 5218 | 117 | 2026-09-21 | 3 | completed | 96.8 |
| 5219 | 131 | 2026-09-22 | 3 | completed | 89.9 |
| 5220 | 50 | 2026-09-23 | NULL | completed | 44.9 |
| 5221 | 91 | 2026-09-23 | NULL | completed | 89.9 |
| 5222 | 92 | 2026-09-23 | 3 | completed | 89.9 |
| 5223 | 132 | 2026-09-26 | 12 | completed | 89.9 |
| 5224 | 115 | 2026-10-03 | NULL | completed | 25.8 |
| 5225 | 133 | 2026-10-04 | 13 | cancelled | 49.9 |
| 5226 | 63 | 2026-10-05 | NULL | completed | 24.9 |
| 5227 | 134 | 2026-10-05 | 13 | completed | 27.9 |
| 5228 | 106 | 2026-10-08 | NULL | completed | 29.9 |
| 5229 | 119 | 2026-10-08 | NULL | completed | 24.9 |
| 5230 | 135 | 2026-10-10 | 12 | completed | 24.9 |
| 5231 | 95 | 2026-10-13 | NULL | completed | 24.9 |
| 5232 | 102 | 2026-10-13 | NULL | completed | 49.9 |
| 5233 | 98 | 2026-10-14 | NULL | completed | 14.9 |
| 5234 | 136 | 2026-10-16 | 4 | completed | 24.9 |
| 5235 | 137 | 2026-10-20 | 3 | completed | 34.9 |
| 5236 | 138 | 2026-10-22 | 13 | completed | 34.9 |
| 5237 | 139 | 2026-10-27 | NULL | completed | 89.8 |
| 5238 | 140 | 2026-10-29 | 3 | completed | 19.9 |
| 5239 | 112 | 2026-10-31 | NULL | completed | 104.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_id | customer_id | order_date | campaign_id | status | amount |
|---|---|---|---|---|---|
| 5001 | 1 | 2025-01-16 | 1 | completed | 29.8 |
| 5002 | 2 | 2025-01-24 | NULL | completed | 139.7 |
| 5003 | 3 | 2025-01-28 | NULL | completed | 49.9 |
| 5004 | 4 | 2025-02-01 | 4 | completed | 69.8 |
| 5005 | 5 | 2025-02-07 | 3 | completed | 89.9 |
| 5006 | 6 | 2025-02-14 | NULL | completed | 24.9 |
| 5007 | 7 | 2025-02-25 | NULL | cancelled | 24.9 |
| 5008 | 8 | 2025-03-03 | 2 | completed | 139.7 |
| 5009 | 9 | 2025-03-06 | 2 | completed | 34.9 |
| 5010 | 6 | 2025-03-13 | NULL | completed | 24.9 |
| 5011 | 10 | 2025-03-15 | 2 | completed | 19.9 |
| 5012 | 11 | 2025-03-17 | NULL | completed | 49.9 |
| 5013 | 12 | 2025-03-29 | 3 | cancelled | 89.9 |
| 5014 | 13 | 2025-03-30 | 4 | completed | 24.9 |
| 5015 | 2 | 2025-04-03 | 3 | completed | 24.9 |
| 5016 | 14 | 2025-04-05 | 4 | completed | 79.9 |
| 5017 | 5 | 2025-04-06 | 3 | completed | 34.9 |
| 5018 | 10 | 2025-04-10 | NULL | completed | 89.9 |
| 5019 | 15 | 2025-04-12 | 3 | completed | 114.8 |
| 5020 | 16 | 2025-04-19 | 3 | completed | 99.9 |
| 5021 | 17 | 2025-04-26 | NULL | cancelled | 79.9 |
| 5022 | 7 | 2025-04-30 | NULL | completed | 29.9 |
| 5023 | 18 | 2025-05-03 | NULL | completed | 34.9 |
| 5024 | 19 | 2025-05-08 | NULL | completed | 89.9 |
| 5025 | 20 | 2025-05-12 | 3 | completed | 19.9 |
| 5026 | 21 | 2025-05-17 | 2 | cancelled | 39.9 |
| 5027 | 22 | 2025-05-18 | NULL | completed | 79.9 |
| 5028 | 14 | 2025-05-24 | 1 | completed | 89.9 |
| 5029 | 23 | 2025-05-25 | 1 | completed | 29.8 |
| 5030 | 16 | 2025-06-01 | 3 | cancelled | 27.9 |
| 5031 | 24 | 2025-06-01 | NULL | completed | 19.9 |
| 5032 | 25 | 2025-06-09 | 3 | completed | 79.9 |
| 5033 | 26 | 2025-06-15 | 6 | cancelled | 34.9 |
| 5034 | 27 | 2025-06-16 | 3 | completed | 24.9 |
| 5035 | 28 | 2025-06-18 | 6 | completed | 29.8 |
| 5036 | 29 | 2025-06-25 | 6 | completed | 19.9 |
| 5037 | 30 | 2025-07-06 | NULL | completed | 19.8 |
| 5038 | 31 | 2025-07-08 | 5 | completed | 24.9 |
| 5039 | 32 | 2025-07-10 | 5 | completed | 119.7 |
| 5040 | 33 | 2025-07-14 | 6 | completed | 49.9 |
| 5041 | 34 | 2025-07-24 | 6 | completed | 34.9 |
| 5042 | 35 | 2025-07-28 | 6 | cancelled | 89.9 |
| 5043 | 24 | 2025-07-29 | 3 | completed | 34.9 |
| 5044 | 36 | 2025-08-01 | 6 | completed | 104.8 |
| 5045 | 5 | 2025-08-05 | 3 | completed | 29.9 |
| 5046 | 37 | 2025-08-06 | NULL | cancelled | 119 |
| 5047 | 2 | 2025-08-07 | 3 | completed | 89.9 |
| 5048 | 38 | 2025-08-09 | NULL | completed | 9.9 |
| 5049 | 39 | 2025-08-14 | 6 | completed | 19.9 |
| 5050 | 40 | 2025-08-19 | 4 | completed | 89.9 |
| 5051 | 41 | 2025-08-29 | NULL | completed | 89.9 |
| 5052 | 42 | 2025-08-29 | 3 | completed | 12.9 |
| 5053 | 43 | 2025-09-03 | NULL | completed | 24.9 |
| 5054 | 44 | 2025-09-06 | 5 | completed | 34.9 |
| 5055 | 23 | 2025-09-07 | 5 | completed | 99.9 |
| 5056 | 38 | 2025-09-12 | 3 | completed | 25.8 |
| 5057 | 45 | 2025-09-15 | NULL | completed | 19.9 |
| 5058 | 46 | 2025-09-18 | 5 | completed | 99.9 |
| 5059 | 41 | 2025-09-19 | NULL | completed | 34.9 |
| 5060 | 47 | 2025-09-19 | NULL | completed | 9.9 |
| 5061 | 5 | 2025-09-24 | NULL | completed | 24.9 |
| 5062 | 48 | 2025-09-29 | NULL | completed | 79.9 |
| 5063 | 49 | 2025-09-29 | 5 | completed | 29.9 |
| 5064 | 50 | 2025-10-08 | NULL | completed | 129 |
| 5065 | 14 | 2025-10-10 | NULL | completed | 86.7 |
| 5066 | 51 | 2025-10-11 | 5 | completed | 119.7 |
| 5067 | 16 | 2025-10-12 | 5 | completed | 99.9 |
| 5068 | 52 | 2025-10-14 | 5 | completed | 119.7 |
| 5069 | 34 | 2025-10-19 | NULL | completed | 54.8 |
| 5070 | 21 | 2025-10-20 | NULL | completed | 49.9 |
| 5071 | 42 | 2025-10-20 | 5 | completed | 16.9 |
| 5072 | 53 | 2025-10-21 | 3 | completed | 89.9 |
| 5073 | 54 | 2025-10-24 | NULL | completed | 54.8 |
| 5074 | 55 | 2025-10-29 | 5 | completed | 99.9 |
| 5075 | 56 | 2025-11-04 | NULL | completed | 34.9 |
| 5076 | 57 | 2025-11-05 | 5 | completed | 114.8 |
| 5077 | 27 | 2025-11-07 | 3 | completed | 12.9 |
| 5078 | 31 | 2025-11-07 | NULL | completed | 59.9 |
| 5079 | 32 | 2025-11-07 | NULL | completed | 27.9 |
| 5080 | 58 | 2025-11-18 | NULL | completed | 19.9 |
| 5081 | 59 | 2025-11-23 | 5 | completed | 89.9 |
| 5082 | 60 | 2025-11-23 | 8 | completed | 25.8 |
| 5083 | 12 | 2025-11-24 | 7 | completed | 47.92 |
| 5084 | 15 | 2025-11-24 | 8 | completed | 91.84 |
| 5085 | 48 | 2025-11-24 | 7 | completed | 55.76 |
| 5086 | 51 | 2025-11-24 | 7 | completed | 23.92 |
| 5087 | 1 | 2025-11-25 | 7 | completed | 31.92 |
| 5088 | 7 | 2025-11-25 | NULL | completed | 47.92 |
| 5089 | 61 | 2025-11-25 | 3 | completed | 23.92 |
| 5090 | 6 | 2025-11-26 | 7 | completed | 39.92 |
| 5091 | 31 | 2025-11-26 | 8 | completed | 23.92 |
| 5092 | 56 | 2025-11-26 | 8 | completed | 10.32 |
| 5093 | 13 | 2025-11-27 | 7 | cancelled | 91.84 |
| 5094 | 30 | 2025-11-27 | 7 | completed | 31.92 |
| 5095 | 38 | 2025-11-28 | 7 | completed | 103.2 |
| 5096 | 55 | 2025-11-28 | 8 | completed | 63.92 |
| 5097 | 9 | 2025-11-29 | 8 | completed | 27.92 |
| 5098 | 5 | 2025-11-30 | 8 | completed | 15.84 |
| 5099 | 42 | 2025-11-30 | 3 | completed | 7.92 |
| 5100 | 44 | 2025-11-30 | 7 | completed | 48.56 |
| 5101 | 46 | 2025-11-30 | 7 | completed | 38.24 |
| 5102 | 47 | 2025-11-30 | 7 | completed | 31.92 |
| 5103 | 24 | 2025-12-01 | 7 | completed | 90.24 |
| 5104 | 62 | 2025-12-01 | 8 | completed | 15.92 |
| 5105 | 63 | 2025-12-08 | NULL | completed | 19.8 |
| 5106 | 64 | 2025-12-11 | NULL | completed | 114.8 |
| 5107 | 14 | 2025-12-12 | NULL | completed | 29.9 |
| 5108 | 65 | 2025-12-15 | 5 | completed | 89.7 |
| 5109 | 66 | 2025-12-19 | 5 | completed | 104.8 |
| 5110 | 67 | 2025-12-25 | NULL | completed | 34.9 |
| 5111 | 68 | 2025-12-26 | NULL | completed | 59.9 |
| 5112 | 42 | 2026-01-04 | NULL | completed | 79.9 |
| 5113 | 69 | 2026-01-06 | 10 | completed | 16.9 |
| 5114 | 70 | 2026-01-06 | 3 | completed | 19.9 |
| 5115 | 2 | 2026-01-09 | 9 | completed | 89.9 |
| 5116 | 71 | 2026-01-16 | 9 | completed | 25.8 |
| 5117 | 72 | 2026-01-17 | 9 | completed | 89.9 |
| 5118 | 50 | 2026-01-18 | NULL | completed | 19.9 |
| 5119 | 73 | 2026-01-23 | 10 | completed | 59.9 |
| 5120 | 74 | 2026-01-26 | NULL | completed | 19.8 |
| 5121 | 75 | 2026-02-02 | 9 | completed | 119 |
| 5122 | 76 | 2026-02-06 | 4 | completed | 24.9 |
| 5123 | 77 | 2026-02-07 | NULL | completed | 79.9 |
| 5124 | 78 | 2026-02-13 | NULL | completed | 9.9 |
| 5125 | 79 | 2026-02-15 | NULL | completed | 104.8 |
| 5126 | 74 | 2026-02-18 | NULL | completed | 49.9 |
| 5127 | 80 | 2026-02-20 | 4 | completed | 19.9 |
| 5128 | 68 | 2026-02-24 | NULL | completed | 12.9 |
| 5129 | 81 | 2026-02-25 | 9 | completed | 29.9 |
| 5130 | 52 | 2026-02-28 | NULL | completed | 59.9 |
| 5131 | 38 | 2026-03-01 | 3 | completed | 89.9 |
| 5132 | 82 | 2026-03-01 | NULL | completed | 19.9 |
| 5133 | 4 | 2026-03-02 | NULL | completed | 29.8 |
| 5134 | 67 | 2026-03-03 | 11 | completed | 39.9 |
| 5135 | 2 | 2026-03-05 | 9 | completed | 44.9 |
| 5136 | 17 | 2026-03-05 | NULL | completed | 79.9 |
| 5137 | 31 | 2026-03-06 | NULL | completed | 136.7 |
| 5138 | 83 | 2026-03-07 | NULL | completed | 49.9 |
| 5139 | 70 | 2026-03-08 | 11 | completed | 47.8 |
| 5140 | 84 | 2026-03-11 | 3 | completed | 49.8 |
| 5141 | 7 | 2026-03-12 | NULL | completed | 19.9 |
| 5142 | 53 | 2026-03-12 | 11 | completed | 19.9 |
| 5143 | 85 | 2026-03-12 | 4 | completed | 22.8 |
| 5144 | 5 | 2026-03-13 | NULL | cancelled | 104.8 |
| 5145 | 16 | 2026-03-14 | 9 | completed | 34.9 |
| 5146 | 57 | 2026-03-15 | NULL | completed | 107.8 |
| 5147 | 14 | 2026-03-16 | 11 | completed | 24.9 |
| 5148 | 32 | 2026-03-16 | 11 | completed | 34.9 |
| 5149 | 54 | 2026-03-17 | NULL | completed | 34.9 |
| 5150 | 86 | 2026-03-17 | NULL | completed | 25.8 |
| 5151 | 27 | 2026-03-18 | NULL | completed | 9.9 |
| 5152 | 87 | 2026-03-19 | 9 | completed | 119 |
| 5153 | 72 | 2026-03-22 | 11 | completed | 24.9 |
| 5154 | 88 | 2026-04-01 | 4 | completed | 29.9 |
| 5155 | 89 | 2026-04-05 | 9 | completed | 49.9 |
| 5156 | 90 | 2026-04-05 | 9 | completed | 102.8 |
| 5157 | 74 | 2026-04-06 | 3 | completed | 24.9 |
| 5158 | 91 | 2026-04-11 | 9 | completed | 29.9 |
| 5159 | 61 | 2026-04-12 | NULL | completed | 79.9 |
| 5160 | 92 | 2026-04-14 | NULL | completed | 29.9 |
| 5161 | 93 | 2026-04-19 | NULL | completed | 29.9 |
| 5162 | 94 | 2026-04-24 | 9 | completed | 29.9 |
| 5163 | 95 | 2026-04-28 | 4 | completed | 44.9 |
| 5164 | 96 | 2026-04-30 | 9 | completed | 129 |
| 5165 | 92 | 2026-05-07 | NULL | completed | 55.7 |
| 5166 | 97 | 2026-05-10 | 4 | completed | 54.8 |
| 5167 | 98 | 2026-05-11 | 9 | completed | 14.9 |
| 5168 | 99 | 2026-05-13 | 4 | completed | 34.9 |
| 5169 | 100 | 2026-05-18 | 9 | completed | 129 |
| 5170 | 101 | 2026-05-19 | 4 | completed | 148.7 |
| 5171 | 63 | 2026-05-23 | 3 | cancelled | 24.9 |
| 5172 | 102 | 2026-05-25 | NULL | completed | 44.9 |
| 5173 | 78 | 2026-06-01 | NULL | cancelled | 153.9 |
| 5174 | 103 | 2026-06-01 | NULL | completed | 99.9 |
| 5175 | 42 | 2026-06-02 | 9 | completed | 129 |
| 5176 | 104 | 2026-06-04 | NULL | completed | 49.9 |
| 5177 | 105 | 2026-06-08 | NULL | completed | 19.9 |
| 5178 | 50 | 2026-06-09 | NULL | completed | 102.8 |
| 5179 | 106 | 2026-06-09 | NULL | completed | 119.7 |
| 5180 | 107 | 2026-06-16 | 4 | cancelled | 29.7 |
| 5181 | 108 | 2026-06-22 | 9 | completed | 12.9 |
| 5182 | 79 | 2026-06-23 | 3 | completed | 99.9 |
| 5183 | 109 | 2026-06-26 | 4 | completed | 129 |
| 5184 | 110 | 2026-06-26 | NULL | cancelled | 24.9 |
| 5185 | 81 | 2026-06-28 | NULL | completed | 99.9 |
| 5186 | 111 | 2026-07-02 | NULL | completed | 158.9 |
| 5187 | 77 | 2026-07-04 | NULL | completed | 114.7 |
| 5188 | 112 | 2026-07-04 | NULL | completed | 39.9 |
| 5189 | 113 | 2026-07-09 | 4 | cancelled | 19.9 |
| 5190 | 114 | 2026-07-14 | NULL | completed | 49.9 |
| 5191 | 83 | 2026-07-15 | NULL | completed | 148.7 |
| 5192 | 76 | 2026-07-17 | NULL | completed | 34.9 |
| 5193 | 84 | 2026-07-17 | NULL | completed | 34.9 |
| 5194 | 115 | 2026-07-19 | NULL | completed | 79.9 |
| 5195 | 74 | 2026-07-23 | 3 | completed | 33.8 |
| 5196 | 92 | 2026-07-27 | 12 | completed | 19.9 |
| 5197 | 116 | 2026-07-27 | 12 | completed | 27.9 |
| 5198 | 117 | 2026-07-28 | NULL | completed | 99.9 |
| 5199 | 67 | 2026-07-30 | 3 | completed | 29.9 |
| 5200 | 118 | 2026-08-03 | NULL | completed | 119 |
| 5201 | 119 | 2026-08-07 | 3 | completed | 102.8 |
| 5202 | 120 | 2026-08-09 | NULL | completed | 89.9 |
| 5203 | 93 | 2026-08-15 | 3 | completed | 47.8 |
| 5204 | 121 | 2026-08-15 | NULL | completed | 27.9 |
| 5205 | 122 | 2026-08-18 | NULL | completed | 44.9 |
| 5206 | 102 | 2026-08-24 | 3 | completed | 89.8 |
| 5207 | 123 | 2026-08-25 | 12 | completed | 142.7 |
| 5208 | 124 | 2026-08-27 | 12 | completed | 113.7 |
| 5209 | 95 | 2026-08-28 | 12 | completed | 99.9 |
| 5210 | 92 | 2026-08-31 | 3 | completed | 29.8 |
| 5211 | 125 | 2026-08-31 | 3 | completed | 19.9 |
| 5212 | 110 | 2026-09-01 | NULL | completed | 42.7 |
| 5213 | 126 | 2026-09-03 | 3 | completed | 29.9 |
| 5214 | 127 | 2026-09-07 | NULL | completed | 29.8 |
| 5215 | 128 | 2026-09-13 | NULL | completed | 24.9 |
| 5216 | 129 | 2026-09-19 | 13 | completed | 89.9 |
| 5217 | 130 | 2026-09-20 | NULL | completed | 19.9 |
| 5218 | 117 | 2026-09-21 | 3 | completed | 96.8 |
| 5219 | 131 | 2026-09-22 | 3 | completed | 89.9 |
| 5220 | 50 | 2026-09-23 | NULL | completed | 44.9 |
| 5221 | 91 | 2026-09-23 | NULL | completed | 89.9 |
| 5222 | 92 | 2026-09-23 | 3 | completed | 89.9 |
| 5223 | 132 | 2026-09-26 | 12 | completed | 89.9 |
| 5224 | 115 | 2026-10-03 | NULL | completed | 25.8 |
| 5225 | 133 | 2026-10-04 | 13 | cancelled | 49.9 |
| 5226 | 63 | 2026-10-05 | NULL | completed | 24.9 |
| 5227 | 134 | 2026-10-05 | 13 | completed | 27.9 |
| 5228 | 106 | 2026-10-08 | NULL | completed | 29.9 |
| 5229 | 119 | 2026-10-08 | NULL | completed | 24.9 |
| 5230 | 135 | 2026-10-10 | 12 | completed | 24.9 |
| 5231 | 95 | 2026-10-13 | NULL | completed | 24.9 |
| 5232 | 102 | 2026-10-13 | NULL | completed | 49.9 |
| 5233 | 98 | 2026-10-14 | NULL | completed | 14.9 |
| 5234 | 136 | 2026-10-16 | 4 | completed | 24.9 |
| 5235 | 137 | 2026-10-20 | 3 | completed | 34.9 |
| 5236 | 138 | 2026-10-22 | 13 | completed | 34.9 |
| 5237 | 139 | 2026-10-27 | NULL | completed | 89.8 |
| 5238 | 140 | 2026-10-29 | 3 | completed | 19.9 |
| 5239 | 112 | 2026-10-31 | NULL | completed | 104.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.