Advanced SQL: Metric Monitoring, Anomaly Detection, and Data Correctness at Scale Questions

SQL for monitoring a business or operational metric over time and catching when it is wrong or behaving abnormally, as distinct from the mechanics of the SQL constructs used to compute it (window function syntax, frame semantics, CTEs, and subqueries are covered by the companion topic Advanced SQL: Window Functions, CTEs, and Subqueries), from defining and computing cohort retention, funnel conversion, DAU/WAU/MAU, and lifetime value themselves (covered by the companion topics User Retention & Engagement, Conversion Funnel Optimization, and SQL for Data Analysis), and from sessionization via inactivity-gap detection (also covered by the companion window-functions topic). Covers rolling-window anomaly and change-point detection (z-score and threshold-based, including seasonality-aware baselines), testing whether a period-over-period change in a rate metric is statistically significant versus noise, decomposing a metric's change into which segments drove it, average revenue per user (ARPU) at both a simple and a rolling/at-scale altitude, keeping cumulative and rolling metrics correct against missing dates, late-arriving or out-of-order events, and incremental (not full-recompute) materialization at very large scale, approximate-distinct (HyperLogLog) computation, standalone timezone/daylight-saving-aware day bucketing, Kaplan-Meier-style survival curves computed in pure SQL, and multi-state subscription churn and retention (renewals, expansions, downgrades, and pauses at the plan level, not just customer presence/absence).

MediumTechnical
87 practiced

Churn detection: Given a user_events table (user_id, occurred_at) and a churn definition of no activity for 30 consecutive days, write SQL that determines churn_date for each user when applicable. churn_date should be the first date after which the user had no activity for 30 days. Also show how to report users currently active (not churned as of analysis_date).

HardTechnical
73 practiced

Given orders(order_id, user_id, order_date, amount) and refunds(refund_id, order_id, refund_amount), write efficient SQL to compute net revenue per user and flag users with outlier net revenue using a z-score threshold of 3. Discuss the optimizations you would apply to run this on a dataset of 100M+ rows.

MediumTechnical
63 practiced

Given the following tables: users(user_id, created_at timestamp), sessions(session_id, user_id, started_at timestamp, duration_seconds), purchases(purchase_id, user_id, amount numeric, purchased_at timestamp). Write ANSI SQL (Postgres-compatible) to compute 30-day retention rate and 30-day LTV per signup cohort for cohorts before and after a program rollout date. Describe key assumptions and how you handle users with no purchases.

MediumTechnical
71 practiced

Implement SQL to compute N-day active users (e.g., 7-day active users) given events(user_id, event_date). Show how to compute DAU, WAU (7-day active users), MAU (30-day active users), and retention ratios like DAU/WAU. Discuss efficiency when computing distinct users over sliding windows.

HardTechnical
139 practiced

Given a subscriptions table (customer_id, start_date, plan_level) and an events table capturing plan changes and payments, design SQL to compute cohort revenue-retention curves (month 0, month 1, month 2, ...) that correctly account for renewals, expansions, downgrades, pauses, and churn happening at the plan level rather than treating the customer as a single all-or-nothing state. Describe the edge cases: proration, refunds, multiple overlapping subscriptions per customer, and simultaneous upgrades, and how you would validate the resulting curves are accurate.

Unlock Full Question Bank

Get access to all 21 Advanced SQL: Metric Monitoring, Anomaly Detection, and Data Correctness at Scale interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.