Esc

↑↓ moveEnter open

← All writing
SQL · Product AnalyticsPublished 26 November 20265 min readSajid Hasan Sifat

Funnel Analysis in SQL: From Event Logs to Conversion Rates You Can Trust

How to build a product funnel from raw events in SQL: define the steps, enforce order and a time window, count users rather than events, split by segment, and avoid the mistakes that inflate conversion.

On this page8 sections · 5 min

Most funnel numbers I’ve been handed in meetings were wrong in the same few ways. Someone counted events instead of people. Or let a user “convert” before they started. Or compared a funnel that allowed thirty days with one that allowed thirty minutes.

None of these are hard to fix. They’re just easy to miss, because a funnel query that’s wrong still returns a nice, falling set of numbers.

This guide builds a funnel from a raw event table, one step at a time, and points out where each of those mistakes creeps in.

The data

Assume a single events table, the shape you get from most product analytics pipelines:

column example
user_id u_1042
event_name view_product, add_to_cart, begin_checkout, purchase
event_time 2026-11-02 14:03:11
platform android, ios, web

The funnel we want: view product → add to cart → begin checkout → purchase, for users who first viewed a product in November.

The SQL is PostgreSQL. BigQuery and Snowflake need only small changes, which I’ve noted where they come up.

Step 1: decide what counts as “entering” the funnel

Before writing any SQL, write down two things:

  1. The entry event and the period. Here: the user’s first view_product in November 2026.
  2. The window. How long after entering does a user have to finish? For a shop, 7 days is a common choice. For a sign-up flow, it might be one session.

The window matters more than people expect. A 30-day funnel will always look better than a 1-day funnel on the same data, and comparing them tells you nothing.

with entry as (
  select
    user_id,
    min(event_time) as entered_at
  from events
  where event_name = 'view_product'
    and event_time >= '2026-11-01'
    and event_time <  '2026-12-01'
  group by user_id
)
select count(*) as users_entered from entry;

One row per user, with the moment they entered. Everything else hangs off this.

Step 2: find each later step, in order, inside the window

For each user, we want the first time they did each step after the previous step and before the window closed.

with entry as (
  select user_id, min(event_time) as entered_at
  from events
  where event_name = 'view_product'
    and event_time >= '2026-11-01' and event_time < '2026-12-01'
  group by user_id
),
cart as (
  select e.user_id, min(ev.event_time) as cart_at
  from entry e
  join events ev
    on ev.user_id = e.user_id
   and ev.event_name = 'add_to_cart'
   and ev.event_time >  e.entered_at
   and ev.event_time <= e.entered_at + interval '7 days'
  group by e.user_id
),
checkout as (
  select c.user_id, min(ev.event_time) as checkout_at
  from cart c
  join entry e using (user_id)
  join events ev
    on ev.user_id = c.user_id
   and ev.event_name = 'begin_checkout'
   and ev.event_time >  c.cart_at
   and ev.event_time <= e.entered_at + interval '7 days'
  group by c.user_id
),
purchase as (
  select k.user_id, min(ev.event_time) as purchase_at
  from checkout k
  join entry e using (user_id)
  join events ev
    on ev.user_id = k.user_id
   and ev.event_name = 'purchase'
   and ev.event_time >  k.checkout_at
   and ev.event_time <= e.entered_at + interval '7 days'
  group by k.user_id
)
select
  (select count(*) from entry)    as viewed,
  (select count(*) from cart)     as added_to_cart,
  (select count(*) from checkout) as began_checkout,
  (select count(*) from purchase) as purchased;

Two details carry most of the weight here:

  • ev.event_time > previous_step_time enforces order. Without it, a user who bought something on 1 November and then browsed on 3 November counts as a conversion they never made.
  • The window is measured from entry, not from the previous step. Otherwise each step quietly gets its own 7 days and the whole funnel stretches to 28.

In BigQuery, write timestamp_add(e.entered_at, interval 7 day) instead of + interval '7 days'.

