How to Clean Data: A Step-by-Step Guide for Excel and SQL
An 8-step process for cleaning messy data, with the exact Excel functions and SQL queries to fix duplicates, missing values, stray spaces, bad dates and inconsistent categories.
To clean data, back up the original, profile it to find problems, remove duplicates, handle missing values, fix formatting such as extra spaces, text case, dates and numbers stored as text, standardize categories, check for outliers and document every change. In Excel, use Remove Duplicates, TRIM, CLEAN and SUBSTITUTE. In SQL, use ROW_NUMBER, TRIM, COALESCE and NULLIF.
Photo: Unsplash
On this page
You open the customer export and something’s off. The same customer appears 3 times with slightly different names. Half the phone numbers are blank. Dates show up in 2 formats, and a column of order totals won’t add up because some numbers are secretly text.
Messy data is normal. Every system, import and person who types into a form adds a little chaos. The good news is that most problems fall into a handful of types, and each has a standard fix. This guide walks through an 8-step process for cleaning data, with the exact Excel functions and SQL queries for each step.
Why clean data first
Every report, dashboard and model inherits the problems in its data. Duplicates inflate customer counts. Missing values skew averages. A trailing space makes “Texas” and “Texas ” count as 2 states. If you’re building forecasts, as covered in our guide to predictive analytics, dirty data quietly distorts the results.
Cleaning isn’t glamorous, but it’s where trustworthy analysis starts. The goal isn’t perfection. It’s data accurate enough for the decision at hand, with every change documented.
The 8 most common data problems
| Problem | Example | Excel fix | SQL fix |
|---|---|---|---|
| Duplicate rows | Same order imported twice | Data > Remove Duplicates | ROW_NUMBER() and delete rows numbered above 1 |
| Near-duplicates | “Acme Inc” and “ACME, Inc.” | TRIM, UPPER, SUBSTITUTE, then compare | UPPER(TRIM()) and REPLACE() to build a match key |
| Missing values | Blank email or region | Filter blanks, fill or flag | COALESCE(), IS NULL checks |
| Placeholder values | “N/A”, “none”, “0000” | Find and Replace | NULLIF() |
| Extra spaces and hidden characters | “Texas ” or nonbreaking spaces | TRIM, CLEAN, SUBSTITUTE(CHAR(160)) | TRIM(), REPLACE() |
| Inconsistent case | “new york”, “NEW YORK” | PROPER, UPPER, LOWER | INITCAP(), UPPER(), LOWER() |
| Numbers or dates stored as text | “1,200” won’t sum | VALUE, DATEVALUE, Text to Columns | CAST() or type conversion functions |
| Inconsistent categories | “CA”, “Calif.”, “California” | Lookup table with XLOOKUP or VLOOKUP | CASE or a join to a mapping table |
Before you start: back up and profile
Step 1: Back up and work on a copy
Before you change anything, save the original file or table untouched. Microsoft’s own instructions for Remove Duplicates warn that duplicates are permanently deleted, and recommend copying the original data to another worksheet first.
- Excel: Duplicate the sheet and name it “raw.” Do all cleaning on a separate sheet.
- SQL: Never clean the source table in place. Create a copy or a view:
sql
CREATE TABLE customers_clean AS
SELECT * FROM customers_raw;
Also start a short change log: a list of every rule you apply, such as “removed 214 duplicate orders based on order ID.” It makes your work repeatable and auditable.
Step 2: Profile the data
Profiling means getting a quick picture of what’s wrong before fixing it. Check row counts, blanks per column, distinct values and ranges.
In Excel:
- Turn on filters (Data > Filter) and click each column’s dropdown to see distinct values and blanks.
- Use
=COUNTBLANK(B2:B5000)to count empty cells in a column. - Add a helper column with
=COUNTIF($A$2:$A$5000,A2)>1to flag IDs that appear more than once, or use a PivotTable to count rows per ID. On large ranges, a PivotTable is faster. - Highlight duplicates visually with Home > Conditional Formatting > Highlight Cells Rules > Duplicate Values.
In SQL:
sql
SELECT
COUNT(*) AS total_rows,
COUNT(DISTINCT customer_id) AS unique_customers,
COUNT(*) - COUNT(email) AS missing_emails,
MIN(order_date) AS first_order,
MAX(order_date) AS last_order
FROM orders_clean;
Write down what you find. That list becomes your cleaning plan.
Step 3: Remove duplicates
Decide first what makes a row a duplicate. Is it the same order ID? The same customer email? The same name, date and amount? The rule matters more than the tool.
In Excel:
- Select the data range.
- Go to Data > Remove Duplicates.
- Check only the columns that define a duplicate, such as Order ID.
- Click OK and note how many rows were removed.
Microsoft recommends filtering for unique values first to confirm the result is what you want before removing anything.
In SQL, number the rows within each group and keep only the first. ROW_NUMBER() numbers each row within its partition starting from 1:
sql
WITH ranked AS (
SELECT *,
ROW_NUMBER() OVER (
PARTITION BY order_id
ORDER BY updated_at DESC
) AS rn
FROM orders_clean
)
SELECT * FROM ranked WHERE rn = 1;
Ordering by updated_at DESC keeps the most recent version of each order.
Near-duplicates (“Acme Inc” vs “ACME, Inc.”) need a match key. Create a helper column that strips spaces, punctuation and case, then check duplicates on that key:
- Excel:
=UPPER(SUBSTITUTE(SUBSTITUTE(TRIM(A2),",",""),".","")) - SQL:
UPPER(REPLACE(REPLACE(TRIM(company_name), ',', ''), '.', ''))
Review near-duplicate matches by hand before merging. Automated fuzzy matching can merge records that shouldn’t be merged.
Step 4: Handle missing values
Not every blank is a problem, and not every blank should be filled. For each column, choose 1 of 4 options:
| Option | When to use it | Example |
|---|---|---|
| Leave blank and flag | The value is unknown and guessing would mislead | Missing phone number |
| Fill from another source | The value exists elsewhere | Look up region from ZIP code |
| Fill with a default | A sensible default exists | “Unknown” for a missing lead source |
| Remove the row | The row is unusable without it | Order with no amount or date |
Avoid filling numeric blanks with zero unless zero is actually true. A missing sale amount isn’t a $0 sale.
Placeholder values like “N/A”, “none” or “0000” are disguised blanks. Convert them to real blanks first.
- Excel: Use Find and Replace (Ctrl+H) to replace “N/A” with nothing, matching entire cell contents.
- SQL:
NULLIF()returns null when a value equals a placeholder, andCOALESCE()returns the first non-null value, which is useful for defaults:
sql
SELECT
customer_id,
NULLIF(TRIM(phone), 'N/A') AS phone,
COALESCE(NULLIF(TRIM(lead_source), ''), 'Unknown') AS lead_source
FROM customers_clean;
Key takeaway: Decide the rule before you touch the tool. “A duplicate is the same order ID,” “blank lead source becomes Unknown,” “California is always CA.” Written rules make cleaning consistent, repeatable and easy to explain.
Steps 5 and 6: Fix formatting and standardize categories
Step 5: Fix formatting problems
Formatting issues are the most common and the easiest to miss.
Extra spaces and hidden characters. Excel’s TRIM removes all spaces except single spaces between words. But Microsoft notes that TRIM by itself doesn’t remove the nonbreaking space character (value 160), which often arrives in data copied from web pages. Combine functions:
=TRIM(CLEAN(SUBSTITUTE(A2,CHAR(160)," ")))
CLEAN removes nonprinting characters, and SUBSTITUTE swaps nonbreaking spaces for regular ones before TRIM runs.
In SQL, TRIM(column) removes leading and trailing spaces. Use REPLACE() for specific unwanted characters.
Text case. Use PROPER, UPPER or LOWER in Excel, and UPPER() or LOWER() in SQL (PostgreSQL also offers INITCAP() for title case). Store emails in lowercase so matching works.
Numbers stored as text. These won’t sum and sort strangely. In Excel, use the error-indicator option to convert to number, VALUE(), or Data > Text to Columns and click Finish. In SQL, convert with CAST(amount AS NUMERIC) after removing commas and currency symbols.
Dates. Dates stored as text, or mixed formats like 03/04/2026 and 2026-04-03, cause wrong sorting and wrong month totals. In Excel, use DATEVALUE() or Text to Columns with a date format. In SQL, convert with a date function and a format string appropriate to your database, and store dates in a proper date type, not text.
Splitting and combining columns. Use Excel’s Convert Text to Columns wizard to split “City, State” into 2 columns, and & or CONCAT to combine them. Flash Fill (Data > Flash Fill) can also learn a pattern from a couple of typed examples.
Step 6: Standardize categories
Free-text categories drift: “CA”, “Calif.” and “California”, or “Facebook”, “FB” and “facebook ads.” The fix is a mapping table with every variant and its standard value.
| Raw value | Standard value |
|---|---|
| CA | California |
| Calif. | California |
| california | California |
| FB | |
| facebook ads |
- Excel: Put the mapping table on its own sheet and use
XLOOKUP(orVLOOKUP) to return the standard value. - SQL: Join to a mapping table, or use
CASEfor a short list:
sql
SELECT
order_id,
CASE
WHEN UPPER(TRIM(state)) IN ('CA', 'CALIF.', 'CALIFORNIA') THEN 'California'
ELSE TRIM(state)
END AS state_clean
FROM orders_clean;
Keep the mapping table and reuse it every time new data arrives. This is a small but real piece of data governance.
Steps 7 and 8: Validate, document and automate
Step 7: Validate and check outliers
Now check that values make sense:
- Ranges: No negative quantities, no birthdates in the future, no order dates before you opened.
- Formats: Emails contain “@”, ZIP codes have 5 digits, phone numbers have the right length.
- Totals: Compare the cleaned total against the source system’s report. If revenue changed, you should know exactly why.
- Outliers: Sort amounts high to low and check the top and bottom rows. A $250,000 order at a coffee shop is probably a typo, but confirm before deleting. Some outliers are real.
sql
SELECT * FROM orders_clean
WHERE quantity <= 0
OR order_date > CURRENT_DATE
OR email NOT LIKE '%@%';
Step 8: Document and automate
Finish by updating your change log: what you changed, how many rows were affected and why. Then make cleaning repeatable, because the same problems will be back next month.
- Excel: Power Query (Data > Get & Transform) records cleaning steps and reapplies them when you refresh with new data.
- SQL: Save your cleaning queries as views or scheduled models. If you load data into a warehouse, cleaning usually happens in the transform step. Our guide to ETL vs ELT explains where that fits.
- Prevention: Fix problems at the source with dropdown menus instead of free text, required fields and input validation on forms.
AI tools can speed some of this up, such as suggesting formulas or spotting inconsistent categories. Always check results against your rules.
Where to start
If you have a messy file in front of you right now:
- Save a copy of the original.
- Profile it: count rows, blanks and distinct values in key columns.
- Remove exact duplicates on the column that defines a unique record.
- Convert placeholders to blanks, then decide how to handle each column’s blanks.
- Run TRIM, CLEAN and SUBSTITUTE (or SQL TRIM and REPLACE) on text columns.
- Convert text numbers and dates to proper types.
- Map categories to standard values.
- Compare totals with the source and write down what changed.
If you want to build these skills into a career, see how to become a data analyst, and for putting clean data to work, data analytics for small business.
Conclusion
Data cleaning follows the same pattern every time: back up, profile, remove duplicates, handle missing values, fix formatting, standardize categories, validate and document. Excel handles most small and midsize datasets with Remove Duplicates, TRIM, CLEAN, SUBSTITUTE and lookups. SQL handles larger data with ROW_NUMBER, TRIM, NULLIF, COALESCE and mapping tables. Write your rules down, automate them where you can and fix problems at the source so there’s less to clean next time.
Sources
- Microsoft Support: Find and remove duplicates — highlighting and removing duplicates in Excel, and the advice to back up first
- Microsoft Support: TRIM function — what TRIM removes and its limit with nonbreaking spaces
- Microsoft Support: Top ten ways to clean your data — CLEAN, SUBSTITUTE, case functions, text-to-number and date fixes, Text to Columns
- PostgreSQL documentation: Conditional expressions — COALESCE and NULLIF behavior
- PostgreSQL documentation: Window functions — ROW_NUMBER numbering rows within a partition
Frequently asked questions
What are the steps to clean data?
The main steps are: back up the original data, profile it to find problems, remove duplicates, handle missing values, fix formatting such as spaces, case, numbers and dates, standardize categories, validate values and outliers, and document every change. Following the same order each time makes cleaning faster, consistent and easy to repeat on new data.
How do I clean data in Excel?
In Excel, start with a copy of your data. Use Data > Remove Duplicates for duplicate rows, TRIM, CLEAN and SUBSTITUTE for extra spaces and hidden characters, PROPER or UPPER for text case, VALUE and DATEVALUE for numbers and dates stored as text, and XLOOKUP with a mapping table to standardize categories. Power Query can record the steps for reuse.
How do you remove duplicates in SQL?
The most reliable way is the ROW_NUMBER window function. Partition by the columns that define a duplicate, such as order ID, order by a column like updated date, and keep only rows where the row number equals 1. SELECT DISTINCT works for exact duplicate rows, but ROW_NUMBER lets you choose which version of a duplicate to keep.
How should I handle missing values?
Handle missing values based on what the blank means. Leave it blank and flag it when the value is truly unknown, fill it from another source when the information exists elsewhere, use a default such as "Unknown" for categories, or remove the row if it's unusable. Avoid filling numeric blanks with zero unless zero is actually correct.
Why doesn't TRIM remove all spaces in Excel?
Excel's TRIM function removes standard spaces but not the nonbreaking space character, value 160, which often comes from web pages and some exports. Microsoft notes TRIM alone doesn't remove it. Use =TRIM(CLEAN(SUBSTITUTE(A2,CHAR(160)," "))) to replace nonbreaking spaces with regular ones, strip nonprinting characters and then trim.
What tools are used for data cleaning?
Common data cleaning tools include Excel and Google Sheets for small to midsize files, Power Query for repeatable spreadsheet cleaning, SQL for data in databases and warehouses, and Python libraries such as pandas for larger or more complex jobs. Data warehouses and pipeline tools can run cleaning rules automatically each time new data loads.
How long does data cleaning take?
It depends on the size and condition of the data, but cleaning often takes longer than the analysis itself, especially the first time. A small spreadsheet might take an hour; combining several messy systems can take days. Documenting rules and automating them with Power Query or saved SQL queries makes later cleanups much faster.
Was this guide useful
No reviews yet. Share your experience and help other readers.