Compare two CSV files
By Tanin Na Nakorn · Updated
Text diff tools compare files line by line. That falls apart when the rows are in a different order, or when one
changed value is buried in a 2GB file. Spreadsheet formulas like VLOOKUP stop at a million rows. The
reliable way is to treat both files as tables and let SQL find the differences.
The example below compares two exports of a customer list: customers_jan.csv and
customers_feb.csv. Both have an id column that identifies each customer.
1. Import both files
Drop both CSV files onto Superintendent. Each one becomes a table named after its file:
customers_jan and customers_feb. Column names are cleaned up the same way for both, which
already removes one common source of false differences.
2. Rows in one file but not the other
EXCEPT returns the rows of the first query that don't appear in the second. Run it both ways:
-- Rows that are in January but not in February,
-- either removed or changed
select id, name, email, plan from customers_jan
except
select id, name, email, plan from customers_feb
-- Rows that are in February but not in January,
-- either added or changed
select id, name, email, plan from customers_feb
except
select id, name, email, plan from customers_jan
If both queries come back empty, the files hold the same rows (for those columns). Leave out columns that change on
every export, like updated_at, so they don't show every row as different.
3. Added, removed, and changed rows by key
To see what kind of difference each one is, join the files on the key with a FULL OUTER JOIN:
select
coalesce(j.id, f.id) as id,
case
when j.id is null then 'added'
when f.id is null then 'removed'
else 'changed'
end as change,
j.plan as plan_before,
f.plan as plan_after,
j.email as email_before,
f.email as email_after
from customers_jan j
full outer join customers_feb f on f.id = j.id
where j.id is null
or f.id is null
or j.plan is distinct from f.plan
or j.email is distinct from f.email
order by change, id
| id | change | plan_before | plan_after | email_before | email_after | |
|---|---|---|---|---|---|---|
| 1 | 10442 | added | NULL | pro | NULL | [email protected] |
| 2 | 10017 | changed | starter | pro | [email protected] | [email protected] |
| 3 | 10230 | changed | pro | pro | [email protected] | [email protected] |
| 4 | 10301 | removed | team | NULL | [email protected] | NULL |
is distinct from treats two empty values as equal, so blank cells don't show up as changes.
4. Look through the differences
The result opens in a grid. Filter the change column to changed, sort by a column, and
scroll through every row. Export the result to CSV with Export CSV to share it.
Save the workspace and you can rerun the same comparison on next month's export by replacing the two tables. To check a migration between two databases instead of two files, see Validate a Postgres to ClickHouse migration.
Questions
Does Superintendent have a diff button?
No. You compare files with SQL, as shown on this page. It takes a few lines, and you decide what counts as the same row and which columns matter.
What if the two files have different column names?
Rename them in the query, for example select "Customer_ID" as customer_id. Or compare only the columns both files share.
What if the columns are in a different order?
List the columns by name in each select instead of using select *. EXCEPT matches columns by position.
Can I compare a CSV with a database table?
Yes. Connect to the database, download the table or a query result into the workspace, and compare it with the CSV using the same queries.
How large can the files be?
Each file can be up to 40GB. The comparison runs on your computer.