Mastering Product Analytics with Mixpanel & GA4Lesson 26 of 32
Lesson 26 of 32

Writing Multi-Step Conversion Funnel Queries in SQL

Overview & Learning Objectives

While visual tools like Mixpanel allow you to drag-and-drop funnel steps, real-world data science workflows require writing pure SQL queries to audit funnels with complex custom rules: "Only count users who completed Step 2 within 10 minutes of Step 1, while holding item category constant." In this lesson, you will master the two primary SQL funnel architectures: Conditional Aggregation (`COUNT(CASE WHEN ...)`) and Self-Joining Sequential CTEs.

  • Write multi-step conversion funnel queries in SQL using Conditional Aggregation.
  • Enforce time-window bounds (e.g., Step 2 must occur within 2 hours of Step 1) using timestamp comparisons.
  • Calculate Step-to-Step Conversion Percentages and Overall Top-to-Bottom Conversion in a single query.
Example

Real-World Example: Passing Through Obstacle Course Checkpoint Gates

PRACTICAL CONTEXT
  • Scenario: Athletes clearing sequential checkpoint gates in a steeplechase obstacle course.
  • Query Logic: Requires step 2 timestamp to be strictly greater than step 1, within an allowed conversion time bracket.
  • Output: Produces accurate cohort-level conversion rates without vendor dashboard sampling limits.
Corporate / Tech Term Everyday / Practical Metaphor Operational Meaning
Step 1 (Top of Funnel) 100 runners crawling under Gate 1 `COUNT(DISTINCT user_id) AS step_1_users`
Step 2 (Middle of Funnel) 60 runners reaching the top of Gate 2 rope `COUNT(DISTINCT CASE WHEN step_2_time > step_1_time ...)`
Step 3 (Finish Line) 35 runners crossing the finish ribbon `COUNT(DISTINCT CASE WHEN step_3_time > step_2_time ...)`
Drop-off Rate The runners who sat down on the grass to rest `1.0 - (Step 2 / Step 1)`
Core Principle: Always use `MIN(event_timestamp)` for the initial step to capture the user's FIRST intent, and verify that subsequent steps occurred AFTER the previous step!

Visual Infographic: The SQL Funnel Query Architecture

SQL LOGIC INFOGRAPHIC
Stage 1
Step 1: Base CTE

Aggregate first timestamp for each step per user (`MIN(event_timestamp)`).

Stage 2
Step 2: Sequential Bounds

Verify: `step_2_time >= step_1_time AND step_2_time <= step_1_time + INTERVAL 1 HOUR`.

Stage 3
Step 3: Conversion Math

Compute `ROUND(step_2 / step_1 * 100, 2)` to display neat percentages.

Industry Standard & Framework

Standard: SQL:2016 Analytical Query Standards & E-Commerce Funnel Formulation.

Real-World Business Application: Flipkart Big Billion Days Flash Sale Checkout Funnel Query

Flipkart data analysts write an optimized BigQuery SQL script to monitor real-time smartphone flash sale conversion.

Funnel StepSQL Logic in CTEUnique UsersStep ConversionOverall Conversion
Step 1: Product View`MIN(CASE WHEN event = "view_item" THEN timestamp END)`1,000,000100.0%100.0%
Step 2: Add to Cart`MIN(CASE WHEN event = "add_to_cart" AND timestamp > step_1 THEN timestamp END)`450,00045.0%45.0%
Step 3: Checkout Initiated`MIN(CASE WHEN event = "begin_checkout" AND timestamp > step_2 THEN timestamp END)`270,00060.0%27.0%
Step 4: Payment Confirmed`MIN(CASE WHEN event = "purchase" AND timestamp > step_3 THEN timestamp END)`189,00070.0%18.9%

Step-by-Step UI Tutorial & Visual Workflow Guide

Below is the practical, non-technical walkthrough showing how Product Analysts design, review, and execute this capability inside real-world tools like Mixpanel, Google Analytics 4, BigQuery, and Google Tag Manager.

Conversion

Writing Multi-Step Conversion Funnel Queries in SQL

Tracking 100,000 App Visitors Across the 4-Step Ordering Journey

CONVERSION FUNNEL
1. App Opened / Home Feed
100% 100,000 users
2. Product Viewed (Kaju Katli)
68.2% 68,200 users
↓ -31.8% Drop-Off
3. Added to Cart
32.1% 32,100 users
↓ -52.9% Cart Leak
4. Checkout Completed (Paid)
12.4% 12,400 users
↓ -61.4% Drop-Off
Overall Funnel Conversion: 12.4% (12,400 customers converted out of 100,000) Biggest Leak: Step 2 → Step 3 (-52.9% drop)
BigQuery / Postgres SQL Studio

Step-by-Step UI Tutorial: Executing a Production Funnel Query

Step 1
Step 1: Open Query Editor

Open your cloud database query tool.

Step 2
Step 2: Write User Milestones CTE

Write: `WITH user_steps AS (SELECT user_id, MIN(CASE WHEN event = "view" THEN event_time END) AS t1, MIN(CASE WHEN event = "cart" THEN event_time END) AS t2 FROM events GROUP BY 1)`.

Step 3
Step 3: Apply Conditional Count

In outer query: `SELECT COUNT(t1) AS step1, COUNT(CASE WHEN t2 > t1 THEN 1 END) AS step2 FROM user_steps`.

Step 4
Step 4: Run & Inspect Step Drop-offs

Execute query and verify conversion percentages.

Production Multi-Step Funnel SQL Template

SQL CHEAT SHEET
query Sectioncode Sample
CTE 1: User Timestamps`SELECT user_id, MIN(CASE WHEN event_name = "view_item" THEN event_timestamp END) AS step_1_time, MIN(CASE WHEN event_name = "add_to_cart" THEN event_timestamp END) AS step_2_time FROM raw_events GROUP BY 1`
CTE 2: Chronological Filtering`SELECT user_id, step_1_time, CASE WHEN step_2_time > step_1_time AND step_2_time <= step_1_time + INTERVAL 2 HOUR THEN step_2_time END AS step_2_time FROM user_timestamps`
Final Step: Aggregation & Math`SELECT COUNT(step_1_time) AS s1_users, COUNT(step_2_time) AS s2_users, ROUND(COUNT(step_2_time) * 100.0 / COUNT(step_1_time), 2) AS conv_rate FROM filtered_steps`

Implementation Guidelines for Product Analysts

  • Always enforce `step_2_time > step_1_time` to prevent users who added an item to cart yesterday and viewed a different item today from being counted as converted.
  • Use `COUNT(DISTINCT user_id)` to ensure users who go through the funnel multiple times are not overcounted.
  • Include a conversion window filter (`INTERVAL 2 HOUR` or `INTERVAL 24 HOUR`) to make your SQL funnel realistic.

Practical Hands-On Exercises

  1. Write a 3-step SQL funnel query (Signup -> Search -> Book) using conditional aggregation.
  2. Explain what error occurs if you forget to check `step_2_time > step_1_time` in your SQL query.