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.
-- 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;
-- 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;
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.
Broaden your statistical analysis, Python data wrangling, and production engineering expertise:
Master the mathematical window clauses required for cohort calculations.
Analyze longitudinal trends, moving averages, and seasonal user activity.
Use cohort indicators as predictive features in machine learning models.
Present interactive cohort heatmaps and retention graphs to leadership.