SQL-Based Data Cleaning and Anomaly Detection Questions
Using SQL to profile, clean, and validate data: detecting duplicates, nulls, outliers, and referential-integrity breaks; writing production-safe diagnostic queries; and root-cause debugging of missing or incorrect data. Covers anomaly-detection queries and data cleaning for analytics. The hands-on SQL data-quality skill.
Standard deviation-based outlier detection breaks down when the underlying data is skewed, because the mean and standard deviation themselves get pulled by the outliers. Write a SQL-based robust alternative using the median and median absolute deviation (MAD), computed with window functions over a rolling window, and explain the threshold rule you'd use to call a day anomalous.
Write a query that compares today's row count (or a daily metric like DAU or transaction count) against a rolling baseline, such as the trailing 28-day median for the same weekday, and flags an alert if today's value is outside a defined tolerance band (for example below 70% or above 130% of the baseline). Discuss how you'd choose the tolerance band and baseline window, and how the check should behave on days with unusually low absolute volume.
You need to validate a data-quality metric (for example, a duplicate rate or a null rate) on a table with tens of billions of rows, where a full scan is too slow or too expensive to run regularly. Describe a statistically sound sampling strategy: how you would choose the sample size and stratification (for example by partition or by day), compute a confidence interval around the estimate, and decide when the sample result is close enough to a threshold that you should escalate to a full scan instead.
Write a query to flag rows where a timestamp is logically impossible: it is set in the future relative to now, or it predates a sane earliest-possible date for the business (for example, before the year 2000). Explain common causes of this kind of error (client clock skew, default/sentinel dates, timezone mistakes) and how you'd prevent it upstream.
Write a query that computes the daily null rate for a required column, grouped by source, over the last 60 days, and flags days where that day's null rate is a statistical outlier relative to a trailing baseline (mean plus three standard deviations over the preceding 28 days) rather than against a fixed threshold. Discuss how you'd avoid false positives when the sample size on a given day is small.
Unlock Full Question Bank
Get access to all SQL-Based Data Cleaning and Anomaly Detection interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.