A question that keeps coming back is "do these two sets of data agree?": the sales in your system against the file from a shop, the customer list of two systems, or a table you moved to a new database and want to confirm is complete. There are several ways, depending on where the data lives and how often you have to do it.
Before you compare: decide three things
- The key: the column that says which row is the same row on both sides, such as an order number or a product code. If the key repeats, the result is unreliable.
- The columns to compare: not every column has to be checked. Skip the ones that always change by themselves, such as a last-modified time.
- The differences you accept: decimals that differ slightly because of rounding, text that differs only in case or spaces.
Way 1: a full outer join in SQL (data in one database)
If both tables are in the same database, a full outer join on the key plus a CASE gives all three groups.
SELECT COALESCE(a.sku, b.sku) AS sku,
CASE WHEN a.sku IS NULL THEN 'only_b'
WHEN b.sku IS NULL THEN 'only_a'
WHEN a.price IS DISTINCT FROM b.price THEN 'different'
ELSE 'same' END AS status,
a.price AS price_a, b.price AS price_b
FROM products a
FULL OUTER JOIN products_new b ON b.sku = a.sku;Watch out: use IS DISTINCT FROM rather than <> so NULL does not hide a difference, and you have to write a condition for every column.
Way 2: across databases or against a file
When the two sides are in different databases, or one is an Excel file, plain SQL cannot do it. People usually export to CSV and compare with VLOOKUP, which is slow and easy to get wrong with many rows. The other way is to load both sides into one database and run the SQL above.
Way 3: Compare in Ruamhub
Choose side A and side B as a table of any connection or a CSV / Excel file, choose a key and press Compare. Columns with the same name are paired for you. Set how much a number may differ, and choose to ignore case or spaces. You get the rows that match, differ or exist on one side only, charts, and the list of rows that differ with the cells that do not agree. Export it as CSV to hand over to whoever fixes it.
Make it routine
If you have to compare every day, save the comparison and put it on a schedule, for example every day at 07:00, with an email alert when the match falls below your threshold. The history keeps the row counts and percentage of each run, so you can see whether the differences shrink or grow, without keeping your data.
Mistakes that come up often
- Keys of different types, such as "001" against 1, because Excel dropped the leading zeros.
- Invisible trailing spaces that stop keys from matching.
- Rounding noise in decimals: set an accepted difference instead of demanding an exact match.
- A file opened and saved again in Excel may change its date and number formats without telling you.