Funnel Conversion Analysis Queries with CTEs
Measure step-to-step conversion rates through product funnels in SQL.
In plain words
Count how many people reach each step, in order, and see what share make it from one step to the next.
Think of it like…
A running race with checkpoints: you count how many runners pass checkpoint 1, then how many of those pass checkpoint 2, and so on. A runner who cut across the field to checkpoint 3 doesn't count for it.
Where you’ve seen it
An e-commerce funnel for November: 10,000 users viewed a product, 2,400 added to cart, 900 started checkout and 610 purchased. Step conversion from checkout to purchase is 610 / 900 ≈ 68%; overall conversion is 6.1%.
Words you’ll hear
- Step
- A defined event in the funnel, such as add_to_cart.
- Step conversion
- Users reaching step n ÷ users reaching step n−1.
- Ordered funnel
- A step counts only if it happened after the previous step.
- Conversion window
- The maximum time allowed from the first step to the last, such as 7 days.
Visual diagram
Theory & example queries
One row per user with step timestamps
Start by pivoting events into one row per user with the first time they did each step: MIN(CASE WHEN event = 'view' THEN ts END) AS t_view, and so on. This turns millions of events into a compact table that's easy to reason about.
Then apply the order: a user reaches "cart" only if t_cart > t_view, "checkout" only if they reached cart and t_checkout > t_cart. Count users who satisfy each condition. Taking the first occurrence of each step is a simplification; strict funnels look for the first cart event after the first view, which needs a slightly more careful query.
| id | created_at | archived |
|---|---|---|
| 1 | 2024-11-20 | false |
| 2 | 2024-12-15 | false |
| 3 | 2026-01-05 | false |
WITH u AS ( SELECT user_id, MIN(ts) FILTER ( WHERE event = 'view' ) AS v, MIN(ts) FILTER ( WHERE event = 'cart' ) AS c, MIN(ts) FILTER ( WHERE event = 'purchase' ) AS p FROM events WHERE ts >= '2026-11-01' GROUP BY user_id )SELECT COUNT(v) AS viewed, COUNT(*) FILTER ( WHERE c > v ) AS carted, COUNT(*) FILTER ( WHERE c > v AND p > c ) AS purchasedFROM u;PostgreSQL is the lesson baseline unless noted otherwise.
Read this line together with the surrounding statement as part of the same SQL expression.
Expected output1 sample row
| viewed | carted | purchased |
|---|---|---|
| 10000 | 3200 | 840 |
Note: Illustrative funnel counts for events since 2026-11-01, with later-step ordering enforced by the timestamp predicates.
Key takeaway: Pivot to one row per user with step times, then enforce order when counting.
Windows, segments and common traps
Add a conversion window by requiring the last step within some time of the first (p <= v + interval '7 days'), otherwise a purchase three months later counts as conversion from an old view. Segment the funnel by device, channel or country by adding that column to the per-user table and grouping by it.
Count users, not events: a user who adds ten items to the cart is still one user at that step. Decide how to treat users who skip steps (purchasing via a "buy now" button without a cart), and say whether the funnel is open (users can enter at any step) or closed (they must start at step 1).
COUNT(*) FILTER (WHERE c > v AND p > c AND p <= v + interval '7 days') AS purchased_7d
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Define the steps and period
List the events in order, the date range of users entering the funnel, and the conversion window.
view → cart → checkout → purchase, 7 days
When to use it & trade-offs
What it is
A funnel measures how many users complete each step of an ordered process (view product → add to cart → checkout → purchase) and the conversion rate between steps. In SQL you find, per user, when they first did each step, keep only steps done in the right order (and often within a time window), then count users at each step.
Why it matters: Funnels show exactly where users drop out, which is what product and growth teams act on. Funnel queries are a common analytics interview problem, because doing them correctly means counting users rather than events and enforcing the step order.
When to reach for it
- Checkout and signup conversion analysis
- Finding the biggest drop-off step
- Comparing funnels across devices or channels
When to avoid or be careful
- Counting events instead of distinct users
- Ignoring step order
- Running a funnel without a conversion window
Ways to compute a funnel
Strengths: One pass over events; clear, testable conditions.
Watch out: First-occurrence logic is a simplification for repeated attempts.
Strengths: Precise ordering per attempt.
Watch out: More joins and slower on large event tables.
Strengths: Fast and visual.
Watch out: Definitions are hidden and hard to reconcile with SQL numbers.
Interview signals & numbers to know
When this topic comes up
- “Where do users drop off?”
- “Conversion rate from cart to purchase”
- Signup or checkout funnel
- Ordered events
- Growth analytics
Benchmarks & rules of thumb
- n ÷ (n−1)
- step conversion
- last ÷ first
- overall conversion
- 1 row
- per user in the pivot step
Common mistakes & follow-ups
Common interview mistakes
- Counting add_to_cart events and reporting more carts than viewers.
- Counting a purchase that happened before the user's first view as a conversion.
- Comparing funnels built with different conversion windows.
Likely interviewer follow-ups
- How would you enforce that each step happens after the previous one?
- How would you show the funnel separately for mobile and desktop?
- How do you handle users who skip a step?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Funnel Conversion Analysis Queries with CTEs with hands-on SQL practice and interview Q&A.