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.
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.
What does referential integrity mean for a child table like order_items referencing a parent like orders? Write a query that finds orphan rows in the child table that reference a parent key which doesn't exist, accounting for the parent's primary key sometimes being NULL in the child and for soft-deleted parent rows. Show both a LEFT JOIN ... WHERE parent.id IS NULL form and a NOT EXISTS form, and explain which you would prefer and why.
Design a checksum-based validation to detect mismatches between a source table and its warehouse copy without comparing every column value directly: compute a row-level hash (for example MD5 over a canonicalized set of columns) and a per-day table-level checksum, and explain how you handle column ordering, NULLs, and floating-point values so the checksum is deterministic and comparable across environments.
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.
Given an events or transactions table where the same real-world event can be logged more than once by an upstream retry (arriving with a slightly different timestamp), write a query to detect duplicates defined as the same entity and event occurring within a short time tolerance (for example, within a few seconds or minutes of each other), and keep only one canonical row per duplicate group.
Unlock Full Question Bank
Get access to all 23 SQL-Based Data Cleaning and Anomaly Detection interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.