KatInterview

Multi-Stage Funnel Analysis in SQL: Windows, Dedup & Rollups

SQL & DataModel answer
HardModel answerSQL & Datafunnel-analysiswindow-functionsCTEs

Last updated

Write ANSI-SQL to build a windowed conversion funnel, attribute acquisition channels, flag bot-like campaigns, deduplicate near-duplicate events, and compute rolling unique viewers.

Context

You are working with an event stream from a video streaming platform. All user activity is captured in a flat events table alongside user profiles and a content catalog. Use today = 2025-09-01 for any relative date calculations.

Schema

users(user_id STRING, created_at TIMESTAMP, country STRING, platform STRING)

events(
  event_ts       TIMESTAMP,
  user_id        STRING,
  event_type     STRING,   -- one of: impression, click, signup, start_subscription, watch_start, watch_end
  show_id        STRING NULL,
  session_id     STRING,
  revenue_cents  INT NULL,
  campaign_id    STRING NULL
)

shows(show_id STRING, title STRING)

Sample Data

users

user_idcreated_atcountryplatform
u12025-08-10 12:00:00USiOS
u22025-08-25 09:00:00USWeb
u32025-08-30 08:30:00CAAndroid
u42025-08-31 21:00:00USiOS

events

event_tsuser_idevent_typeshow_idsession_idrevenue_centscampaign_id
2025-08-26 10:01:00u1impressions1sANULLc10
2025-08-26 10:02:00u1clicks1sANULLc10
2025-08-26 10:05:00u1signupNULLsANULLNULL
2025-08-27 11:00:00u1start_subscriptionNULLsA7999NULL
2025-08-30 09:00:00u2impressions2sBNULLc10
2025-08-30 09:01:00u2clicks2sBNULLc10
2025-08-31 12:00:00u2signupNULLsBNULLNULL
2025-09-01 08:00:00u3impressions1sCNULLc99
2025-09-01 08:00:02u3impressions1sCNULLc99
2025-09-01 08:03:00u3clicks1sCNULLc99
2025-09-01 08:20:00u3watch_starts1sCNULLNULL
2025-09-01 08:50:00u3watch_ends1sCNULLNULL
2025-08-31 20:00:00u4impressions3sDNULLNULL
2025-09-01 07:30:00u4clicks3sDNULLNULL
2025-09-01 07:40:00u4signupNULLsDNULLNULL

shows

show_idtitle
s1The Bear
s2Only Murders
s3The Kardashians

Tasks

a) Daily conversion funnel

Build a day-by-day funnel for each calendar date in [2025-08-26, 2025-09-01]. Attribute every user to the day of their first impression. A user advances through each stage only if the qualifying event falls within these windows:

  • Click within 1 day of their impression
  • Signup within 3 days of that click
  • Subscription start within 7 days of that signup

Output columns: day, impressions, clicks_w1d, signups_w3d, subs_w7d, plus the three stage-to-stage conversion rates.

b) Time-to-first-watch by acquisition channel

For users who signed up in August 2025, compute — grouped by signup_date and acquisition_channel — the median number of minutes from signup to their first watch_start. Define acquisition channel as paid if any impression or click in the 7 days before signup had a non-null campaign_id, otherwise organic. Note that signup rows themselves never carry a campaign_id.

c) Suspicious campaign detection

Over the 7-day window [2025-08-26, 2025-09-01], flag any campaign where CTR > 0.80, total impressions ≥ 100, and distinct users ≤ 5. Return campaign_id, impressions, clicks, ctr, distinct_users.

d) Near-duplicate event deduplication

Within each (user_id, event_type, show_id) group, successive events less than or equal to 5 seconds apart are considered duplicates of the earlier one. Keep only the first event in each such burst. Produce a deduplicated ev CTE that all other parts can reference.

e) Bonus — 28-day rolling unique viewers

For each day across August and September 2025, count the distinct users who had a watch_start for each show_id in the trailing 28 days (inclusive). On 2025-09-01, return the top 3 shows by that metric, breaking ties by the highest single-day unique viewer count on that date.


For each part, provide working Snowflake-compatible SQL, note any edge cases in your approach, and explain how your solution holds up on a table with billions of rows (partitioning strategy, clustering keys, approximate aggregations).

Hints

Hint 1

Start with part (d): a single deduplicated CTE that every other query reads from saves you from re-solving dedup logic five times. Use LAG() partitioned by (user_id, event_type, show_id) ordered by event_ts to compare each event against its predecessor — if the gap is ≤ 5 seconds, drop it. Be explicit about how NULLs in show_id are handled: NULLs are grouped together, which is correct for signups.

Hint 2

For the funnel in part (a), resist the urge to do calendar-day comparisons. The windows (1 day, 3 days, 7 days) are timestamp-based intervals anchored to each user's qualifying event, not midnight-to-midnight buckets. Each stage CTE should pick the MIN qualifying timestamp per user per impression-day, then the next stage joins against that timestamp. Use a date spine with a recursive CTE or VALUES…

Hint 3

For the acquisition channel in part (b), the signup event itself has a NULL campaign_id in the data — you cannot read the channel from the signup row. Instead, look backward from the signup timestamp at the user's impression and click events in the prior 7 days and pick the most recent non-null campaign_id (MAX_BY(campaign_id, event_ts) in Snowflake). If none exists, the user is organic. For the r…