Guide · How-to

How to compare two SQL queries

Format both queries the same way first, then compare them to see which clauses, columns or conditions changed.

The short version

  1. Paste both queriesOriginal on the left, edited query on the right.
  2. Format both sidesUse Format so that keywords, columns and clauses each start on a predictable line.
  3. Ignore whitespace if neededSwitch on Ignore whitespace to hide differences in spacing and line breaks.
  4. Review clause by clauseCheck SELECT, JOIN, WHERE, GROUP BY and ORDER BY separately; a small change in one can change the result set.

Someone rewrote a slow query, a migration changed a view, or a colleague "just tidied" a report. You need to know what is actually different. SQL is awkward to compare by eye because the same query can be written on one line or fifty.

Step 1: put both queries in the same shape

Paste both queries into the editor and use Format on each side. The formatter writes keywords in upper case and puts clauses on their own lines, so two queries that differ only in layout become identical text, and real changes stand out.

Step 2: compare, and ignore whitespace if you need to

Once both sides are formatted, the diff points at the lines that really differ. If your queries come from different sources and still disagree on spacing, switch on Ignore whitespace.

Step 3: read it clause by clause

Different kinds of change deserve different attention:

  • SELECT list. Added or removed columns change the shape of the result and can break code that reads it.
  • JOIN. A different join type (for example INNER to LEFT) changes which rows appear.
  • WHERE and HAVING. A changed operator or value changes which rows are filtered. Check AND and OR grouping carefully.
  • GROUP BY and ORDER BY. These change aggregation and row order.
  • LIMIT. A changed limit is easy to miss and has an obvious effect on results.

What a text comparison cannot tell you

A diff compares text, not behaviour. Two queries can look different and return the same rows, or look nearly identical and return different data. When it matters, run both against the same data and compare the results too.

Questions

Does the comparison tell me if two queries return the same rows?

No. It compares text. Two queries can look different and return identical results, or look almost the same and return different data. Run both against the same data if you need to be sure.

Does Format change what my query does?

Formatting only changes spacing, line breaks and keyword case. It is meant to leave the meaning untouched, but always read the formatted query before running it.

Was this helpful?