KatInterview

SQL Window Functions: Streaks, Daily Metrics, and Inactivity Gaps

SQL & DataModel answer
MediumModel answerSQL & Datawindow-functionsgap-and-islandrolling-aggregates

Last updated

Use window functions to detect consecutive-day activity streaks, compute rolling DAU and revenue metrics, and identify post-order inactivity gaps across three related tables.

Assume today is 2025-09-01. All queries must be written in standard SQL (PostgreSQL dialect). You may use CTEs and window functions freely, but no procedural loops and no correlated subqueries.


Schema

users(user_id INT, signup_date DATE)
events(user_id INT, event_date DATE, event_name TEXT)  -- one row per user per day per event type
orders(order_id INT, user_id INT, order_date DATE, amount DECIMAL(8,2))

Sample Data

users

user_idsignup_date
12025-08-20
22025-08-22
32025-08-25

events

user_idevent_dateevent_name
12025-08-29login
12025-08-30login
12025-09-01login
22025-08-26login
22025-08-28login
22025-08-29login
22025-09-01login
32025-08-25login
32025-08-26login
32025-08-27login
32025-09-01login

orders

order_iduser_idorder_dateamount
1012025-08-3020.00
1122025-08-2715.00
1222025-09-0130.00
1332025-08-2612.00

Tasks

Part A — First qualifying 3-day streak per user

A "3-day consecutive activity streak" means a user had at least one event on three calendar days in a row. Find each user's first such streak whose end date falls in 2025-08-26 through 2025-09-01 inclusive.

Output columns: user_id, streak_start_date, streak_end_date.

Constraint: solve using window functions and date arithmetic only — no self-joins against a dates table.


Part B — Per-day metrics for 2025-08-26 through 2025-09-01

For every calendar day d in that range, return:

  • DAU — count of distinct users with at least one event on d
  • revenue_d — total order amount for d (0 if no orders)
  • rolling_7d_dau_avg — trailing 7-day average of DAU ending on d, rounded to 4 decimal places
  • revenue_per_active_user — revenue_d / DAU rounded to 4 decimal places; NULL when DAU is 0

Days with no events must still appear in the result with DAU = 0.


Part C — Earliest post-order inactivity gap per user

A user "triggers" this flag when they placed an order on some date t and had no events at all for a strict 10-day window immediately after t, but then did have an event on some later date u.

Return only the earliest such gap per user.

Output columns: user_id, last_order_date (= t), gap_days, first_post_gap_event_date (= u).

Hints

Hint 1

Part A: Before applying LAG, collapse events to one row per (user_id, event_date). With fully deduplicated dates, if the active date two positions back equals the current date minus 2, all three intermediate dates must be filled in — that's your streak. Filter streak ends to the target window after the window function runs, not before, or you'll cut off streaks that start before 08-26.

Hint 2

Part B: Build a date spine that starts 6 days before your output window begins. Without it, the 7-day trailing average on 08-26 only sees one day instead of seven. Join aggregated events and orders separately to the spine using LEFT JOIN, then apply AVG(...) OVER (ROWS BETWEEN 6 PRECEDING AND CURRENT ROW). Apply the output-window filter in the outermost SELECT, after the rolling window is computed…

Hint 3

Part C: For each user, find their last order date and the date of their next event after that order. The gap is (next_event_date - last_order_date - 1). Use LEAD or a ranked subquery to get the first event after each order date, then filter to gaps >= 10 and pick the earliest one per user with ROW_NUMBER().