How to compare two SQL queries and find the difference
SQL Compare — open the free tool →A report suddenly shows a different total, a migration script was edited after review, or two "almost identical" queries return different row counts. In each case you need to see exactly what changed in the SQL. Reading two long queries side by side is slow, and a plain text diff is useless when one version is a single line copied from a log and the other is nicely formatted.
This guide covers two different questions: how to compare the text of two queries, and how to compare the results they return.
Compare two SQL queries online
- Open SQL Compare.
- Paste the original query or script on the left and the changed one on the right.
- Keep Format SQL ticked (the default). Both sides are laid out the same way before comparing.
- Click Compare now. Changed lines are highlighted, and the exact words that differ are marked inside each line.
- Tick Only changes for long scripts, then copy or download the report for a pull request or ticket.
Why formatting first matters
Queries from ORMs, application logs and BI tools are usually one long line. Compare that with a formatted version and a normal diff marks everything as changed. Format SQL rewrites both sides into the same layout, for example:
select o.id, c.name, sum(o.total) as revenue from orders o join customers c on c.id = o.customer_id where o.status = 'paid' and o.created_at >= '2026-01-01' group by o.id, c.name order by revenue desc
becomes
SELECT o.id,
c.name,
sum(o.total) AS revenue
FROM orders o
JOIN customers c
ON c.id = o.customer_id
WHERE o.status = 'paid'
AND o.created_at >= '2026-01-01'
GROUP BY o.id,
c.name
ORDER BY revenue DESC
With one column and one condition per line, a changed filter such as o.status = 'paid' versus o.status IN ('paid', 'refunded') shows up as one highlighted line instead of a wall of red. Only whitespace and keyword case are changed; strings, quoted identifiers, comments and parameters stay exactly as written. Untick Format SQL when you need to compare the scripts character for character.
What to look for when two queries return different results
- JOIN type: a
LEFT JOINchanged toJOINdrops rows that have no match. - Conditions: an extra
AND, a changed date boundary (>versus>=) orORwithout brackets. - NULL handling:
col <> 'x'does not return rows wherecolis NULL. - GROUP BY and DISTINCT: adding a column to GROUP BY splits totals into more rows.
- Filters moved between ON and WHERE: on an outer join, a condition in
WHEREcan remove the unmatched rows the join kept.
Compare the results of two queries
Sometimes the text is fine and you need to know which rows differ. Set operators do this inside the database:
-- rows returned by query A but not by query B
SELECT id, name, total FROM report_v1
EXCEPT
SELECT id, name, total FROM report_v2;
-- then swap them for rows only in B
EXCEPTworks in PostgreSQL, SQL Server, SQLite and MySQL 8.0.31+. Oracle usesMINUS(Oracle 21c also acceptsEXCEPT).- Both queries must return the same number of columns with compatible types.
EXCEPTremoves duplicate rows. If duplicates matter, compareCOUNT(*)per key as well, or useEXCEPT ALLin PostgreSQL.
If the results come from two different databases, export each result to CSV and use CSV Compare with Match rows by key (for example the id column). Rows are paired by key, so row order does not matter, and changed cells are highlighted.
Other ways to compare SQL
- VS Code: right-click the first .sql file, choose Select for Compare, then right-click the second and choose Compare with Selected. Format both files the same way first, or one-line queries will not line up.
- Command line:
git diff --no-index --word-diff old.sql new.sqlshows changed words inside each line. - Schemas: to compare two database schemas, export the
CREATE TABLEstatements from both and paste them into SQL Compare. - For application code around the query, use Code Compare; for API responses, use JSON Compare.
Frequently asked questions
How do I compare two SQL queries online?
Open SQL Compare, paste the original query on the left and the changed one on the right, keep Format SQL ticked and click Compare now. Both queries are laid out the same way first, so changed columns, joins and conditions are highlighted instead of the whole line.
Can I compare a one-line query with a formatted query?
Yes. Format SQL puts every clause, selected column and AND/OR condition on its own line on both sides, so a minified query from a log and a hand-formatted one from your editor line up.
How do I compare the results of two SQL queries?
Use a set operator: (query1 EXCEPT query2) shows rows only the first query returns, and swapping them shows rows only the second returns. Oracle uses MINUS. Or export both results to CSV and compare them with CSV Compare using Match rows by key.
Does Format SQL change my query?
Only its layout: line breaks, spaces and the case of SQL keywords. Strings, quoted names, comments and parameters are kept exactly as written.
Are my queries uploaded?
No. SQL Compare runs in your browser, so table names, schema details and data in your queries stay on your machine.