Conversion funnel in SQL
Of the users who signed up, how many subscribed, and how fast? A funnel counts, at each step, the users who reached the previous step and did this one after it, often within a time limit ("subscribed within 7 days of signing up"). One CTE per step: the first time each user reached it.
WITH signed_up AS (
SELECT user_id, MIN(occurred_at) AS signed_up_at
FROM events
WHERE event_name = 'signup'
GROUP BY user_id
),
subscribed AS (
SELECT s.user_id, MIN(e.occurred_at) AS subscribed_at
FROM signed_up s
JOIN events e
ON e.user_id = s.user_id
AND e.event_name = 'subscribe'
AND e.occurred_at > s.signed_up_at
AND e.occurred_at <= s.signed_up_at + INTERVAL '7' DAY
GROUP BY s.user_id
)
SELECT (SELECT COUNT(*) FROM signed_up) AS signed_up,
(SELECT COUNT(*) FROM subscribed) AS subscribed_7d;
A third step joins subscribed the same way, after subscribed_at.
The trap. Counting the users who ever did each event (COUNT(DISTINCT CASE WHEN event_name = … THEN user_id END)) lets in subscriptions made before the sign-up or a month later: the last steps look too full. Chain the steps
instead. And leave out the users whose 7 days are not over when the data ends: counted as not converted, they pull the
rate down.
On your warehouse. + INTERVAL '7' DAY runs on Postgres and Databricks; BigQuery writes
TIMESTAMP_ADD(s.signed_up_at, INTERVAL 7 DAY), Snowflake DATEADD(day, 7, s.signed_up_at).
WITH events(user_id, event_name, occurred_at) AS (VALUES
(1, 'signup', TIMESTAMP '2026-06-01 10:00:00'), (1, 'subscribe', TIMESTAMP '2026-06-03 09:00:00'),
(2, 'signup', TIMESTAMP '2026-06-01 11:00:00'), (2, 'subscribe', TIMESTAMP '2026-06-12 15:00:00'),
(3, 'subscribe', TIMESTAMP '2026-06-02 08:00:00'), (3, 'signup', TIMESTAMP '2026-06-02 09:30:00'),
(4, 'signup', TIMESTAMP '2026-06-04 14:00:00'),
(5, 'signup', TIMESTAMP '2026-06-05 16:00:00'), (5, 'subscribe', TIMESTAMP '2026-06-11 18:00:00')
),
signed_up AS (
SELECT user_id, MIN(occurred_at) AS signed_up_at
FROM events
WHERE event_name = 'signup'
GROUP BY user_id
),
converted AS (
SELECT DISTINCT s.user_id
FROM signed_up s
JOIN events e
ON e.user_id = s.user_id
AND e.event_name = 'subscribe'
AND e.occurred_at > s.signed_up_at
AND e.occurred_at <= s.signed_up_at + INTERVAL 7 DAY
)
SELECT (SELECT COUNT(*) FROM signed_up) AS signed_up,
(SELECT COUNT(DISTINCT user_id) FROM events WHERE event_name = 'subscribe') AS ever_subscribed,
(SELECT COUNT(*) FROM converted) AS subscribed_within_7_days;
| signed_up | ever_subscribed | subscribed_within_7_days |
|---|---|---|
| 5 | 4 | 2 |