Skip to main content

SQL Formatter

Free SQL formatter: beautify SQL queries for MySQL, PostgreSQL and more with readable indentation. Runs in your browser.

Raw SQL

Stays in your browser

Formatted SQL

Formatted SQL will appear here.

Shortcuts: Ctrl+Enter run · Ctrl+Shift+C copy result · / search (use ⌘ on Mac)

Next steps

Done here? Use the Send to… menu on the result to carry it on to Text Compare or Diff Checker.

Free Online SQL Formatter & Prettifier

Paste a raw SQL query to instantly format it with clean indentation and consistent keyword casing, across ANSI SQL, MySQL, or PostgreSQL syntax. The SQL formatter lays a query out so each clause is easy to read. It is useful for database developers, analysts, and reviewers who need a readable SQL query before troubleshooting joins or sharing a migration.

Why format SQL?

SQL written on a single line, or with inconsistent indentation and keyword casing, is hard to review and easy to introduce bugs into — a misplaced condition in a dense WHERE clause is a common source of subtle query bugs. Consistent formatting makes clauses, joins, and conditions visually distinct, which matters both for your own review and for code review by teammates.

How to use this formatter

Paste your raw SQL, choose the dialect that matches your database (ANSI SQL for a generic/standard query, or MySQL/PostgreSQL if you're using dialect-specific syntax), and click Format. Keywords are uppercased and each clause (SELECT, FROM, WHERE, ORDER BY, and so on) starts on its own line with consistent indentation.

Why the dialect setting matters

SQL dialects differ in identifier quoting (MySQL's backticks vs. PostgreSQL's double quotes), available functions, and syntax extensions (like PostgreSQL's :: cast operator or MySQL's # comments). Some of these genuinely require the correct dialect to parse without error — but not every mismatch does; a few dialect-specific clauses parse under the wrong dialect without erroring and just come out mis-indented instead, so picking the dialect that actually matches your database is still worth doing even when nothing obviously breaks.

Worked example

SELECT id, name FROM users WHERE active = 1 ORDER BY name; formats to one clause per line with uppercased keywords, regardless of dialect. PostgreSQL's SELECT '5'::int; only parses under the PostgreSQL dialect — both ANSI SQL and MySQL reject it with a parse error, since :: casting is PostgreSQL-specific syntax.

Common mistakes and edge cases

  • A query that's missing real content, like SELECT FROM WHERE;, doesn't error — in practice, it formats cleanly since every keyword is valid SQL syntax on its own. A successful format isn't proof the query is complete or correct.
  • MySQL's ON DUPLICATE KEY UPDATE clause parses under the ANSI SQL dialect without an error, but comes out mis-indented (confirmed by testing) rather than formatted as one clause — choose MySQL as the dialect for MySQL-specific syntax rather than relying on an error to catch a wrong choice.

Frequently asked questions

Is my SQL query uploaded anywhere?
No — formatting happens entirely in your browser. Nothing you paste, including table or column names from a real schema, is ever sent to a server.
Why does the dialect (ANSI, MySQL, PostgreSQL, SQL Server and others) matter?
Some syntax genuinely requires the right dialect to parse at all — as tested, MySQL's # line comments and PostgreSQL's :: cast operator each throw a real parse error under the wrong dialect. But the reverse isn't always true: ANSI SQL will silently accept MySQL backtick identifiers without complaint, and dialect-specific multi-word clauses like MySQL's ON DUPLICATE KEY UPDATE don't error under the wrong dialect either — they just get mis-indented instead of rejected. When in doubt, pick the dialect that actually matches your database rather than relying on an error to tell you it's wrong.
Does formatting change what my query does?
No — formatting only changes whitespace, indentation, and keyword casing for readability. The query logic, table names, and values are never altered.
What happens if my SQL has a syntax error?
It depends on the kind of error — confirmed by testing. A structurally broken query (unbalanced parentheses, an unexpected token) does report a parse error with its rough position. But this formatter is a tokenizer/prettifier, not a full semantic validator: a query missing entire clause content, like "SELECT FROM WHERE;", parses and reformats without any error at all, since every keyword it sees is still valid SQL syntax in isolation. Don't treat a clean format as proof your query is logically complete.
Can formatting SQL fix a broken query?
No. Formatting only changes spacing and line breaks, so a query with a missing comma or a wrong table name is still wrong afterwards. The formatted layout does help, because each clause starts on its own line and a misplaced bracket or comma is much easier to spot. Pick the right dialect (MySQL, PostgreSQL, SQL Server and others) so dialect-specific syntax is understood.

Related tools

More Formatters & Parsers

View all Formatters & Parsers tools →