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 reconcile financial transactions recorded across two or three related systems (for example payments, invoices, and a general ledger) where amounts may be in different currencies, rounding conventions differ, and each source can lag the others by a different amount of time. Propose a SQL-based reconciliation approach: matching by transaction ID where present, falling back to a tolerance-aware match on amount, date, and counterpart when it isn't, applying historical FX conversion, and reporting unmatched records and the summary deltas.
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.
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.
Design a daily scheduled check that counts orphan records for a critical relationship (for example, orders whose customer_id no longer exists in customers), tracks that count as a trend over the last 30 days, and distinguishes a one-off blip from a genuine regression. What producer-side and consumer-side fixes would you propose to prevent new orphans from being created going forward?
Write a query that detects a material shift in a categorical column's distribution between two time periods (for example this month versus the prior six months, or this week versus last week): compute the percentage share per category in each period, the relative change, and flag categories whose share changed by more than a threshold (for example 10 percentage points). Discuss how you would avoid false alarms from categories with very few observations, and how you'd handle a category that appears in only one of the two periods.
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.