Why did a three-step funnel report 150% conversion? Fix the SQL.
Question
You measure a 3-step funnel (view_plan -> start_checkout -> pay). A user converts only when steps occur in order (t1 <= t2 <= t3) within 24 hours of their first view_plan (t1). The starter query reports view_plan = 4, start_checkout = 6 (150%), and pay = 4 (100%).
events (UTC timestamps)
| user_id | step | ts |
|---|---|---|
| u1 | view_plan | 2026-12-01 10:00:00 |
| u1 | start_checkout | 2026-12-01 10:05:00 |
| u1 | start_checkout | 2026-12-01 10:08:00 |
| u1 | pay | 2026-12-01 10:12:00 |
| u2 | view_plan | 2026-12-01 11:00:00 |
| u2 | start_checkout | 2026-12-01 11:10:00 |
| u2 | start_checkout | 2026-12-01 11:15:00 |
| u3 | view_plan | 2026-12-01 12:00:00 |
| u3 | start_checkout | 2026-12-01 12:30:00 |
| u3 | pay | 2026-12-02 18:00:00 |
| u4 | pay | 2026-12-01 13:00:00 |
| u5 | start_checkout | 2026-12-01 09:00:00 |
| u5 | view_plan | 2026-12-01 14:00:00 |
| u5 | pay | 2026-12-01 14:20:00 |
Fix the SQL to return step1_users, step2_users, and step3_users.
SELECT step, COUNT(*) AS events FROM events GROUP BY step;
Show hints (3)
- "ANCHOR ON STEP 1"
Start with one row per user who fired
view_plan, keeping their earliest timestampt1. Users likeu4never enter the funnel. - "ENFORCE STEP ORDER"
For step 2, find each user's earliest
start_checkoutwithtsbetweent1anddatetime(t1, '+24 hours'). That dropsu5's earlier event. - "CHECK STEP 3 WINDOW"
For step 3, require a
payevent betweent2anddatetime(t1, '+24 hours'). Count each user at most once.
Show checked explanation
Raw event counts inflate funnels above 100% because retries count multiple times and deep links skip earlier steps. Anchor one row per user on their first view_plan (t1), find the first start_checkout (t2) inside [t1, t1 + 24h], and require pay inside [t2, t1 + 24h]. The true funnel is 4 -> 3 (75%) -> 1 (25%).
WITH s1 AS ( SELECT user_id, MIN(ts) AS t1 FROM events WHERE step = 'view_plan' GROUP BY user_id ), s2 AS ( SELECT s1.user_id, s1.t1, MIN(e.ts) AS t2 FROM s1 LEFT JOIN events AS e ON e.user_id = s1.user_id AND e.step = 'start_checkout' AND e.ts BETWEEN s1.t1 AND datetime(s1.t1, '+24 hours') GROUP BY s1.user_id, s1.t1 ) SELECT COUNT(*) AS step1_users, COUNT(t2) AS step2_users, SUM(CASE WHEN EXISTS ( SELECT 1 FROM events AS e WHERE e.user_id = s2.user_id AND e.step = 'pay' AND e.ts BETWEEN s2.t2 AND datetime(s2.t1, '+24 hours') ) THEN 1 ELSE 0 END) AS step3_users FROM s2;
step1_users | step2_users | step3_users 4 | 3 | 1
Starter and fixed SQL queries run on SQLite 3.49.1 against the 14-row event table and extra edge cases via Python 3.12.10; official SQLite documentation links checked. Checked by an editor other than the author.
Walkthrough
The starter query fails for four separate reasons on this 14-row log:
- Repeated events:
u1andu2each firedstart_checkouttwice, inflating raw step-2 counts to6against4step-1 events (150%). - Deep-link entry:
u4jumped directly topayfrom an invoice link without seeingview_plan. Raw counts addu4to step 3. - Out-of-order steps:
u5firedstart_checkoutat09:00, five hours beforeview_planat14:00, then paid at14:20. Nostart_checkoutfollowedview_plan. - Expired conversion window:
u3paid 30 hours afterview_plan, outside the 24-hour window.
Tracing every user under strict user-scoped ordering (t1 <= t2 <= t3 and t3 <= t1 + 24 hours) makes the true funnel clear:
| User | Step 1 (view_plan) | Step 2 (start_checkout) | Step 3 (pay) | Outcome |
|---|---|---|---|---|
u1 | 10:00 | 10:05 (first of 2) | 10:12 | Converts all 3 steps |
u2 | 11:00 | 11:10 (first of 2) | none | Stops at step 2 |
u3 | 12:00 | 12:30 | 30h later (expired) | Stops at step 2 |
u4 | none | none | 13:00 (no step 1) | Excluded from funnel |
u5 | 14:00 | 09:00 (before step 1) | 14:20 (no valid step 2) | Stops at step 1 |
Why COUNT(t2) and EXISTS stay accurate:
s1collapses each user to one row (MIN(ts)), givingstep1_users = 4(100%).s2uses aLEFT JOINplusMIN(e.ts)so repeatedstart_checkoutretries still produce onet2, while users without a valid step 2 keept2 = NULL.COUNT(t2)ignoresNULL, yielding3(75%).- In step 3,
e.ts BETWEEN s2.t2 AND datetime(s2.t1, '+24 hours')evaluates toNULLwhens2.t2isNULL, sou5cannot skip step 2. Onlyu1matches (1user,25%).
Common mistakes
- Using
COUNT(DISTINCT user_id)per step without checking timestamps. That fixes repeated clicks but still countsu4's deep link,u5's reversed order, andu3's late payment. - Joining
eventsto itself for step 2 and step 3 without collapsing repeats first. Two retries at step 2 times two retries at step 3 turns one user into four rows. - Checking
t3 <= t2 + 24 hoursinstead oft3 <= t1 + 24 hours. That quietly extends a 24-hour funnel window up to 48 hours. - Using an
INNER JOINfor step 2. That drops users likeu5fromstep1_usersand artificially inflates step-2 conversion to100%.
Follow-up questions
- "What if a user views a plan on Day 1, leaves, and completes all three steps in a fresh visit on Day 5?" Anchoring only on
MIN(ts)ties the user to Day 1 and misses the Day-5 conversion. For repeat-attempt funnels, anchor on eachview_planevent (or session ID), match the earliestt2andt3inside[t1, t1 + 24h], and countDISTINCT user_idper step. - "Should deep-link buyers like u4 be thrown away completely from business reporting?" No. Exclude them from the
view_planfunnel so step drop-offs stay accurate, and track direct checkout conversions in a separate entry-channel metric so total revenue still reconciles. - "What if step 1 and step 2 share the exact same second in a mobile batch upload?" Use inclusive
>=(BETWEEN) when event clocks have 1-second precision and steps can legitimately fire in the same second, or add a monotonic client sequence number to break ties.
What a strong answer covers
Identifies all four flaws (repeats, deep links, wrong order, expired window), writes a user-scoped ordered query giving 4 -> 3 -> 1, and explains how to extend MIN(ts) to multi-attempt funnels.
Open in interactive practice deck →