SQL Boy
TutorialsPlayground

Format & Validate

SQL FormatterSQL MinifierSyntax Validator

Convert

JSON to SQLCSV to SQLSQL to JSONRegex to SQLExcel to SQLSQL Dialect Convertersoon

Visualize

ER Diagram GeneratorSQL Schema Diff

Generate

SQL Mock Data Generator

Analyze

SQL Query ExplainerSQL Query Analyzer

Workflow hubs

FormattingConversionSchemaAnalysis
View all tools
Daily ChallengeInterviewsCheat SheetBlog

© 2026 SQL Boy. Built for developers.

ContactPrivacyTermsAI Usage PolicyEditorial StandardsAboutRSS Feed

New conversation

What can I help with?

Ask about this page, get a hint on a challenge, or explore SQL concepts.

I know what's on this page and can give answers grounded in SQL Boy content.

Current page

Data Cleaning With Sql

/blog/data-cleaning-with-sql

Usage status will load after login
SQL Boy
TutorialsPlayground

Format & Validate

SQL FormatterSQL MinifierSyntax Validator

Convert

JSON to SQLCSV to SQLSQL to JSONRegex to SQLExcel to SQLSQL Dialect Convertersoon

Visualize

ER Diagram GeneratorSQL Schema Diff

Generate

SQL Mock Data Generator

Analyze

SQL Query ExplainerSQL Query Analyzer

Workflow hubs

FormattingConversionSchemaAnalysis
View all tools
Daily ChallengeInterviewsCheat SheetBlog
Back to Blog
Published 2025-12-28
Updated 2026-04-20
7 min read

Data Cleaning with SQL: From Messy to Masterpiece

sqltutorialdata-cleaningetlstrings

Author

SQL Boy Team

Editorial Team at SQL Boy

This article is maintained as part of SQL Boy's hands-on SQL library.

We aim to keep examples runnable, call out dialect differences, and revise unclear sections over time.

Read editorial standardsAbout SQL BoyRequest a correction

If a result or dialect note looks wrong, email [email protected] with the article URL and the section you want reviewed.

In the real world, data is rarely pristine. It comes with trailing spaces, inconsistent capitalization, strange characters, and missing values.

Before you can analyze data, you must clean it. Here are the 4 essential tools in your SQL cleaning kit.

Messy data cleaned into standardized fields
Messy data cleaned into standardized fields

A Real Business Scenario: Preparing CRM Exports for Reporting

One of the most common cleanup jobs is importing a CSV from a CRM, billing tool, or spreadsheet and discovering that the same logical field appears in five slightly different shapes.

You might see:

  • emails with leading or trailing spaces
  • statuses that are sometimes blank and sometimes NULL
  • phone numbers stored with different separators
  • duplicated people whose only difference is capitalization

If you aggregate that data without cleaning it first, the report becomes misleading fast. "Active" and NULL may look like different business states. "[email protected]" and " [email protected] " may behave like different customers. SQL cleaning is not cosmetic. It changes whether your grouped results are trustworthy.

1. COALESCE: The Null-Buster

NULL values can ruin math and look ugly in reports. COALESCE() returns the first non-null value in a list.

SELECT COALESCE(phone_number, 'No Phone') FROM users;

2. TRIM: The Whitespace Whacker

Spaces at the start or end of strings are invisible enemies. They make 'Alice' imply not equal to 'Alice '.

SELECT TRIM(name) FROM users;

3. LOWER / UPPER: The Case Normalizer

'iPad', 'IPad', and 'ipad' are three different strings to a computer. Normalize them to fix grouping.

SELECT LOWER(device_type) FROM devices;

4. REPLACE: The Substitution Surgeon

Great for fixing inconsistent formats, like removing dashes from phone numbers or fixing typos.

SELECT REPLACE(phone, '-', '') FROM users;

A Common Wrong Move: Overwriting Raw Data Too Early

A frequent mistake is applying cleanup functions directly in an UPDATE before you have compared raw values with cleaned values.

UPDATE customers
SET email = LOWER(TRIM(email));

That may be fine eventually, but it is risky as a first step:

  • you lose the original messy value that might matter for debugging imports
  • you may normalize a field incorrectly and not notice until downstream joins stop matching
  • you cannot easily compare "before" and "after" logic once the original input is gone

For exploratory cleanup, start with a SELECT that shows raw and cleaned columns side by side. Promote that logic to an UPDATE only after you understand the impact.

Putting It All Together: Interactive Cleaning

Let's clean a messy customer list.

The Horror Show (Raw Data):

  • Inconsistent names (alice, Bob, CHARLIE)
  • Messy emails with spaces ( [email protected])
  • NULL phone numbers
  • NULL status (should be 'Active')
Interactive SQL
Loading...

The Cleanup Query:

Interactive SQL
Loading...

