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).
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.
A metric such as daily revenue or a per-segment event count is monitored with a rolling N-day mean and standard deviation, flagging a day as anomalous when its z-score exceeds a threshold. Write the SQL, then explain two ways this naive approach breaks in practice: it fires false positives on days with genuine weekly seasonality (a Monday should be compared to prior Mondays, not to the raw trailing window), and the underlying mean/variance estimate itself gets noisy early in a series or for low-volume segments.
Build a recurring operational report for a service or pipeline: success rate, average latency, volume, and the week-over-week change in success rate, using a window function for the period-over-period comparison. Then extend it: for a related operational timing metric (for example, time between two lifecycle events of the same entity), compute its distribution (median, broken out by hour of day) rather than only an average, and explain what a single average would hide.
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.
Describe how timezone differences and daylight-saving transitions can affect a daily cohort assignment or any window-based aggregate that buckets events by calendar day. Provide SQL that normalizes event timestamps to UTC and then assigns the bucket consistently across regions, and list the pitfalls and testing strategies you would use to catch a timezone-boundary bug before it reaches production.
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 ContinueJoin thousands of developers preparing for their dream job.