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.
Real-World Example: Passing Through Obstacle Course Checkpoint Gates
- 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.
Visual Infographic: The SQL Funnel Query Architecture
SQL LOGIC INFOGRAPHICAggregate first timestamp for each step per user (`MIN(event_timestamp)`).
Verify: `step_2_time >= step_1_time AND step_2_time <= step_1_time + INTERVAL 1 HOUR`.
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 Step | SQL Logic in CTE | Unique Users | Step Conversion | Overall Conversion |
|---|---|---|---|---|
| Step 1: Product View | `MIN(CASE WHEN event = "view_item" THEN timestamp END)` | 1,000,000 | 100.0% | 100.0% |
| Step 2: Add to Cart | `MIN(CASE WHEN event = "add_to_cart" AND timestamp > step_1 THEN timestamp END)` | 450,000 | 45.0% | 45.0% |
| Step 3: Checkout Initiated | `MIN(CASE WHEN event = "begin_checkout" AND timestamp > step_2 THEN timestamp END)` | 270,000 | 60.0% | 27.0% |
| Step 4: Payment Confirmed | `MIN(CASE WHEN event = "purchase" AND timestamp > step_3 THEN timestamp END)` | 189,000 | 70.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.
Writing Multi-Step Conversion Funnel Queries in SQL
Tracking 100,000 App Visitors Across the 4-Step Ordering Journey
Step-by-Step UI Tutorial: Executing a Production Funnel Query
Open your cloud database query tool.
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)`.
In outer query: `SELECT COUNT(t1) AS step1, COUNT(CASE WHEN t2 > t1 THEN 1 END) AS step2 FROM user_steps`.
Execute query and verify conversion percentages.
Production Multi-Step Funnel SQL Template
SQL CHEAT SHEET| query Section | code 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
- Write a 3-step SQL funnel query (Signup -> Search -> Book) using conditional aggregation.
- Explain what error occurs if you forget to check `step_2_time > step_1_time` in your SQL query.