SQL Business Analytics: Retention Cohorts & Conversion Funnels

In high-growth technology companies, two analytical workflows form the lifeblood of product strategy: Cohort Retention Analysis and Conversion Funnels. In this class, we build complete production SQL scripts to compute retention heatmaps and conversion drop-offs.

The Complete SQL User Retention Matrix

-- Step 1: Determine initial signup cohort for each user
WITH UserCohorts AS (
    SELECT 
        user_id,
        DATE_TRUNC('month', created_at) AS cohort_month
    FROM users
),
-- Step 2: Track active billing/activity dates
UserActivities AS (
    SELECT 
        o.user_id,
        DATE_TRUNC('month', o.order_date) AS activity_month
    FROM orders o
    GROUP BY o.user_id, DATE_TRUNC('month', o.order_date)
),
-- Step 3: Compute monthly index offset (Month 0, Month 1, Month 2...)
CohortCalculations AS (
    SELECT 
        c.cohort_month,
        (EXTRACT(YEAR FROM a.activity_month) - EXTRACT(YEAR FROM c.cohort_month)) * 12 +
        (EXTRACT(MONTH FROM a.activity_month) - EXTRACT(MONTH FROM c.cohort_month)) AS month_number,
        COUNT(DISTINCT a.user_id) AS active_users
    FROM UserCohorts c
    JOIN UserActivities a ON c.user_id = a.user_id
    GROUP BY c.cohort_month, month_number
),
-- Step 4: Extract cohort baseline size (Month 0 count)
CohortSizes AS (
    SELECT 
        cohort_month,
        active_users AS total_cohort_size
    FROM CohortCalculations
    WHERE month_number = 0
)
-- Step 5: Final output percentage matrix
SELECT 
    c.cohort_month,
    s.total_cohort_size,
    c.month_number,
    c.active_users,
    ROUND((c.active_users::DECIMAL / s.total_cohort_size) * 100.0, 2) AS retention_rate_pct
FROM CohortCalculations c
JOIN CohortSizes s ON c.cohort_month = s.cohort_month
ORDER BY c.cohort_month, c.month_number;

E-Commerce Checkout Conversion Funnel

-- Measure user drop-off across 4 key e-commerce checkout funnel stages
WITH FunnelSteps AS (
    SELECT 
        user_id,
        MAX(CASE WHEN event_name = 'PAGE_VIEW' THEN 1 ELSE 0 END) AS step_1_view,
        MAX(CASE WHEN event_name = 'ADD_TO_CART' THEN 1 ELSE 0 END) AS step_2_cart,
        MAX(CASE WHEN event_name = 'CHECKOUT_START' THEN 1 ELSE 0 END) AS step_3_checkout,
        MAX(CASE WHEN event_name = 'PURCHASE_COMPLETED' THEN 1 ELSE 0 END) AS step_4_purchase
    FROM website_events
    WHERE event_timestamp >= '2026-01-01'
    GROUP BY user_id
)
SELECT 
    SUM(step_1_view) AS visitors,
    SUM(step_2_cart) AS cart_adders,
    ROUND(SUM(step_2_cart)::DECIMAL / SUM(step_1_view) * 100, 2) AS view_to_cart_pct,
    SUM(step_3_checkout) AS checkout_starters,
    ROUND(SUM(step_3_checkout)::DECIMAL / SUM(step_2_cart) * 100, 2) AS cart_to_checkout_pct,
    SUM(step_4_purchase) AS buyers,
    ROUND(SUM(step_4_purchase)::DECIMAL / SUM(step_3_checkout) * 100, 2) AS checkout_to_buy_pct,
    ROUND(SUM(step_4_purchase)::DECIMAL / SUM(step_1_view) * 100, 2) AS overall_conversion_pct
FROM FunnelSteps;

Visualizing Cohort Matrices

In Class 26, we will take the output of this SQL query and render an interactive heatmap using Seaborn and Plotly, color-coding retention drop-offs to pinpoint the exact month where customer churn accelerates.

Frequently Asked Questions (FAQ) - Student & Interview Review

Q1. What is Cohort Analysis in product analytics?

Cohort analysis groups users by a shared starting characteristic (most commonly their signup or acquisition month) and tracks their recurring engagement, retention, or spending behavior over successive subsequent months.

Q2. What is a retention curve?

A retention curve plots the percentage of active users remaining over time (Day 1, Day 7, Day 30, Month 3, Month 6). A healthy SaaS or consumer product displays a retention curve that flattens out into a horizontal plateau, signaling Product-Market Fit.

Q3. What is a Conversion Funnel in SQL?

A conversion funnel tracks users through sequential milestone steps (e.g., Homepage Visit -> Product View -> Add to Cart -> Initiate Checkout -> Purchase) and measures the percentage drop-off at each friction point.

Q4. How do you calculate retention in SQL without massive subquery overhead?

Group users by cohort signup date using DATE_TRUNC, join against activity event logs, calculate the monthly index offset: (EXTRACT(year from activity) - cohort_year)*12 + (EXTRACT(month from activity) - cohort_month), and aggregate distinct user counts.

Q5. Why is cohort retention superior to simple overall churn rate?

Overall churn masks underlying product shifts. A high influx of new users can hide the fact that older customers are defecting rapidly, whereas cohort analysis pinpoints whether newer app releases are improving long-term retention.

Explore Related Data Science Tutorials & Next Steps

Broaden your statistical analysis, Python data wrangling, and production engineering expertise:

Advanced SQL: Window Functions & CTEs

Master the mathematical window clauses required for cohort calculations.

Time Series Analysis & Resampling

Analyze longitudinal trends, moving averages, and seasonal user activity.

Customer Churn Prediction Modeling

Use cohort indicators as predictive features in machine learning models.

Streamlit Business Intelligence Dashboards

Present interactive cohort heatmaps and retention graphs to leadership.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!