How to Calculate Churn Rate in Sql

Churn Rate & SQL Logic Calculator

Verify your database metrics and generate SQL snippets

Analysis Result:

Churn Rate: 0%

How to Calculate Churn Rate in SQL

Churn rate is the percentage of customers that stopped using your service during a specific timeframe. In SQL, this involves comparing the number of active users at the beginning of a period to the number of those users who failed to renew or interact by the end.

1. Basic SQL Churn Formula

The most straightforward way to calculate churn for a single month is to count users active in month A who are not active in month B.

-- Calculate Churn for January to February
SELECT 
    (COUNT(DISTINCT jan.user_id) - COUNT(DISTINCT feb.user_id))::float / 
    NULLIF(COUNT(DISTINCT jan.user_id), 0) * 100 as churn_rate
FROM 
    active_users jan
LEFT JOIN 
    active_users feb ON jan.user_id = feb.user_id 
    AND feb.month = '2023-02-01'
WHERE 
    jan.month = '2023-01-01';

2. Handling the "Divide by Zero" Error

In SQL, if your starting customer count is zero, the query will fail. Always use NULLIF(count, 0) or a CASE statement to prevent production errors.

3. Monthly Cohort Analysis

To see churn trends over time, you can use a Self-Join or Window Functions. Here is a robust query for monthly tracking:

WITH monthly_active AS (
    SELECT 
        DATE_TRUNC('month', activity_date) as active_month,
        user_id
    FROM usage_logs
    GROUP BY 1, 2
)
SELECT 
    this_month.active_month,
    COUNT(DISTINCT this_month.user_id) as current_users,
    COUNT(DISTINCT next_month.user_id) as retained_users,
    (COUNT(DISTINCT this_month.user_id) - COUNT(DISTINCT next_month.user_id))::float / 
    COUNT(DISTINCT this_month.user_id) * 100 as churn_percentage
FROM monthly_active this_month
LEFT JOIN monthly_active next_month 
    ON this_month.user_id = next_month.user_id 
    AND next_month.active_month = this_month.active_month + INTERVAL '1 month'
GROUP BY 1
ORDER BY 1;

Key Metrics to Consider

  • Gross Churn: Purely the loss of customers.
  • Net Churn: Includes "expansion revenue" from existing customers.
  • Revenue Churn: The dollar amount lost rather than the customer count.

Example Scenario

If your SaaS platform had 1,200 active users on March 1st, and only 1,140 of those same users remained active or paid their bill by April 1st, your calculation would be:

(1,200 – 1,140) / 1,200 = 60 / 1,200 = 5% Churn Rate

function calculateChurn() { var startCount = document.getElementById("startCustomers").value; var lostCount = document.getElementById("lostCustomers").value; var resultDiv = document.getElementById("resultDisplay"); var churnValueSpan = document.getElementById("churnValue"); var interpretation = document.getElementById("churnInterpretation"); var start = parseFloat(startCount); var lost = parseFloat(lostCount); if (isNaN(start) || isNaN(lost) || start start) { alert("Lost customers cannot exceed starting customers for a standard churn calculation."); return; } var churnRate = (lost / start) * 100; var roundedRate = churnRate.toFixed(2); churnValueSpan.innerText = roundedRate; resultDiv.style.display = "block"; var message = ""; if (churnRate = 3 && churnRate <= 7) { message = "Average. This is standard for B2B SaaS, but room for improvement exists."; } else { message = "High Churn. Consider analyzing user friction points or product-market fit."; } interpretation.innerText = message; }

Leave a Comment