Stop 14 · SQL for Data AnalysisTopics · 5/9

Funnel Analysis in SQL

Part of the SQL Roadmap

In short

Thousands of people open your app, but only some buy. A funnel shows exactly which step loses the most people, so the team knows where to fix things first.

A funnel counts how many users reach each step, like visit, add to cart, checkout and purchase, and the drop-off between steps.

The jaadu stands at the end of a college auditorium corridor watching a big crowd of students enter, with fewer and fewer reaching each doorway until a small group arrives on the stage.

Think of it like this

College fest dance auditions

For the dance team at a college fest in Chennai, lots of students sign up, fewer actually show up, fewer get a callback and only a handful make the final team. If most students drop off between sign-up and showing up, better reminders will help more than better choreography. A product funnel finds that biggest drop.

Watch it happen

October funnel for a grocery app

App open
View item
Add to cart
Purchase

1/4Count distinct users who opened the app this month. That's the top of the funnel.

The big ideas

What you need to know

Count users, not events

One person tapping 'add to cart' five times is one user at that step. Use COUNT(DISTINCT user_id).

Each step needs the last

Only count a user at step 3 if they also did steps 1 and 2. Otherwise the funnel can widen.

Step and overall rates

Step conversion is step N over step N-1. Overall conversion is the last step over the first.

Fix the window

Pick a clear time window, like one month, so the numbers can be compared over time.

See it in code

Walk through the code

sql
1WITH steps AS (2  SELECT3    user_id,4    MAX(CASE WHEN event_name = 'app_open'    THEN 1 ELSE 0 END) AS opened,5    MAX(CASE WHEN event_name = 'view_item'   THEN 1 ELSE 0 END) AS viewed,6    MAX(CASE WHEN event_name = 'add_to_cart' THEN 1 ELSE 0 END) AS carted,7    MAX(CASE WHEN event_name = 'purchase'    THEN 1 ELSE 0 END) AS bought8  FROM events9  WHERE event_time >= TIMESTAMP '2026-10-01'10    AND event_time <  TIMESTAMP '2026-11-01'11  GROUP BY user_id12)13SELECT14  SUM(opened)                              AS step1_opened,15  SUM(opened * viewed)                     AS step2_viewed,16  SUM(opened * viewed * carted)            AS step3_carted,17  SUM(opened * viewed * carted * bought)   AS step4_bought,18  ROUND(100.0 * SUM(opened * viewed * carted * bought)19        / NULLIF(SUM(opened), 0), 1)       AS overall_pct20FROM steps;

1/5Collapse all events to one row per user, so heavy users don't count more than once.

Works in PostgreSQL, MySQL and most warehouses. This version checks that users did each step, not the order they did them in. For strict order, compare each user's first timestamp per step.

Save yourself the pain

Common mistakes

Oops

Counting events at each step with COUNT(*).

Do this instead

Count distinct users. Event counts reward people who click repeatedly, not real progress.

Oops

Counting step 4 purchasers who skipped earlier steps, like reorders from a push notification.

Do this instead

Require all earlier steps, or report those users as a separate path so the funnel stays honest.

Your mission

Find the leakiest step

~30 min

Quick check

Why does the query multiply flags, like opened * viewed * carted, for step 3?

Go deeper

  1. 1Count distinct users at each step from an events table.
  2. 2Calculate step-to-step conversion rates.
  3. 3Make sure a user is only counted at a step if they did the earlier steps.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge