download

Why did a three-step funnel report 150% conversion? Fix the SQL.

Data analytics~10 minReviewed 3 Dec 2026Free · No sign-in

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_idstepts
u1view_plan2026-12-01 10:00:00
u1start_checkout2026-12-01 10:05:00
u1start_checkout2026-12-01 10:08:00
u1pay2026-12-01 10:12:00
u2view_plan2026-12-01 11:00:00
u2start_checkout2026-12-01 11:10:00
u2start_checkout2026-12-01 11:15:00
u3view_plan2026-12-01 12:00:00
u3start_checkout2026-12-01 12:30:00
u3pay2026-12-02 18:00:00
u4pay2026-12-01 13:00:00
u5start_checkout2026-12-01 09:00:00
u5view_plan2026-12-01 14:00:00
u5pay2026-12-01 14:20:00

Fix the SQL to return step1_users, step2_users, and step3_users.

"CODE · SQL"
SELECT step, COUNT(*) AS events
FROM events
GROUP BY step;
Show hints (3)
  1. "ANCHOR ON STEP 1"

    Start with one row per user who fired view_plan, keeping their earliest timestamp t1. Users like u4 never enter the funnel.

  2. "ENFORCE STEP ORDER"

    For step 2, find each user's earliest start_checkout with ts between t1 and datetime(t1, '+24 hours'). That drops u5's earlier event.

  3. "CHECK STEP 3 WINDOW"

    For step 3, require a pay event between t2 and datetime(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%).

"SOLUTION · SQL"
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;
"OUTPUT"
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:

  1. Repeated events: u1 and u2 each fired start_checkout twice, inflating raw step-2 counts to 6 against 4 step-1 events (150%).
  2. Deep-link entry: u4 jumped directly to pay from an invoice link without seeing view_plan. Raw counts add u4 to step 3.
  3. Out-of-order steps: u5 fired start_checkout at 09:00, five hours before view_plan at 14:00, then paid at 14:20. No start_checkout followed view_plan.
  4. Expired conversion window: u3 paid 30 hours after view_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:

UserStep 1 (view_plan)Step 2 (start_checkout)Step 3 (pay)Outcome
u110:0010:05 (first of 2)10:12Converts all 3 steps
u211:0011:10 (first of 2)noneStops at step 2
u312:0012:3030h later (expired)Stops at step 2
u4nonenone13:00 (no step 1)Excluded from funnel
u514:0009:00 (before step 1)14:20 (no valid step 2)Stops at step 1

Why COUNT(t2) and EXISTS stay accurate:

  • s1 collapses each user to one row (MIN(ts)), giving step1_users = 4 (100%).
  • s2 uses a LEFT JOIN plus MIN(e.ts) so repeated start_checkout retries still produce one t2, while users without a valid step 2 keep t2 = NULL. COUNT(t2) ignores NULL, yielding 3 (75%).
  • In step 3, e.ts BETWEEN s2.t2 AND datetime(s2.t1, '+24 hours') evaluates to NULL when s2.t2 is NULL, so u5 cannot skip step 2. Only u1 matches (1 user, 25%).

Common mistakes

  • Using COUNT(DISTINCT user_id) per step without checking timestamps. That fixes repeated clicks but still counts u4's deep link, u5's reversed order, and u3's late payment.
  • Joining events to 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 hours instead of t3 <= t1 + 24 hours. That quietly extends a 24-hour funnel window up to 48 hours.
  • Using an INNER JOIN for step 2. That drops users like u5 from step1_users and artificially inflates step-2 conversion to 100%.

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 each view_plan event (or session ID), match the earliest t2 and t3 inside [t1, t1 + 24h], and count DISTINCT user_id per step.
  • "Should deep-link buyers like u4 be thrown away completely from business reporting?" No. Exclude them from the view_plan funnel 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 →

Original OfferVibe question. SQLite CTEs (WITH) · SQLite date and time functions · SQLite aggregate functions.
© 2026 OfferVibe · Text CC BY-NC-SA 4.0 · code MITFree to share and adapt with credit, not for commercial use. Code samples are free for any use.