Format a SQL query

Break a long query onto lines at its main clauses and indent the columns, with keywords in capitals, without changing a single value.

Format a SQL query

0 characters

Three steps — nothing to install

  1. Paste the query

    A SELECT with joins, or an INSERT, UPDATE or DELETE.

  2. Choose the indent

    Two or four spaces, and whether keywords should be uppercased.

  3. Copy the result

    Strings, comments and quoted identifiers are copied exactly as written.

A layout pass, not a parser

The formatter walks the query as tokens, keeps strings, comments and quoted identifiers untouched, and moves whitespace so each major clause starts on its own line. It does not build a syntax tree. That means it works on any dialect — MySQL, PostgreSQL, SQL Server, SQLite — and it also means a deeply nested subquery gets reasonable but not perfect indentation.

It will not rewrite JOIN … ON into a subquery, reorder conditions or add aliases. Those are semantic changes, and a formatter that makes them is no longer safe to run on a query you are about to execute.

What is preserved exactly

String literals keep their quotes and their internal spacing, including doubled quotes and backslash escapes. Line comments and block comments are kept on their own lines. Quoted identifiers, whether double-quoted, backtick-quoted or bracketed, are copied unchanged, so a column named "order" stays quoted.

Keyword case is the only change inside tokens, and only when the option is on. It uppercases recognised SQL keywords and leaves identifiers alone, so a table called user stays lowercase while select becomes SELECT. That distinction matters on PostgreSQL, where an unquoted identifier is folded to lowercase and quoting it changes which column you get.

When the output looks odd

Long IN lists and CASE expressions are the usual complaints: the comma rule splits at top level, so a 200-item list becomes 200 lines. That is readable in a review and unhelpful in a query window. Paste it back unformatted for execution, or shorten the list with a temporary table.

Stored procedures, PL/pgSQL blocks and dynamic SQL built from strings are not understood, because they are not single statements. Format the SELECT inside the string separately. For JSON returned by a query, the JSON formatter is the next step; for a comma-separated result, use the CSV converter.

Frequently asked questions

Can formatting change the query's result?No. Only whitespace between tokens and keyword case change; strings, comments and identifiers are untouched.
Does it work with MySQL and PostgreSQL?Yes. Because it does not parse the dialect, the layout works for any of them.
Are comments kept?Yes, line and block comments are kept on their own lines.
Why is my IN list one item per line?The comma rule splits at top level. That is readable in review and awkward in a query window.
Is the query uploaded?No. The formatting happens on the string in this tab.
Does it handle stored procedures?Not well. It formats single statements; format the statements inside a procedure one at a time.