SQL data cleaning
Turn messy rows into trustworthy data — before it ever reaches a dashboard or a model.
Functions for tidying messy datasets
What you'll take away
Standardise text, types and dates in a single pass
Handle NULLs and bad values without breaking queries
Deduplicate records and keep the newest version
31 entries · click a command to copy
String operations
Remove whitespace from both ends, or only one side.
Normalise text casing before comparisons or grouping.
Substitute a substring throughout a value.
e.g. REPLACE(phone, '-', '')
Measure string length for validation rules.
Extract a fixed number of characters from either end.
Extract a portion of a string by position.
Clean text using a regular expression.
Join values into one string.
NULL and value handling
Return the first non-NULL value — the standard way to replace NULLs.
Return NULL when the column equals a given value, useful to avoid division by zero.
Exclude rows with missing values from a result.
Convert a value to another data type.
Convert safely, returning NULL instead of failing on bad data.
Tip: Great for staging tables with inconsistent inputs.
Apply conditional logic row by row.
Numeric operations
Round a number to a chosen precision.
Round down or up to the nearest integer.
Return the absolute value.
Return the remainder — useful for bucketing and parity checks.
Pick the largest or smallest of several values.
Dates and times
Pull a single component such as year, month or day.
Add an interval to a date. Some engines use DATE_ADD.
Measure the difference between two dates.
Parse a string into a date value.
Render a date as a formatted string.
Round a timestamp down to a period boundary.
Aggregation and deduplication
Collapse rows into groups for aggregate functions.
Filter groups after aggregation — WHERE filters rows before it.
Core aggregate measures for a group.
Find extremes within a group.
Remove duplicate rows from a result set.
Rank rows within a group so you can keep the newest record.
e.g. WITH ranked AS (...) SELECT * FROM ranked WHERE rn = 1
This sheet was curated from reference material in the Skyware AI technical library (SQL data cleaning cheatsheet (Gina Acosta)). Command syntax is factual; explanations, grouping and examples were written by our engineering team. Always verify syntax against your tool or database version.
More cheat sheets
Git commands
A practical Git reference grouped by task: setup, inspecting changes, commits, branches, remotes and undoing mistakes.
OpenPandas essentials
The core Pandas operations you reach for daily, organised by task with short, copyable examples.
OpenPySpark handbook
A structured quick reference for PySpark setup, DataFrame operations, SQL, writing data and model training — distilled from the full guide.
Open