Remove duplicate rows from a CSV file
By Tanin Na Nakorn · Updated
Duplicates sneak in when exports overlap, when a sync runs twice, or when the same person signs up with
[email protected] and [email protected]. Excel's Remove Duplicates works until the file passes a
million rows, and it only removes exact matches. SQL handles both cases, on files of any size.
The example uses contacts.csv, an export of 3.4 million contacts with email,
name, company, and updated_at columns.
1. Import the file
Drop contacts.csv onto Superintendent. It becomes the table contacts, with the column types
detected.
2. Find out how many duplicates there are
Before removing anything, see what you're dealing with:
select
count(*) as rows,
count(distinct email) as distinct_emails,
count(distinct lower(trim(email))) as distinct_emails_cleaned
from contacts
| rows | distinct_emails | distinct_emails_cleaned | |
|---|---|---|---|
| 1 | 3402118 | 3287540 | 3251906 |
About 115,000 rows repeat an email exactly, and another 36,000 differ only in capitalization or spaces.
3. Remove exact duplicates
When two rows are identical in every column, distinct keeps one of each:
select distinct *
from contacts
4. Keep one row per key
More often, duplicates share a key (here, the email) but differ elsewhere: an older company name, a missing name.
Number the rows in each group and keep the first. The order by decides which one wins; here it's the
most recently updated:
select *
from contacts
qualify row_number() over (
partition by lower(trim(email))
order by updated_at desc
) = 1
qualify filters on a window function, like where does on a regular column. It saves you a
subquery.
5. Look at what you removed
Don't export blind. List the duplicate groups with all their rows, so you can check the right ones are being kept:
select
lower(trim(email)) as email_key,
count(*) over (partition by lower(trim(email))) as copies,
*
from contacts
qualify count(*) over (partition by lower(trim(email))) > 1
order by email_key, updated_at desc
Scroll through the result in the grid, sort by copies to see the worst offenders first, and filter to a
specific email when something looks odd.
6. Export the clean file
Open the contacts_deduplicated result and click Export CSV. The new file has the header
row and one row per contact. The original contacts.csv is untouched.
Got two files that overlap instead of one file with repeats? Stack them with
union all first, then deduplicate the result. Or
compare them to see exactly where they differ.
Questions
Does removing duplicates change my original CSV file?
No. Superintendent imports a copy into its workspace. The deduplicated result is a new table, which you export to a new file.
How do I treat emails that differ only in capital letters or spaces as the same?
Compare a cleaned-up version of the value: lower(trim(email)). The queries on this page show how.
Which row is kept when duplicates differ?
You decide, with the order by inside row_number(). Order by updated_at desc to keep the newest, or by created_at to keep the oldest.
Can Excel remove duplicates from a file with millions of rows?
Only up to its 1,048,576-row limit. Superintendent works on CSV files up to 40GB.