Tip: Depending on your SQL dialect (PostgreSQL, SQL Server), you might have INITCAP() to capitalize just the first letter (e.g., 'Alice'). Standard SQL doesn't always have INITCAP, so we often settle for UPPER or complex SUBSTRING logic for names.

Boundary and Performance Notes

Cleaning logic can be simple at the row level and still expensive at scale.

  • Wrapping indexed columns in functions such as LOWER(email) can make equality filters and joins less index-friendly unless you also maintain a normalized column or functional index.
  • Repeated cleanup expressions inside many downstream queries are a sign that the normalized shape should become part of your ingestion pipeline.
  • Replacing values blindly can collapse meaningful distinctions. For example, turning both blank strings and NULL into the same label may be wrong if one means "missing from source" and the other means "intentionally empty".
  • Text cleanup rules are often locale-sensitive. Uppercasing names is convenient for grouping, but it may not be the best display format for real customer-facing output.

The right question is not just "How do I clean this text?" but also "At what stage should this normalization happen, and do we need to preserve the raw form too?"

Deduplication

Another huge part of cleaning is removing duplicates.

The best way to deduplicate? Use ROW_NUMBER().

WITH flagged_dupes AS (
  SELECT 
    *,
    ROW_NUMBER() OVER(PARTITION BY email ORDER BY id DESC) as rn
  FROM users
)
SELECT * FROM flagged_dupes WHERE rn = 1; -- Keep only the latest entry

When NOT to Clean Everything in SQL

SQL is strong at deterministic cleanup close to the data. It is not always the best layer for every transformation.

Examples where SQL may not be the best primary tool:

  • complex address parsing that needs specialized libraries or external validation
  • fuzzy matching of names where "close enough" is a product decision, not a simple string rule
  • one-time ad hoc cleanup where the real fix belongs upstream in the export or form validation layer

If the same dirty pattern keeps reappearing, the durable fix is often to repair the data pipeline or input validation, not just to add more cleanup expressions in downstream analytics.

Official References

  • PostgreSQL string functions and operators for vendor documentation on trimming, replacement, casing, and related text operations.
  • SQLite core functions for SQLite behavior of functions such as trim, replace, lower, upper, and coalesce.
  • SQL Server COALESCE documentation for another major-engine reference on null-handling semantics.

Tool Workflow

Use tools when cleaned data still needs to move between formats or test environments

Cleaning is rarely the final step. Once the values are normalized, it often helps to convert them into importable SQL, reusable fixtures, or safer non-production datasets.

CSV to SQL

Turn cleaned spreadsheet exports into runnable INSERT statements when the source data still lives in CSV.

JSON to SQL

Useful when the cleaned records came from API payloads or semi-structured exports rather than flat files.

Data Conversion Workflow Hub

Use the broader conversion path when cleaned rows need to move between CSV, JSON, spreadsheets, and SQL.

Mock Data Generator

Generate extra representative rows once you understand the cleaned shape your downstream systems expect.

Related Articles

  • Generating Massive Test Data with SQL for building larger synthetic datasets after you understand the cleaned target shape.
  • Working with JSON in SQL for cases where the messy fields stay semi-structured instead of being fully flattened.
  • SQL Data Types for deciding when cleaned text should become stricter numeric, date, or boolean columns.

Conclusion

Dirty data leads to dirty insights. By mastering TRIM, COALESCE, and normalization functions, you ensure your analysis is built on a solid foundation.

Share this article:

Topic Path

This article belongs to a larger cluster

If this page matches the problem you are working on, jump to the topic hub to see the surrounding articles in the same path instead of treating this as a one-off post.

Data Prep

Data cleaning, staging, and SQL-ready inputs

Use this path when the hard part is turning messy files, semi-structured payloads, or staging tables into something you can query with confidence.

Open topic hub

Related Articles

sqldata-cleaning

Removing Duplicate Rows in SQL: A Complete Guide

Duplicate data is a common headache. Learn multiple strategies to identify and remove duplicates in SQL, from simple DISTINCT to advanced ROW_NUMBER techniques.

Read more
sqlstrings

Essential SQL String Functions for Data Cleaning and Analysis

Clean, format, and extract data like a pro. Master essential SQL string functions including UPPER, LOWER, TRIM, REPLACE, SUBSTR, and concatenation.

Read more
sqltutorial

SQL for E-Commerce: Analytics That Drive Sales

Master the SQL queries every e-commerce analyst needs. Track best-selling products, monitor inventory health, and build revenue dashboards with real examples.

Read more
Previous

Debugging Common SQL Logic Errors: Why Your Query is Wrong

Next

Calculating Moving Averages and Rolling Windows in SQL

Comments

© 2026 SQL Boy. Built for developers.

ContactPrivacyTermsAI Usage PolicyEditorial StandardsAboutRSS Feed