When you need it
"What share of users who viewed an item added it to the cart, then bought?", "where do people drop off in checkout", "conversion from signup to first purchase". Words that signal it: "funnel", "drop-off", "step", "then", "after", "conversion", "reached".
A funnel is an ordered list of steps. Each user is counted at a step only if they did that step, usually after the one before it.
The shape of the query
with first_times as (
select user_id,
min(case when event_name = 'view_item' then occurred_at end) as viewed_at,
min(case when event_name = 'add_to_cart' then occurred_at end) as carted_at,
min(case when event_name = 'purchase' then occurred_at end) as bought_at
from events
group by user_id
)
select count(viewed_at) as viewed,
count(*) filter (where carted_at >= viewed_at) as carted,
count(*) filter (where bought_at >= carted_at and carted_at >= viewed_at) as bought
from first_times;Step by step:
- Reduce events to one row per user with the first time of each step.
MIN(CASE ...)returns NULL when the user never did it. - Count users at each step. A step counts only if the earlier steps happened and in the right order.
- Make each step a subset of the previous one, so counts can only fall.
- Divide by the previous step (step conversion) or by the first step (overall conversion).
with first_times as (
select user_id,
min(case when event_name = 'view_item' then occurred_at end) as viewed_at,
min(case when event_name = 'add_to_cart' then occurred_at end) as carted_at,
min(case when event_name = 'begin_checkout' then occurred_at end) as checkout_at,
min(case when event_name = 'purchase' then occurred_at end) as bought_at
from events
group by user_id
), reached as (
select user_id,
viewed_at is not null as s1,
viewed_at is not null and carted_at >= viewed_at as s2,
viewed_at is not null and carted_at >= viewed_at and checkout_at >= carted_at as s3,
viewed_at is not null and carted_at >= viewed_at and checkout_at >= carted_at and bought_at >= checkout_at as s4
from first_times
)
select count(*) filter (where s1) as viewed,
count(*) filter (where s2) as carted,
count(*) filter (where s3) as checkout,
count(*) filter (where s4) as purchased,
round(100.0 * count(*) filter (where s4) / nullif(count(*) filter (where s1), 0), 1) as overall_pct
from reached;Traps interviewers look for
- Ignoring order. Counting users who ever did each event lets a user "buy" before they "view". Compare timestamps so each step follows the previous one.
- Counting events, not users. Ten views by one user is one user at the top of the funnel. Reduce to one row per user first.
- Only the first occurrence matters, but which? If a user viewed, bought, then viewed again, the first view still precedes the purchase. Using
minper step handles this. A strict "next step after the prior one" sometimes needs the first time after the previous step instead. - Equal timestamps. Two events in the same second can tie. Decide whether
>=or>is the rule and say so. - Wrong population. The funnel should start from the users who did step 1, not all users. Otherwise the top number is the whole user base.
- Percent denominators. Step conversion divides by the previous step. Overall conversion divides by step 1. Label them.
- Time limits. "Within 7 days of signup" needs an extra condition on the gap, not just order.
Variations
- Sessions. Funnel within a
session_idrather than across the user's lifetime. - By segment. Add
platformorchannelto the grouping and compare funnels. - Time to convert. Median or average of
bought_at - viewed_atfor those who converted. - Drop-off table. Rows per step with
lagof the previous count, and the loss in users and percent. - A/B test funnel. Join the experiment assignments and compare variants step by step.
In other warehouses
- Boolean columns like
viewed_at is not null as s1work in PostgreSQL, Snowflake, BigQuery and Databricks. SQL Server has no boolean type, so usecase when ... then 1 else 0 endandsum. FILTERis PostgreSQL and Databricks. Elsewhere writesum(case when s2 then 1 else 0 end). BigQuery hasCOUNTIF(s2)and Snowflake hasCOUNT_IF(s2).- Snowflake and BigQuery can compute the first time per step with
MIN_BYorARRAY_AGG, but theMIN(CASE ...)form travels everywhere. - Time gaps. Use
DATEDIFF(second, a, b)in Snowflake and SQL Server,TIMESTAMP_DIFF(b, a, SECOND)in BigQuery.