How would you build a funnel analysis in SQL, measuring how many users progress through a sequence of steps?
Quick Overview
Asks how to write a SQL funnel analysis that counts how many users move through an ordered sequence of product steps. Tests how you reshape event data, enforce step order and time windows, and compute conversion rates without overcounting.
How would you build a funnel analysis in SQL, measuring how many users progress through a sequence of steps?
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
How would you build a funnel analysis in SQL, measuring how many users progress through a sequence of steps?
Assume an event log with one row per user action, for example `events(user_id, event, ts)`, and a funnel such as view, then add to cart, then buy.
```hint Shape the data
Think about how to turn many event rows per user into one row per user that records when each step happened.
```
```hint Watch the order
Trace your query on a user who added to cart once before viewing and again after, and on a user who bought without a valid cart step.
```
### Clarifying Questions to Ask
- Must the steps happen in strict order, or does reaching a later step in any order count?
- Is the funnel measured per user or per session?
- Is there a time window, such as all steps within 7 days of the first one?
- Which users enter the funnel: everyone who did step 1 in a date range, or a specific cohort?
- Do ties in timestamps count as being in order?
### What a Strong Answer Covers
- How raw events are reduced to per-user step times
- How step order and an optional time window are enforced, cumulatively across steps
- How counts, step-to-step conversion and overall conversion are computed
- Edge cases such as repeated events, ties, NULLs and users whose window has not closed yet
### Follow-up Questions
- How would you report the funnel by daily entry cohort, and which cohorts would you leave out?
- How does the query change if the steps can happen in any order?
- How would you compute the median time between consecutive steps?
- How would you make this run on billions of events?
Overview: Asks how to write a SQL funnel analysis that counts how many users move through an ordered sequence of product steps. Tests how you reshape event data, enforce step order and time windows, and compute conversion rates without overcounting.