#1 India's Top IT Training Institute

On Demand Courses

9 programs

×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →

SQL Data Cleaning Guide
PDF Downloads

Download the SQL data cleaning guide PDF from Uncodemy: NULLs, duplicates, trimming, casting and standardising text with tested queries.

Prepared by: Uncodemy trainer team Reviewed by: Uncodemy Last reviewed: 27 Oct 2026
A Cleaning Workflow in SQL
1
Profile the tableRow counts, NULLs per column and distinct values.
2
Find duplicatesOn the columns that define a unique record.
3
Standardise textTRIM, UPPER or LOWER and REPLACE.
4
Handle NULLsCOALESCE or an explicit label.
5
Convert types and datesDo it explicitly, not by accident.
6
Write and validateSave to a view or table; check counts and totals.

Quick Answer

This page offers Uncodemy's SQL data cleaning guide as a PDF download. When data already lives in a database, cleaning it with SQL is fast and repeatable. The guide shows queries to profile data, find and remove duplicates, handle NULLs, standardise text and dates, and validate the result, using examples that run in SQLite.

Download

SQL Data Cleaning Guide PDF

Download the guide below. Each download is its own file, and everything in it is also explained on this page.

1 PDF Available
PDF 01 Main Guide

SQL Data Cleaning Guide

Profiling, finding duplicates, standardising text, handling NULLs, dates and numbers, building a cleaned view and validating it, with a checklist and a cleaning log, using a messy raw_customers table.

PDF Free Oct 2026
Download PDF

A short form asks for your details before the download starts. Your details are used only to share study material and course information.

01

Why clean data in SQL

If data is already stored in a database, SQL is often the quickest way to inspect and fix it, because you avoid moving large files around. A saved SQL cleaning query is also repeatable and auditable: anyone can read exactly what was changed.

The best practice is not to modify the raw table but to create a cleaned view or a new table derived from it, so the original stays intact. This guide uses a messy example table called raw_customers with columns customer_id, name, email, city, signup_date and monthly_spend, which contains duplicates, inconsistent text, NULLs and mixed date styles.

02

A cleaning workflow in SQL

  1. Profile the table: row counts, NULLs per column and distinct values.
  2. Find duplicates on the columns that define a unique record.
  3. Standardise text with TRIM, UPPER or LOWER and REPLACE.
  4. Handle NULLs with COALESCE or an explicit label.
  5. Convert types and dates explicitly.
  6. Write the cleaned result to a view or table and validate counts and totals.
03

Profiling the table

Counts rows, missing values and distinct cities in one query.

SELECT COUNT(*)                                    AS total_rows,
       COUNT(DISTINCT customer_id)                 AS distinct_ids,
       SUM(CASE WHEN email IS NULL THEN 1 ELSE 0 END)         AS missing_email,
       SUM(CASE WHEN monthly_spend IS NULL THEN 1 ELSE 0 END) AS missing_spend,
       COUNT(DISTINCT city)                        AS distinct_city_values
FROM raw_customers;
04

Finding duplicates

Shows customer ids that appear more than once, and then ranks rows within each id so that you can keep one.

-- which ids are duplicated?
SELECT customer_id, COUNT(*) AS copies
FROM raw_customers
GROUP BY customer_id
HAVING COUNT(*) > 1;

-- keep the row with the latest signup_date for each id
SELECT *
FROM (
  SELECT r.*,
         ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY signup_date DESC) AS rn
  FROM raw_customers r
)
WHERE rn = 1;
05

Standardising text and handling NULLs

Trim spaces, fix case, standardise a misspelling and give missing values a default or a clear label.

SELECT customer_id,
       TRIM(name)                                   AS name_clean,
       LOWER(TRIM(email))                           AS email_clean,
       CASE UPPER(TRIM(city))
            WHEN 'NEW DELHI' THEN 'Delhi'
            WHEN 'DELHI'     THEN 'Delhi'
            ELSE SUBSTR(UPPER(TRIM(city)), 1, 1) || LOWER(SUBSTR(TRIM(city), 2))
       END                                          AS city_clean,
       COALESCE(monthly_spend, 0)                   AS spend_filled,
       COALESCE(NULLIF(TRIM(email), ''), 'unknown') AS email_or_unknown
FROM raw_customers;
06

Useful cleaning functions

