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).

HardTechnical
82 practiced

Compute a Kaplan-Meier-style survival curve for user retention using pure SQL. Given each user's signup date and their activity dates, produce a table with one row per time interval (for example, week) showing the at-risk count, the churn count, and the survival probability for that interval, carrying the correct cumulative product forward. Explain how you handle right-censoring: users who have not yet had the chance to churn because too little time has passed since they signed up.

MediumTechnical
112 practiced

Given a transactions table (transaction_id, user_id, amount, occurred_at), write SQL to flag a transaction as anomalous when its amount exceeds that user's own mean plus 3 standard deviations, computed over the user's trailing 365 days. Decide how you handle users with very few historical transactions, and explain your choice.

HardSystem Design
63 practiced

Design a cost-effective system to compute a cohort or rolling user-activity metric at very large scale (order of 1B entities, 100B events/year) with daily updates. Specify storage format, partitioning and clustering strategy, compute-engine choice, incremental-refresh strategy, the trade-off between window functions and pre-aggregation, cost considerations, and monitoring.

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
85 practiced

COUNT(DISTINCT) over a window is unsupported or too slow for your dataset. Design a batch architecture that computes a daily cumulative unique-entity count (for example, unique users per product) across several years of history with incremental updates. Cover storage format, how you maintain and merge approximate-distinct sketches (e.g. HyperLogLog), your backfill approach, and how you would still produce exact counts for small cohorts where the approximation error matters.

Unlock Full Question Bank

Get access to all 18 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.