Step 3: turn counts into conversion rates

Raw counts are hard to compare. Two rates are useful, and they answer different questions:

  • Step conversion: of the people who reached this step, what share reached the next one? This tells you where people drop.
  • Overall conversion: of everyone who entered, what share got this far? This tells you how much is left at each point.
-- assumes the four counts from step 2 are in a CTE called f
select
  viewed,
  added_to_cart,
  round(100.0 * added_to_cart  / nullif(viewed, 0), 1)         as view_to_cart_pct,
  began_checkout,
  round(100.0 * began_checkout / nullif(added_to_cart, 0), 1)  as cart_to_checkout_pct,
  purchased,
  round(100.0 * purchased      / nullif(began_checkout, 0), 1) as checkout_to_purchase_pct,
  round(100.0 * purchased      / nullif(viewed, 0), 1)         as overall_pct
from f;

nullif(..., 0) stops a divide-by-zero from failing the whole query when a segment is empty.

Here’s what the output might look like. These numbers are made up:

step users step conversion overall
Viewed product 20,000 — 100%
Added to cart 3,600 18.0% 18.0%
Began checkout 1,980 55.0% 9.9%
Purchased 1,190 60.1% 6.0%

Read the step column first. The biggest drop is view to cart, but that’s normal for most shops. A 40% loss between checkout and purchase is usually the more fixable problem, because those people had already decided to buy.

Step 4: split by segment

A funnel for “everyone” hides more than it shows. Add a segment from the entry event and carry it through:

entry as (
  select distinct on (user_id)
    user_id,
    event_time as entered_at,
    platform
  from events
  where event_name = 'view_product'
    and event_time >= '2026-11-01' and event_time < '2026-12-01'
  order by user_id, event_time
)

Then group by platform in the final select, and count each step’s users per platform. In BigQuery, replace distinct on with qualify row_number() over (partition by user_id order by event_time) = 1.

Take the segment from the entry event and don’t change it later. If a user starts on Android and buys on web, decide upfront which funnel they belong to. Mixing the two quietly double-counts people.

Step 5: time to convert

Conversion rate tells you whether people finish. Time to convert tells you how hesitant they were, and it’s often the first number to move when something breaks.

select
  percentile_cont(0.5) within group (
    order by extract(epoch from (p.purchase_at - e.entered_at)) / 3600
  ) as median_hours_to_purchase
from purchase p
join entry e using (user_id);

Use the median, not the average. A handful of people who buy on day 6 will drag an average far away from what a typical buyer does.

Mistakes I see most often

  • Counting events, not users. Ten add_to_cart events from one person is one person who added to cart. Every step should count distinct users, which the min(...) group by user_id pattern does for you.
  • No order. Without event_time > previous_step_time, the funnel includes steps done in any order, including backwards.
  • Windows that don’t match. A dashboard funnel with a 30-day window and an experiment funnel with a 1-day window will never agree. Write the window in the chart title.
  • Entry period cut off at the end. Users who entered on 30 November haven’t had 7 days yet. Either stop the entry period 7 days before your data ends, or label the last week as incomplete.
  • Duplicate or bot events. If purchase can fire twice on a page refresh, de-duplicate on an order ID before you count anything.
  • Treating the funnel as the cause. A funnel shows where people leave, not why. The next step is to look at the sessions of people who dropped at the worst step, or to run a test.

A quick checklist before you share a funnel

  1. Is the entry event and its period written down?
  2. Is there one window, measured from entry?
  3. Are steps forced into order?
  4. Does every step count distinct users?
  5. Is the last, incomplete stretch of entry dates excluded or labelled?
  6. Is the segment fixed at entry?

If all six are yes, the numbers will hold up when someone asks how you got them.

If you’re preparing for interviews, funnels come up constantly in product analyst rounds. The SQL interview practice page has questions in this style, and the cohort retention guide covers the other half of “are users sticking around?”.

Thanks for reading

There’s more in the writing archive.

Browse all articles