Skip to content

SQL diff checker

Compare two SQL queries, migration files, or stored procedure definitions online. Paste the original on the left and the modified version on the right to see every added and removed line highlighted instantly, all in your browser with nothing sent to a server.

Diff
RemovedAdded

How to compare two SQL queries

  1. Paste the original query, file, or definition into the Original box.
  2. Paste the version you want to check into the Modified box. The diff appears right away and updates with every keystroke.
  3. Read the highlights. Red lines with a minus sign exist only in the original, green lines with a plus sign exist only in the modified version, and plain lines are identical on both sides.
  4. If everything looks changed, the two versions are probably formatted differently. Run both through the SQL formatter and compare again.
  5. Use the line numbers on each side to point a teammate to the exact change.

Example: compare two versions of a query

Say a revenue report originally counted every paid order. The new version limits the date range and returns only the top 20 customers. Pasting both versions produces this diff:

The new date filter and the LIMIT are easy to see. Note that ORDER BY revenue DESC; is also marked as removed. The semicolon moved to the new last line, and a line diff treats that as a changed line. That is expected behavior, and it is why the diff stays reliable on long queries: nothing is hidden or summarized.

Example: compare two migration files

Migration reviews are where a diff saves the most time. Here the original CREATE TABLE is compared with a revised version that adds a foreign key, widens a numeric column, and adds a paid_at column:

The diff surfaces the new foreign key, the wider numeric type, and the new column. The status line appears as removed and added even though only a trailing comma changed, because adding a column forces a comma onto the line above it. Knowing that pattern makes migration diffs quick to read. For whole-database comparisons across many tables, use the database schema diff, which reports added, removed, and changed tables and columns instead of raw lines.

Example: two queries that only differ in formatting

A line diff compares lines, so a query written on one line and the same query written across several lines look completely different. Compare these two:

-- Original
select id,name from users where active=true

-- Modified
SELECT
  id,
  name
FROM users
WHERE active = TRUE

Every line is flagged, although the two queries are the same. Format both with the same settings in the SQL formatter first, and the diff shrinks to the changes that matter. This is also the best way to verify that an auto-formatter or refactor did not change query logic.

Compare what two queries return

A text diff shows how two queries differ, not whether their results differ. When you need to know if a rewrite still returns the same rows, run both against your database and subtract one result from the other in each direction:

(SELECT id, email FROM users_v1)
EXCEPT
(SELECT id, email FROM users_v2);

Then swap the two queries and run it again. If both runs return zero rows, the results match. EXCEPT is available in PostgreSQL, SQL Server, SQLite, and MySQL 8.0.31 or later, BigQuery requires EXCEPT DISTINCT, and Oracle calls it MINUS.

Compare stored procedures, functions, and views

To compare database objects such as staging against production, extract each definition as text and paste both into the tool:

  • PostgreSQL: pg_get_functiondef('my_function'::regproc) and pg_get_viewdef('my_view'::regclass)
  • MySQL: SHOW CREATE PROCEDURE my_procedure and SHOW CREATE VIEW my_view
  • SQL Server: OBJECT_DEFINITION(OBJECT_ID('dbo.my_procedure')) or sp_helptext

When to use a SQL diff tool

  • Reviewing query changes in pull requests before merging
  • Auditing database migration files by comparing the old and new versions of a CREATE TABLE, view, or stored procedure
  • Debugging regressions by pasting the last working version of a query next to the current broken one
  • Documenting how a query or schema evolved over time
  • Verifying that a formatter or refactor did not change query logic

How the diff is computed

The tool uses a standard line-by-line diffing algorithm, the same approach as git diff. Lines present only in the original are marked as removed, lines present only in the modified version are marked as added, and matching lines appear unchanged with line numbers for both sides. Whitespace counts, so trailing spaces and tabs are differences too.

Stop writing SQL manually for dashboards

If you find yourself iterating on SQL queries to build reports and charts, Basedash can eliminate that loop entirely. Describe the metric or visualization you need in plain English, and Basedash generates the SQL, runs it against your database, and renders the result, with no manual query writing or diffing needed. It connects to PostgreSQL, MySQL, BigQuery, Snowflake, and 750+ other data sources.

FAQ

How do I compare two SQL queries online?

Paste the first query into the Original box and the second into the Modified box. The diff updates as you type, with removed lines in red, added lines in green, and unchanged lines in between. Line numbers for both versions sit in the left margin, so you can point a reviewer to the exact line that changed. Nothing needs to be saved or submitted.

Can I compare two SQL files or migration files?

Yes. Open each file, copy its contents, and paste one into each box. This works for CREATE TABLE and ALTER TABLE migrations, view definitions, seed scripts, and stored procedure bodies. For a full migration, paste the complete before and after scripts so that lines which moved or were reordered are easy to spot in context.

Does it tell me whether two queries return the same results?

No. The tool compares the text of the two queries, not the rows they return. Two differently written queries can return identical data, and one changed character can change the results. To compare outputs, run both queries against your database and use EXCEPT (MINUS in Oracle) in each direction. Any rows that come back exist in one result but not the other.

Can I compare stored procedures, functions, or views?

Yes, as long as you can copy the definition as text. In PostgreSQL, pg_get_functiondef() and pg_get_viewdef() return it. MySQL has SHOW CREATE PROCEDURE and SHOW CREATE VIEW, and SQL Server has OBJECT_DEFINITION() and sp_helptext. Paste the definition from each environment, such as staging and production, to see how they drifted apart.

Is my SQL stored or sent to a server?

No. The entire diff runs in your browser using JavaScript. Your SQL is never transmitted, logged, or stored anywhere, so it is safe to compare queries that reference internal table and column names.

Why do lines that look identical show up as changed?

The diff is exact, so invisible differences count: trailing spaces, tabs versus spaces, different line endings, or a semicolon on one side only. If two versions were formatted differently, run both through the SQL formatter first so that only real changes remain, then diff them again.

Does the diff understand SQL semantics?

No. It is a line-by-line text comparison, so it highlights textual differences rather than logical equivalence. Reordering the columns in a SELECT list shows up as a change even though the query returns the same data, and a renamed alias shows up even if nothing else moved. Format both queries the same way first to keep the noise down.

Which SQL dialects does it work with?

All of them. The comparison is plain text, so PostgreSQL, MySQL, SQL Server, BigQuery, Snowflake, SQLite, and Oracle scripts all diff the same way. Syntax highlighting uses a general SQL grammar. The tool also works on non-SQL text, such as a config file, but keyword colors are tuned for SQL.

Skip the SQL — let AI build your charts

Connect your data and describe what you want to see. Basedash handles the query, visualization, and dashboard.