Stop 14 · SQL for Data AnalysisTopics · 1/9

Data Cleaning in SQL

Part of the SQL Roadmap

In short

Real data arrives messy: stray spaces, 'Bangalore' and 'Bengaluru' as two cities, amounts stored as text. Clean it in SQL first, or every number in your report will be quietly wrong.

Cleaning in SQL means fixing types, trimming text, standardizing categories, handling NULLs and filtering out bad rows before analysis.

At a kitchen sink, the jaadu rinses muddy carrots and tomatoes under a tap, tossing a squashed tomato into a bin and sorting clean vegetables into neat baskets.

Think of it like this

Washing vegetables from the mandi

You come back from the sabzi mandi with a bag of vegetables. Before cooking, you rinse off the mud, trim the stalks, throw out the rotten tomato and sort onions from potatoes. Nobody cooks straight from the bag. Raw tables need the same wash: trim, standardise, convert types, handle blanks and drop the bad rows.

Watch it happen

One messy row on its way to clean

Raw row
Trim
Standardise
Fix types
Handle blanks
Filter bad rows

1/6Raw: name ' rahul SHARMA ', city 'bangalore', amount '1,299', phone ''.

The big ideas

What you need to know

Trim and fix case

TRIM, LOWER, UPPER and INITCAP fix the most common text mess, like trailing spaces and random capitals.

Standardise categories

Map spelling variants to one value with CASE, or better, with a small lookup table you join to.

Correct types

Clean the text first, then CAST it. Casting '1,299' straight to NUMERIC throws an error.

Never overwrite raw data

Build a cleaned view or table on top of the raw one, so you can always check what changed.

See it in code

Walk through the code

sql
1CREATE VIEW clean_customers AS2WITH cleaned AS (3  SELECT4    customer_id,5    INITCAP(TRIM(full_name)) AS full_name,6    LOWER(TRIM(email))       AS email,7    CASE8      WHEN LOWER(TRIM(city)) IN ('bangalore', 'bengaluru', 'blr') THEN 'Bengaluru'9      WHEN LOWER(TRIM(city)) IN ('bombay', 'mumbai')              THEN 'Mumbai'10      ELSE INITCAP(TRIM(city))11    END                      AS city,12    NULLIF(TRIM(phone), '')  AS phone,13    CAST(NULLIF(REPLACE(TRIM(signup_amount), ',', ''), '') AS NUMERIC(10,2)) AS signup_amount,14    signup_date15  FROM raw_customers16)17SELECT *18FROM cleaned19WHERE email LIKE '%_@_%._%'20  AND signup_date <= CURRENT_DATE;

1/5A view keeps raw_customers untouched. Anyone can compare raw and clean at any time.

PostgreSQL syntax, assuming the raw columns are text. MySQL has no INITCAP, so skip it or build it with CONCAT, UPPER and SUBSTRING.

Save yourself the pain

Common mistakes

Oops

Running UPDATE on the raw table to 'fix' it.

Do this instead

Keep raw data as it came. Put cleaning logic in a view or a new table so it can be checked and rerun.

Oops

Filtering bad rows silently and moving on.

Do this instead

Count how many rows each rule removes and mention it in your report. Losing 30% of rows changes the story.

Your mission

Clean a messy table

~30 min

Quick check

Why does CAST('1,299' AS NUMERIC) fail, and what's the fix?

Go deeper

  1. 1Profile a raw table: row count, NULLs per column and distinct values per category.
  2. 2Create a cleaned view rather than changing the raw table.
  3. 3Document every cleaning rule you apply.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge