Churn rate SQL queries
Three PostgreSQL queries that turn a table of subscriptions into monthly churn, MRR movements and a cohort table. Each one was run on example data with awkward cases in it, and the results are shown below, so you can check your copy gives the same.
Table, example data and the three queries in one file. Tested on PostgreSQL 19 through PGlite. The queries use FILTER, DISTINCT ON and generate_series, which PostgreSQL has supported for many years.
The table the queries expect
One row per subscription. Most billing systems can export this, or you can build it as a view on top of your own tables.
-- One row per subscription. A customer can have several (plan changes, add-ons, returns).
CREATE TABLE subscriptions (
subscription_id text PRIMARY KEY,
customer_id text NOT NULL,
started_on date NOT NULL, -- first day of paid service
ended_on date, -- first day without paid service; NULL while active
mrr numeric(12,2) NOT NULL, -- monthly recurring revenue (a yearly price / 12)
end_reason text -- 'voluntary', 'failed_payment' or NULL
);
- ended_on is the first day the customer no longer has paid service, not the day they clicked cancel. A customer who cancels on 3 March but has paid until 1 April gets 1 April.
- mrr is the monthly value: divide yearly prices by 12, leave out taxes and one-off fees. See the MRR calculator for what counts.
- end_reason separates customers who chose to leave from failed payments. If you do not have it, leave it empty; the queries still work.
The example data has ten customers, including a plan upgrade, a downgrade, a customer who joins and leaves in the same month, one who leaves and comes back, a cancellation that takes effect later, and two failed payments.
-- Example data: ten customers that cover the awkward cases.
INSERT INTO subscriptions VALUES
('S1', 'C1', '2025-10-15', NULL, 50, NULL), -- steady customer
('S2', 'C2', '2025-11-03', '2026-01-20', 30, 'voluntary'), -- leaves in January
('S3', 'C3', '2025-12-10', '2026-02-05', 40, 'failed_payment'), -- card fails in February
('S4a', 'C4', '2025-09-01', '2026-02-10', 20, NULL), -- upgrades in February:
('S4b', 'C4', '2026-02-10', NULL, 60, NULL), -- a new plan, not churn
('S5', 'C5', '2026-01-12', '2026-01-25', 25, 'voluntary'), -- joins and leaves in January
('S6a', 'C6', '2025-08-01', '2026-01-15', 35, 'voluntary'), -- leaves in January
('S6b', 'C6', '2026-03-05', NULL, 35, NULL), -- and comes back in March
('S7', 'C7', '2026-02-14', NULL, 45, NULL), -- new in February
('S8a', 'C8', '2025-06-01', '2026-03-01', 100, NULL), -- downgrades on 1 March
('S8b', 'C8', '2026-03-01', NULL, 70, NULL),
('S9', 'C9', '2025-12-01', '2026-05-01', 55, 'voluntary'), -- cancelled, paid until May
('S10', 'C10', '2026-01-01', '2026-03-15', 15, 'failed_payment'); -- starts 1 Jan, card fails in March
1. Monthly customer churn
Of the customers paying on the first day of each month, how many no longer pay on the first day of the next. A customer with any active subscription counts as paying, so moving to another plan is not churn. Lost customers are split by the reason on their last subscription.
-- Monthly customer churn: of the customers paying on the first day of a month,
-- how many no longer pay on the first day of the next month.
-- A customer counts as paying if any of their subscriptions is active, so plan changes are not churn.
WITH params AS (
SELECT date '2026-01-01' AS first_month, -- first month to report
date '2026-03-01' AS last_month -- last FULL month: the month after it must have started
),
months AS (
SELECT gs::date AS month_start, (gs + interval '1 month')::date AS next_start
FROM params, generate_series(params.first_month, params.last_month, interval '1 month') AS gs
),
at_start AS (
SELECT DISTINCT m.month_start, m.next_start, s.customer_id
FROM months m
JOIN subscriptions s
ON s.started_on <= m.month_start
AND (s.ended_on IS NULL OR s.ended_on > m.month_start)
),
still_paying AS (
SELECT DISTINCT a.month_start, a.customer_id
FROM at_start a
JOIN subscriptions s
ON s.customer_id = a.customer_id
AND s.started_on <= a.next_start
AND (s.ended_on IS NULL OR s.ended_on > a.next_start)
),
lost AS (
SELECT a.month_start, a.next_start, a.customer_id
FROM at_start a
LEFT JOIN still_paying p USING (month_start, customer_id)
WHERE p.customer_id IS NULL
),
lost_reason AS ( -- the reason on the last subscription that ended during the month
SELECT DISTINCT ON (l.month_start, l.customer_id)
l.month_start, l.customer_id, coalesce(s.end_reason, 'unknown') AS reason
FROM lost l
JOIN subscriptions s
ON s.customer_id = l.customer_id
AND s.ended_on > l.month_start AND s.ended_on <= l.next_start
ORDER BY l.month_start, l.customer_id, s.ended_on DESC
)
SELECT a.month_start,
count(*) AS customers_at_start,
count(r.customer_id) AS customers_lost,
count(r.customer_id) FILTER (WHERE r.reason = 'failed_payment') AS lost_to_failed_payments,
round(count(r.customer_id)::numeric / count(*), 4) AS churn_rate
FROM at_start a
LEFT JOIN lost_reason r USING (month_start, customer_id)
GROUP BY a.month_start
ORDER BY a.month_start;
| month_start | customers_at_start | customers_lost | lost_to_failed_payments | churn_rate |
|---|---|---|---|---|
| 2026-01-01 | 8 | 2 | 0 | 0.2500 |
| 2026-02-01 | 6 | 1 | 1 | 0.1667 |
| 2026-03-01 | 6 | 1 | 1 | 0.1667 |
In January C2 and C6 leave. C5 joined and left within January, so it was never in the starting group and does not count as churn. The upgrade (C4) and the downgrade (C8) are not churn either. This is the same definition the churn rate calculator uses.
2. MRR movements
Each customer’s MRR on the first day of the month against the first day of the next, sorted into new, reactivation, expansion, contraction and churn. The movements always add up from the start to the end.
-- MRR movements per month: each customer's MRR on the first day of the month
-- compared with their MRR on the first day of the next month.
WITH params AS (
SELECT date '2026-01-01' AS first_month, date '2026-03-01' AS last_month
),
months AS (
SELECT gs::date AS month_start, (gs + interval '1 month')::date AS next_start
FROM params, generate_series(params.first_month, params.last_month, interval '1 month') AS gs
),
customers AS (
SELECT customer_id, min(started_on) AS first_start FROM subscriptions GROUP BY customer_id
),
grid AS (
SELECT m.month_start, c.first_start,
coalesce((SELECT sum(s.mrr) FROM subscriptions s
WHERE s.customer_id = c.customer_id AND s.started_on <= m.month_start
AND (s.ended_on IS NULL OR s.ended_on > m.month_start)), 0) AS mrr_start,
coalesce((SELECT sum(s.mrr) FROM subscriptions s
WHERE s.customer_id = c.customer_id AND s.started_on <= m.next_start
AND (s.ended_on IS NULL OR s.ended_on > m.next_start)), 0) AS mrr_end
FROM months m CROSS JOIN customers c
)
SELECT month_start,
sum(mrr_start) AS mrr_at_start,
coalesce(sum(mrr_end) FILTER (WHERE mrr_start = 0 AND mrr_end > 0 AND first_start >= month_start), 0) AS new_mrr,
coalesce(sum(mrr_end) FILTER (WHERE mrr_start = 0 AND mrr_end > 0 AND first_start < month_start), 0) AS reactivation_mrr,
coalesce(sum(mrr_end - mrr_start) FILTER (WHERE mrr_start > 0 AND mrr_end > mrr_start), 0) AS expansion_mrr,
coalesce(sum(mrr_start - mrr_end) FILTER (WHERE mrr_end > 0 AND mrr_end < mrr_start), 0) AS contraction_mrr,
coalesce(sum(mrr_start) FILTER (WHERE mrr_start > 0 AND mrr_end = 0), 0) AS churned_mrr,
sum(mrr_end) AS mrr_at_end
FROM grid
GROUP BY month_start
ORDER BY month_start;
| month_start | mrr_at_start | new | reactivation | expansion | contraction | churned | mrr_at_end |
|---|---|---|---|---|---|---|---|
| 2026-01-01 | 345 | 0 | 0 | 0 | 0 | 65 | 280 |
| 2026-02-01 | 280 | 45 | 0 | 40 | 30 | 40 | 295 |
| 2026-03-01 | 295 | 0 | 35 | 0 | 0 | 15 | 315 |
Comparing two days per month is how most analytics tools report MRR. It has one blind spot: a customer who starts and leaves within the same month (C5) never shows up. Put the results in the revenue churn calculator or the quick ratio calculator, or paste them into the monthly retention report.
3. Cohort retention
Customers grouped by the month they first paid, and the share still paying a whole number of months after their own start date. A month only counts once it has been reached, so young cohorts are not dragged down by months that have not happened yet. Customers who come back count as paying again, so this is retention, not survival until the first cancellation.
-- Cohort retention: customers grouped by the month they first started paying, and the share
-- still paying 0, 1, 2 ... months after their own start date. A month only counts for a
-- customer once that date has been reached, so young cohorts are not dragged down.
WITH params AS (
SELECT date '2026-04-01' AS as_of, 24 AS max_months
),
customers AS (
SELECT customer_id, min(started_on) AS first_start FROM subscriptions GROUP BY customer_id
),
marks AS (
SELECT c.customer_id,
date_trunc('month', c.first_start)::date AS cohort_month,
k AS months_since_start,
(c.first_start + k * interval '1 month')::date AS mark_date
FROM customers c, params p, generate_series(0, p.max_months) AS k
WHERE (c.first_start + k * interval '1 month')::date <= p.as_of
),
status AS (
SELECT m.*, EXISTS (
SELECT 1 FROM subscriptions s
WHERE s.customer_id = m.customer_id AND s.started_on <= m.mark_date
AND (s.ended_on IS NULL OR s.ended_on > m.mark_date)) AS paying
FROM marks m
)
SELECT cohort_month, months_since_start,
count(*) AS customers,
count(*) FILTER (WHERE paying) AS still_paying,
round(count(*) FILTER (WHERE paying)::numeric / count(*), 4) AS retention
FROM status
GROUP BY cohort_month, months_since_start
ORDER BY cohort_month, months_since_start;
| cohort_month | months_since_start | customers | still_paying | retention |
|---|---|---|---|---|
| 2025-12-01 | 0 | 2 | 2 | 1.0000 |
| 2025-12-01 | 1 | 2 | 2 | 1.0000 |
| 2025-12-01 | 2 | 2 | 1 | 0.5000 |
| 2025-12-01 | 3 | 2 | 1 | 0.5000 |
| 2025-12-01 | 4 | 1 | 1 | 1.0000 |
| 2026-01-01 | 0 | 2 | 2 | 1.0000 |
| 2026-01-01 | 1 | 2 | 1 | 0.5000 |
| 2026-01-01 | 2 | 2 | 1 | 0.5000 |
| 2026-01-01 | 3 | 1 | 0 | 0.0000 |
Month 4 of the December cohort has only one customer, because the other one’s four-month mark falls after the as-of date. Pivot the result into a table with cohorts as rows; the guide to reading a cohort table explains what to look for.
Before you trust the numbers
- Check one month by hand. Pick a month, count the customers at its start in your billing system and compare.
- Only report full months. The last month in the churn and MRR queries must be one whose next month has already started.
- Watch for duplicates. If your export has one row per invoice instead of per subscription, the counts will be wrong. Build the subscriptions table first.
- Test customers and internal accounts belong out of the table.
Other databases
The queries are written for PostgreSQL and tested there only. Two parts are specific to it: generate_series for the list of months, which most data warehouses replace with a date array or a calendar table, and DISTINCT ON, which can be replaced by a window function that keeps the first row per customer. count(*) FILTER (WHERE …) can be written as sum(CASE WHEN … THEN 1 ELSE 0 END) in any SQL database. Run the example data first and compare with the tables above.
Prefer a spreadsheet? The Excel and Google Sheets guide does the same monthly churn calculation with COUNTIFS.