SQL Formatter and Beautifier – Free

Paste a query and this SQL formatter rewrites it with uppercase keywords, one clause per line and consistent indentation, using the grammar of the dialect you choose.

How to use the SQL formatter tool

  1. Choose the database under Dialect: Standard SQL, PostgreSQL, MySQL, SQLite or SQL Server.
  2. Paste one or more statements into the SQL box.
  3. Select Format SQL. Select Example to format a short sample query.
  4. Review the formatted query in the Result box and select Copy to paste it into your editor or migration file.

What the SQL formatter does to a query

Formatting is done by the open-source sql-formatter library, running in the page. It tokenizes the text according to the chosen dialect and lays the tokens out again. Clause keywords such as SELECT, FROM, WHERE and GROUP BY start their own lines. Select-list items and conditions go beneath them, one per line with two-space indentation, and AND and OR start new lines.

Words the dialect treats as reserved keywords are converted to uppercase. Identifiers, string literals, function names and data type names keep the case you typed, so count stays count and varchar stays varchar. Comments are preserved, and several statements separated by semicolons are split apart with a blank line between them.

Worked example: a join with grouping

The one-line query below was formatted with the PostgreSQL dialect. The join stays indented under FROM with its ON condition on the same line, and the IN list gains a space after the comma.

select u.id, u.email, count(o.id) as orders from users u left join orders o on o.user_id = u.id where u.status in ('active','trial') group by u.id, u.email having count(o.id) > 2 order by orders desc limit 10;

SELECT
  u.id,
  u.email,
  count(o.id) AS orders
FROM
  users u
  LEFT JOIN orders o ON o.user_id = u.id
WHERE
  u.status IN ('active', 'trial')
GROUP BY
  u.id,
  u.email
HAVING
  count(o.id) > 2
ORDER BY
  orders DESC
LIMIT
  10;

Why the dialect matters

Each dialect has its own operators, quoting rules and keyword list. Pick the wrong one and the tokenizer may stop at syntax it does not know. Oracle, BigQuery, Snowflake and other dialects are not offered; Standard SQL handles plain ANSI queries for those, but vendor-specific syntax may fail. The cases below were checked against this tool.

  • PostgreSQL: the :: cast, JSON operators such as ->> and numbered parameters like $1 are understood. Under Standard SQL the :: cast is a parse error.
  • SQL Server: square-bracket identifiers such as [dbo].[users], TOP and @variables are understood. Under Standard SQL the brackets are a parse error.
  • MySQL: backtick-quoted identifiers and the two-argument LIMIT 5, 10 form are kept intact.
  • Keyword lists differ too. SQLite and SQL Server leave true and false as typed, and MySQL leaves is null as typed, where Standard SQL and PostgreSQL uppercase both.

Formatting is not validation

The formatter does not check a query against the full SQL grammar, connect to a database or run anything. Misspelled keywords and unknown table names pass straight through as ordinary words, so a clean result does not mean the query is correct. Errors appear in two cases: the tokenizer meets characters it cannot classify, such as an unterminated string literal, or parentheses do not balance.

A tokenizer error quotes the first few characters it could not read, with their line and column. If it suggests a more specific dialect, try that first. An unbalanced parenthesis produces a very long message from the library's grammar; only its first line, which gives the line and column, is useful.

Frequently asked questions

Does formatting change what my query does?

No. Only whitespace and the letter case of reserved keywords change. SQL keywords are case-insensitive, and string literals, quoted identifiers and comments are left untouched, so the database receives an equivalent statement.

Can I choose lowercase keywords or a different indent?

Not in this tool. Keyword case is fixed to uppercase and indentation to two spaces. The only setting is the dialect.

Is my SQL sent to a server?

No. The formatting library is loaded into the page and runs in your browser. Queries that contain table names, customer IDs or other internal details stay on your machine.

Why are function names and data types not uppercased?

The tool requests uppercase for reserved keywords only. Function names such as count or coalesce and type names such as int or varchar belong to separate token classes, and their case is preserved as typed.

Can I format several statements at once?

Yes. Paste a whole script and each statement ending in a semicolon is formatted in turn, with a blank line between statements. A line comment placed between two statements stays above the statement that follows it.

Related tools