How to calculate MRR in SQL: churn, expansion and NRR
Month-end MRR, a movement bridge, logo churn, gross and net revenue retention, all from one subscriptions table, plus the edge cases that make two analysts get two different numbers.
To calculate MRR in SQL, take every subscription that is active at a fixed point in each month (usually the last day), divide its recurring price by the number of months it covers, and sum. An annual plan at 2,400 contributes 200 a month, a quarterly plan at 900 contributes 300, and trials, taxes and one-off charges contribute nothing. That is also how Stripe describes its own number: MRR “is the sum of the monthly-normalized value of all your active and past_due subscriptions”, excluding taxes, free plans and metered products. The hard part is not the sum. It is everything downstream: splitting the monthly change into new, expansion, contraction and churn, then turning those into churn rates and net revenue retention (NRR) that finance, sales and the board all accept. Below is a small subscriptions table, five tested queries (run on PostgreSQL 17), and the six decisions that make two careful analysts get two different churn rates from the same data.
What does the subscriptions table need to look like?
Everything below works from one table that records each subscription state as a period: one row per customer per price, with the date it started and the date it stopped being true. If your billing system gives you an event log or a “current state” table instead, building this shape is the first job, and usually the biggest one. Stripe’s subscription object, for example, holds the latest state, so history has to come from events or from snapshots you took yourself.
CREATE TABLE subscription_periods (
customer_id int NOT NULL,
plan text NOT NULL,
status text NOT NULL, -- 'active' | 'trialing' | 'paused'
amount numeric NOT NULL, -- recurring price per billing period, net of recurring discounts, excl. tax
interval_months int NOT NULL, -- 1 = monthly, 3 = quarterly, 12 = annual
valid_from date NOT NULL,
valid_to date -- exclusive; NULL = still current
);
INSERT INTO subscription_periods VALUES
(1, 'team', 'active', 100, 1, '2026-01-10', '2026-03-20'),
(1, 'business', 'active', 150, 1, '2026-03-20', NULL), -- upgrade mid-month
(2, 'business', 'active', 2400, 12, '2026-02-01', NULL), -- annual plan
(3, 'starter', 'active', 50, 1, '2026-01-05', '2026-03-05'), -- cancels
(3, 'starter', 'active', 50, 1, '2026-05-12', NULL), -- comes back
(4, 'team', 'active', 80, 1, '2026-01-20', '2026-03-01'),
(4, 'team', 'paused', 80, 1, '2026-03-01', '2026-04-01'), -- one-month pause
(4, 'team', 'active', 80, 1, '2026-04-01', NULL),
(5, 'starter', 'trialing', 0, 1, '2026-02-10', '2026-02-24'), -- free trial
(5, 'starter', 'active', 60, 1, '2026-02-24', NULL),
(6, 'business', 'active', 120, 1, '2026-01-02', '2026-04-10'),
(6, 'team', 'active', 90, 1, '2026-04-10', NULL), -- downgrade
(7, 'starter', 'active', 40, 1, '2026-03-03', '2026-03-28'), -- in and out within March
(8, 'team', 'active', 900, 3, '2026-02-15', NULL); -- quarterly plan
Eight customers, and almost every one of them is an edge case on purpose: a mid-month upgrade, an annual plan, a cancellation followed by a return, a pause, a trial, a downgrade, a customer who joined and left inside one month, and a quarterly plan. Two details in the schema matter. amount is already net of recurring discounts and excludes tax, so nothing downstream has to remember to do it. And valid_to is exclusive, which makes “active on day D” a clean valid_from <= D AND (valid_to IS NULL OR valid_to > D).
How do you calculate MRR per month in SQL?
Build a month spine, join every period that is active on the last day of the month, and sum the normalized amount:
WITH months AS (
SELECT m::date AS month,
(m + interval '1 month - 1 day')::date AS month_end
FROM generate_series(date '2026-01-01', date '2026-05-01', interval '1 month') AS m
)
SELECT m.month,
count(DISTINCT s.customer_id) AS paying_customers,
coalesce(sum(s.amount / s.interval_months), 0) AS mrr
FROM months m
LEFT JOIN subscription_periods s
ON s.status = 'active'
AND s.valid_from <= m.month_end
AND (s.valid_to IS NULL OR s.valid_to > m.month_end)
GROUP BY m.month
ORDER BY m.month;
| month | paying_customers | mrr |
|---|---|---|
| 2026-01-01 | 4 | 350 |
| 2026-02-01 | 7 | 910 |
| 2026-03-01 | 5 | 830 |
| 2026-04-01 | 6 | 880 |
| 2026-05-01 | 7 | 930 |
The month spine matters more than it looks. Grouping the subscriptions table directly by date_trunc('month', valid_from) gives you bookings by start month, not MRR, and it silently drops months in which nothing started. The LEFT JOIN from the spine keeps every month, including empty ones.
Notice what the snapshot does with the awkward rows. Customer 1 upgraded on 20 March, so March shows the full 150, not a prorated blend. Customer 7 paid for 25 days in March and never appears at all, because they were gone by the 31st. Customer 5’s two-week trial is invisible because its status is trialing. Each of those is a defensible choice, and each is a place where a different query would disagree. More on that below.
How do you split MRR change into new, expansion, contraction and churn?
The total alone does not tell you whether 830 is a good March. You need the MRR bridge: starting MRR, plus new, reactivation and expansion, minus contraction and churn, equals ending MRR. The reliable way to build it is per customer, per month, including the months where a customer has zero MRR, so that lag() always compares a customer with their own previous month.
CREATE VIEW mrr_movements AS
WITH months AS (
SELECT m::date AS month,
(m + interval '1 month - 1 day')::date AS month_end
FROM generate_series(date '2026-01-01', date '2026-05-01', interval '1 month') AS m
),
customer_months AS ( -- one row per customer per month, zeros included
SELECT c.customer_id, m.month,
round(coalesce(sum(s.amount / s.interval_months)
FILTER (WHERE s.status = 'active'), 0), 2) AS mrr,
coalesce(bool_or(s.status = 'paused'), false) AS is_paused
FROM (SELECT DISTINCT customer_id FROM subscription_periods) c
CROSS JOIN months m
LEFT JOIN subscription_periods s
ON s.customer_id = c.customer_id
AND s.valid_from <= m.month_end
AND (s.valid_to IS NULL OR s.valid_to > m.month_end)
GROUP BY c.customer_id, m.month
),
with_prev AS (
SELECT customer_id, month, mrr, is_paused,
coalesce(lag(mrr) OVER w, 0) AS prev_mrr,
coalesce(lag(is_paused) OVER w, false) AS prev_paused,
coalesce(max(mrr) OVER (w ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING), 0) > 0
AS paid_before
FROM customer_months
WINDOW w AS (PARTITION BY customer_id ORDER BY month)
)
SELECT customer_id, month, prev_mrr, mrr, mrr - prev_mrr AS delta,
CASE
WHEN prev_mrr = 0 AND mrr > 0 AND NOT paid_before THEN 'new'
WHEN prev_mrr = 0 AND mrr > 0 AND prev_paused THEN 'resumed'
WHEN prev_mrr = 0 AND mrr > 0 THEN 'reactivation'
WHEN prev_mrr > 0 AND mrr = 0 AND is_paused THEN 'paused'
WHEN prev_mrr > 0 AND mrr = 0 THEN 'churn'
WHEN mrr > prev_mrr THEN 'expansion'
WHEN mrr < prev_mrr THEN 'contraction'
ELSE 'flat'
END AS movement
FROM with_prev;
The classification is at the customer level, not the subscription level. A customer who swaps one 100 plan for another 150 plan is expansion of 50, not churn of 100 plus new of 150. That matches how ChartMogul defines its MRR movements: churn is when a customer cancels “their last (or only) subscription”, and a returning customer is reactivation, not new business. The paid_before flag is what separates the two.
Pauses get their own labels because they are neither churn nor retention, and pretending otherwise skews one or the other. Then the bridge is one GROUP BY:
SELECT month,
sum(prev_mrr) AS starting,
coalesce(sum(delta) FILTER (WHERE movement = 'new'), 0) AS new,
coalesce(sum(delta) FILTER (WHERE movement IN ('reactivation', 'resumed')), 0) AS reactivated,
coalesce(sum(delta) FILTER (WHERE movement = 'expansion'), 0) AS expansion,
coalesce(sum(delta) FILTER (WHERE movement = 'contraction'), 0) AS contraction,
coalesce(sum(delta) FILTER (WHERE movement = 'paused'), 0) AS paused,
coalesce(sum(delta) FILTER (WHERE movement = 'churn'), 0) AS churned,
sum(mrr) AS ending
FROM mrr_movements
GROUP BY month
ORDER BY month;
| month | starting | new | reactivated | expansion | contraction | paused | churned | ending |
|---|---|---|---|---|---|---|---|---|
| 2026-01-01 | 0 | 350 | 0 | 0 | 0 | 0 | 0 | 350 |
| 2026-02-01 | 350 | 560 | 0 | 0 | 0 | 0 | 0 | 910 |
| 2026-03-01 | 910 | 0 | 0 | 50 | 0 | -80 | -50 | 830 |
| 2026-04-01 | 830 | 0 | 80 | 0 | -30 | 0 | 0 | 880 |
| 2026-05-01 | 880 | 0 | 50 | 0 | 0 | 0 | 0 | 930 |
Every row reconciles: starting plus the movements equals ending. Make that a test. If a bridge stops reconciling, a movement category is missing or a customer is double-counted, and you want to know before the board deck does. The data-quality tests post covers how to wire a check like that into your pipeline.
FILTER is standard SQL and works in PostgreSQL, but not every engine supports it. Where it is missing, sum(CASE WHEN movement = 'new' THEN delta END) is the portable spelling.
How do you calculate churn rate in SQL?
“Churn rate” is at least three different numbers, and most arguments about churn are two people quoting different ones. Compute them side by side from the same view:
SELECT month,
count(*) FILTER (WHERE prev_mrr > 0) AS customers_at_start,
count(*) FILTER (WHERE movement = 'churn') AS churned_customers,
round(100.0 * count(*) FILTER (WHERE movement = 'churn')
/ nullif(count(*) FILTER (WHERE prev_mrr > 0), 0), 1) AS logo_churn_pct,
round(100.0 * coalesce(-sum(delta) FILTER (WHERE movement IN ('churn', 'paused', 'contraction')), 0)
/ nullif(sum(prev_mrr), 0), 1) AS gross_mrr_churn_pct,
round(100.0 * sum(mrr) FILTER (WHERE prev_mrr > 0)
/ nullif(sum(prev_mrr), 0), 1) AS nrr_pct,
round(100.0 * sum(least(mrr, prev_mrr)) FILTER (WHERE prev_mrr > 0)
/ nullif(sum(prev_mrr), 0), 1) AS grr_pct
FROM mrr_movements
GROUP BY month
HAVING sum(prev_mrr) > 0
ORDER BY month;
| month | customers_at_start | churned_customers | logo_churn_pct | gross_mrr_churn_pct | nrr_pct | grr_pct |
|---|---|---|---|---|---|---|
| 2026-02-01 | 4 | 0 | 0.0 | 0.0 | 100.0 | 100.0 |
| 2026-03-01 | 7 | 1 | 14.3 | 14.3 | 91.2 | 85.7 |
| 2026-04-01 | 5 | 0 | 0.0 | 3.6 | 96.4 | 96.4 |
| 2026-05-01 | 6 | 0 | 0.0 | 0.0 | 100.0 | 100.0 |
What each column means:
| Metric | Formula | Answers |
|---|---|---|
| Logo churn | churned customers / paying customers at start | How many customers did we lose? |
| Gross MRR churn | (churn + contraction + paused MRR) / starting MRR | How much existing revenue leaked? |
| Gross revenue retention (GRR) | starting MRR kept, capped at each customer’s starting value / starting MRR | Same as 100% minus gross MRR churn |
| Net revenue retention (NRR) | MRR now from customers who paid at start / starting MRR | Did the existing base grow or shrink? |
Two things to notice in March. Logo churn and gross MRR churn happen to be equal (14.3%), but for different reasons: logo churn counts only customer 3, while gross MRR churn counts customer 3’s 50 plus customer 4’s paused 80. In April nobody left, so logo churn is zero, yet 3.6% of revenue leaked through customer 6’s downgrade. A team that reports only logo churn would call April perfect.
Even the denominator is a choice. The query above divides by customers paying at the start of the month. Stripe’s dashboard defines subscriber churn rate as churned subscribers in the past 30 days divided by active subscribers 30 days ago plus new subscribers in that window. Neither is wrong. They are different numbers with the same name, which is exactly how the metric drift problem starts.
How do you calculate net revenue retention in SQL?
The monthly NRR above is useful for spotting a bad month, but the number investors and boards ask for is usually trailing twelve months: take every customer who paid twelve months ago, and compare what that same group pays today with what it paid then. New customers are excluded by construction, which is the whole point.
SELECT cur.month,
round(100.0 * sum(cur.mrr) / sum(base.mrr), 1) AS nrr_pct,
round(100.0 * sum(least(cur.mrr, base.mrr)) / sum(base.mrr), 1) AS grr_pct
FROM mrr_movements base
JOIN mrr_movements cur
ON cur.customer_id = base.customer_id
AND cur.month = (base.month + interval '12 months')::date
WHERE base.mrr > 0
GROUP BY cur.month
ORDER BY cur.month;
The sample data covers only five months, so swap 12 months for 3 months to see output: NRR is 91.4% in April (January’s four customers went from 350 to 320) and 102.2% in May. May clears 100% partly because customer 3 came back, and this is a decision in its own right. The cohort query counts a returning customer as retained revenue, while the monthly bridge counts the same 50 as reactivation. Decide which one your NRR means and write it down.
Note that compounding the monthly NRR figures does not give you the trailing-twelve-month value. Monthly NRR is computed over a different base each month, so the product of twelve monthly values drifts away from the cohort number. Use the cohort query for annual NRR.
Where do teams disagree on MRR and churn?
These are the decisions behind most “why doesn’t my number match yours” threads. None has a universally right answer; each needs one written answer for your company.
| Decision | Common options | What the sample data shows |
|---|---|---|
| Measurement point | Month-end snapshot, average daily MRR, invoice-based | Customer 7 paid for March but is invisible at month end |
| Mid-month changes | Count the end state, or prorate by days | Customer 1’s upgrade counts in full in March |
| Annual plans | Divide by 12, or recognize at billing | Customer 2 adds 200 a month, not 2,400 in February |
| Churn timing | When cancellation is requested, or when the paid term ends | Annual customers can be “cancelled” for 11 months |
| Pauses | Keep MRR, set to zero as churn, or track separately | Customer 4 is churn, pause, or nothing, depending on the rule |
| Trials, discounts, tax, usage | Usually excluded or netted out | Customer 5 counts only from 24 February |
The churn-timing and pause rows are configurable even inside dedicated subscription-analytics tools. ChartMogul’s MRR movements documentation describes a setting to recognize churn either when the subscription ends in the billing system or at the end of the final service period, and three ways to treat a paused subscription: no MRR movement, MRR set to zero (churn on pause, reactivation on resume), or cancelled. If a vendor needs a setting for it, your SQL needs a decision for it.
Two more that bite later. Multi-currency businesses should convert at a fixed planning rate, or exchange-rate swings show up as expansion and contraction; Stripe’s MRR growth chart reports an explicit FX adjustment line for this reason. And metered or usage-based charges should either stay out of MRR, as in Stripe’s definition, or go into a separately named metric, because they move with usage rather than with contracts.
Why the definitions belong in one place
Every query in this post is short. The trouble is that each one encodes about a dozen decisions, and those decisions get re-made, slightly differently, every time someone writes a new MRR query for a new dashboard, a finance export or a notebook. Six months later there are four MRR numbers, all defensible, and a meeting to decide which one is real. The fix is to put the definition of mrr, churned_mrr and nrr in one reviewed, versioned place and have every consumer ask for it by name rather than re-deriving it. That is what a semantic layer is for: the decision table above becomes code that is reviewed once, not tribal knowledge that is re-implemented per chart.
Whatever tool holds the definitions, the practical steps are the same. Build subscription_periods once, as a tested model. Build mrr_movements on top of it as the single source for every revenue retention number. Add a reconciliation test for the bridge. Then expose only named metrics to dashboards and to AI assistants, so nobody gets to improvise a fifth definition of churn.
If you want that workflow without stitching several tools together, Goni is one option. It is one AI platform for the data stack: ELT connectors, a ClickHouse warehouse, a YAML semantic layer where a metric like mrr or a ratio metric like churn_rate is defined once, and BI (SQL IDE, dashboards, alerts, scheduled reports) plus AI Q&A that resolve to the same definition and let you inspect the compiled SQL. The trade-off: ClickHouse is the only warehouse it queries, so it fits teams willing to run their analytics there. You can try it with the 14-day free trial.
FAQ
What is the formula for MRR?
MRR is the sum of every active subscription’s recurring price divided by the number of months the price covers, measured at one consistent point in the month. Monthly plans count in full, annual plans at one twelfth, quarterly plans at one third. Exclude trials, taxes, one-off charges and, usually, metered usage, and net out recurring discounts.
How do you calculate churn rate in SQL?
Build one row per customer per month with their MRR, compare each row with the previous month using lag(), and label customers who drop from positive MRR to zero as churned. Logo churn rate is churned customers divided by paying customers at the start of the month. Revenue churn uses MRR instead of counts.
What is the difference between NRR and GRR?
Both start from the MRR of an existing customer group and exclude new customers. Net revenue retention includes expansion, so it can exceed 100% when upgrades outweigh losses. Gross revenue retention caps each customer at their starting MRR, counting only downgrades and churn, so it can never exceed 100%. Report both: NRR shows growth, GRR shows leakage.
Should annual plans count as MRR in the month they are paid?
No. MRR measures recurring run rate, not cash. An annual plan at 1,200 contributes 100 to MRR in every month of its term. Booking the full 1,200 in the payment month inflates that month, then shows false contraction afterwards. Track cash collected and bookings as separate metrics with their own names.
How should paused subscriptions affect MRR?
Pick one rule and apply it everywhere. The common options are to keep the paused customer’s MRR, to set it to zero and count churn then reactivation, or to track pauses as their own movement. A separate movement is usually clearest, because it keeps pauses out of churn while still showing revenue that is not being billed.