InterviewStack.io LogoInterviewStack.io

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

A metric tracked per segment is compared to its own recent history using a simple ratio or percent-change rule (for example: this week's rolling average is more than 50% below, or more than 1.5x above, the prior window) instead of a full z-score model. Write SQL to compute and flag this, explain why a minimum-volume guard is needed to stop noisy low-traffic segments from dominating the alert feed, and discuss when you would reach for this simpler rule instead of the z-score approach.

EasyTechnical
121 practiced

Write an ANSI SQL query to compute monthly ARPU (average revenue per user) for January 2025 by region, using a subscriptions table where a subscriber counts as active in the month if their subscription overlaps it. Explain your assumptions.

HardTechnical
68 practiced

Given orders(order_id, seller_id, status, created_at, canceled_at, amount), compute each seller's cancellation rate over the trailing 90 days, excluding sellers with fewer than 50 orders in that window. Then describe the SQL approach for surfacing which sellers have a week-over-week change in cancellation rate large enough to be statistically significant rather than noise.

HardTechnical
84 practiced

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.

MediumTechnical
81 practiced

An executive dashboard needs month-to-date revenue, period-over-period growth, and a rolling multi-month average revenue-per-user, sometimes with a user-adjustable window size. For each, decide whether you would compute it in the SQL/warehouse layer with window functions or leave it to the visualization tool, and discuss latency, reusability across dashboards, and performance.

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.