SQL 格式化工具

使用多种风格和规范格式化并美化 SQL 查询

原始 SQL

行数: 1 | 字符数: 0

格式化后的 SQL

行数: 0 | 字符数: 0

格式化风格

高级设置

SQL 示例

历史记录

About the SQL Formatter & Beautifier

Anyone who has scrolled through a 400-character SQL query jammed onto a single line knows the pain this tool solves. A stored procedure export, a query copied from a Slack thread, or a minified statement pulled from an application log often arrives with no line breaks, inconsistent capitalization, and clauses packed together in a way that makes it nearly impossible to spot a missing JOIN condition or a misplaced parenthesis. The SQL Formatter takes that raw text and rebuilds it into a properly indented, readable query so you can actually see its structure — SELECT list, FROM, WHERE, GROUP BY, and nested subqueries each landing on their own visually distinct block.

Paste or type a query and the output updates as you type: in the Standard style SELECT, FROM, WHERE, GROUP BY, ORDER BY and HAVING each start a new line, JOINs break onto their own lines, each column in the SELECT list gets its own line, and every parenthesis indents its contents one level so subqueries stand out. Expanded also breaks after every comma, while Compact only wraps at the maximum line length. You choose keyword case (UPPERCASE, lowercase or Title Case), spaces or tabs, the indent width, and whether comments are stripped. It is a lightweight tokenizer rather than a full SQL parser, so it formats standard SELECT, INSERT, UPDATE and CTE queries well but does not know dialect-specific syntax; the Oracle, MySQL, PostgreSQL and SQL Server presets currently produce the same layout as Compact.

Formatting happens in your browser, so the query text is not sent anywhere — which matters when the SQL contains internal table names or business logic. Your settings, and the last 20 queries you copied, are kept in local storage; Clear History removes them. A common workflow is formatting a query before a code review or before adding it to a migration file, since consistent indentation makes diffs in version control far easier to read than one squashed onto a single line.

How to format a SQL query

Turn a dense or minified SQL query into a cleanly indented, readable statement.

  1. 1

    Paste your SQL

    Copy the query from your database client, log file, or codebase and paste it into the input box.

  2. 2

    Pick a formatting style

    Choose Standard for one clause per line, Expanded to also break after every comma, or Compact to keep clauses together and wrap only at the maximum line length.

  3. 3

    Adjust the settings

    Under Advanced Settings, set keyword case, spaces or tabs, indent width and maximum line length. The formatted output updates automatically about half a second after each change.

  4. 4

    Review the structure

    Read through the output before using it: check that subqueries are indented as expected and that nothing dialect-specific (backslash escapes, dollar-quoted bodies) was rearranged.

  5. 5

    Copy the result

    Copy the cleaned-up query and paste it back into your SQL file, migration script, or code review.

Frequently Asked Questions

What SQL dialects does the formatter support?

It handles the standard SQL that all major databases share: SELECT, INSERT, UPDATE, DELETE, JOINs, subqueries and WITH (CTE) queries. The Oracle, MySQL, PostgreSQL and SQL Server style buttons do not add dialect-specific rules; they currently produce the same layout as Compact. Dialect features such as PostgreSQL dollar-quoted function bodies are not understood.

Does the formatter change the logic of my query?

It is meant to change only whitespace, line breaks and keyword case. Operators such as >=, <> and ::, qualified names like u.id, and strings with a doubled quote ('O''Brien') are kept intact. It is still a simple tokenizer rather than a parser, so check the result before running it — and note that Remove Comments deletes anything after -- on a line, even inside a string.

Can I minify SQL as well as format it?

Not fully. The Compact style keeps clauses on the same line and wraps only at the Max Line Length, but it still starts a new line after each comma in the SELECT list. For a true one-line query, use Compact, turn off Align Columns and raise Max Line Length (the maximum is 200 characters); queries longer than that will still wrap.