SQL Interview & Case Lab
Back to Business SQL Patterns

Funnel Analysis with Self-Joins and Window Functions

Two ways to write a funnel — self-joins (simple, slow) and window functions (fast, scalable). FIND_VIDEO: search 'SQL funnel analysis conversion' — recommended channel: Mode Analytics. Aim for 12 min or under.

21 minutesVideo LessonPDF notes
🎯 Free Guest Mode: You are learning for free. Sign in to save your completion progress and quiz answers.
Loading progress...

Key moments

  1. Funnel Analysis Overview — Krishna introduces funnel analysis concepts and explains how tech companies track user drop-off across journey stages.
  2. Sample Funnel Metrics — Walks through an example e-commerce conversion funnel from landing page to completed transaction.
  3. Step Counts with UNION ALL — Writes conditional COUNT DISTINCT queries and combines them with UNION ALL to aggregate step counts.
  4. Lagged Window Ordering — Encapsulates step counts in a CTE and applies LAG() ordered by a custom CASE expression to fetch previous step counts.
  5. Conversion Rate Calculation — Calculates step-to-step conversion percentages, rounds decimals, and highlights float division behavior across SQL dialects.
PDF notes

Frequently asked questions

Why does LAG() return NULL for the very first step in the funnel?

LAG() retrieves the preceding row value. Because the first step has no prior row in the window, it naturally returns NULL.

Why is COUNT(DISTINCT user_id) required instead of COUNT(*)?

Users may visit the same step multiple times. DISTINCT ensures each user is counted exactly once per funnel stage.

How do you calculate baseline conversion relative to the first step?

Replace LAG(users) with FIRST_VALUE(users) OVER (ORDER BY stage_order) or join against the landing page user count.

How was this lesson?

Your feedback helps us refine explanations and catch bugs.