Names vary slightly by database.

TaskFunction idea
Remove spacesTRIM, LTRIM, RTRIM
Change caseUPPER, LOWER
Replace textREPLACE
Part of a stringSUBSTR or SUBSTRING, LEFT, RIGHT
Default for NULLCOALESCE
Turn empty text into NULLNULLIF(column, '')
Conditional fixesCASE WHEN ... THEN ... END
Change typeCAST(column AS type)
Detect patternLIKE; regular expressions where supported
07

Dates and numbers

Dates are a frequent source of trouble: text values in different styles, impossible dates and time zones. Where possible store dates in a proper date type and write them in ISO form, year-month-day, which sorts correctly as text too. When converting, test the conversion on a sample and check for rows that fail.

For numbers, watch for values stored as text, currency symbols and commas. CAST a cleaned string to a number, but first remove characters that do not belong, and report rows that could not be converted instead of hiding them.

08

Creating a cleaned view and validating it

A view keeps the raw data intact. The check afterwards compares counts with the raw table.

CREATE VIEW clean_customers AS
SELECT customer_id,
       TRIM(name)         AS name,
       LOWER(TRIM(email)) AS email,
       TRIM(city)         AS city,
       COALESCE(monthly_spend, 0) AS monthly_spend
FROM (
  SELECT r.*, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY signup_date DESC) AS rn
  FROM raw_customers r
)
WHERE rn = 1;

SELECT (SELECT COUNT(*) FROM raw_customers)        AS raw_rows,
       (SELECT COUNT(*) FROM clean_customers)      AS clean_rows,
       (SELECT COUNT(DISTINCT customer_id) FROM raw_customers) AS raw_distinct_ids;
09

Common SQL cleaning mistakes — and keeping a cleaning log

Common SQL cleaning mistakes

  • Updating the raw table in place and losing the original.
  • Deleting duplicates without deciding which row to keep.
  • Comparing with NULL using = instead of IS NULL.
  • Filling NULLs with zero when a missing value should stay unknown.
  • Not validating row counts and totals afterwards.

Keep a cleaning log

  • Record each rule, such as treat blank strings as NULL.
  • Note how many rows each rule changed.
  • Keep the raw table unchanged and write cleaned data into a new table or view.
10

Document your rules

Write a short note or comments in the SQL for every rule: what the problem was, how you fixed it and how many rows it touched. The count is easy to get with a query before and after. These notes turn a one-off fix into reusable knowledge, and they make it easy for a colleague to review your work.

11

Data cleaning checklist for SQL

CheckQuery idea
Count rows and distinct keysSELECT COUNT(*), COUNT(DISTINCT id) FROM table
Find NULLs per columnSUM(CASE WHEN col IS NULL THEN 1 ELSE 0 END)
Find duplicatesGROUP BY key HAVING COUNT(*) > 1
Find leading and trailing spacesWHERE col <> TRIM(col)
Check value rangesMIN, MAX and ordered samples
Check category spellingsSELECT DISTINCT col ORDER BY col

Learn with a trainer

Want to clean real data the way analysts do at work? Explore Uncodemy's Data Analytics course and learn from industry trainers.

View Course
About these PDFs: These PDFs are study material for learning and revision. They are not a certificate, a degree or a promise of a job, and technology changes, so check official documentation for the latest details. Spot an error? Write to info@uncodemy.com and the trainer team will review it.
FAQ

Frequently Asked Questions

How do I remove duplicates in SQL?
Decide which columns define a unique record, then use ROW_NUMBER over a partition by those columns and keep rn equal to 1, or group and aggregate. Avoid deleting from the raw table without a backup.
How do I handle NULL values in SQL?
Use IS NULL to find them, COALESCE to supply a default, and NULLIF to turn empty strings into NULL. Choose defaults carefully.
How do I standardise text in SQL?
Use TRIM to remove spaces, UPPER or LOWER to normalise case, REPLACE for fixed substitutions and CASE for mapping variants to one label.
Should I clean data in SQL or Python?
If data is in a database and rules are simple, SQL is efficient and repeatable. Python suits complex logic or files outside a database.
Why create a view for cleaned data?
A view leaves the raw table untouched and shows exactly what rules you applied.
Are these PDFs free to download?
Yes, the SQL data cleaning guide PDF is free. A short form asks for your details before the download starts.