SQL Formatter & Beautifier

Format, beautify and pretty-print SQL queries online. Uppercase keywords, indent SELECT, FROM, WHERE, and JOIN clauses onto their own lines, and support multiple SQL dialects.

Dialect: Keyword case: Indent:
Copied
Input0 B
Output0 B

What does an SQL formatter do?

An SQL formatter takes a query that arrives as one long line — from an ORM, a log file or a database export — and lays it out so its structure is visible: each major clause (SELECT, FROM, WHERE, GROUP BY, ORDER BY) starts a new line, the items under it are indented, and keywords get a consistent case. The query means exactly the same thing afterwards; only whitespace and keyword case change.

Beautify on this page uses the open-source sql-formatter library (version 15), loaded from cdn.jsdelivr.net and run in your browser. It is a formatter, not a database: it does not connect to anything or execute your SQL.

How to use the SQL formatter

  1. Paste a query into the Input pane, or click Upload File to load a .sql file.
  2. Pick the Dialect your database uses.
  3. Choose the Keyword case and Indent.
  4. Click Beautify for a multi-line layout, or Minify to collapse the query onto one line.
  5. Use Copy, or Download to save the result as formatted.sql.

Options explained

Minify does not use the parser. It replaces every run of whitespace (including line breaks) with a single space and applies the chosen keyword case to a fixed list of common keywords. Quoted strings, quoted identifiers and comments are copied unchanged, and each -- line comment is rewritten as a /* … */ block comment so that it cannot comment out the rest of the one-line query.

Examples

InputOptionsBeautify output
select id,name from users where active=1 order by nameStandard SQL, UPPERSELECT
  id,
  name
FROM
  users
WHERE
  active = 1
ORDER BY
  name
select top 10 name from dbo.usersT-SQL, UPPERSELECT
  TOP 10 name
FROM
  dbo.users
select a::int from tPostgreSQL, UPPERSELECT
  a::int
FROM
  t
SELECT id FROM tStandard SQL, lowerselect
  id
from
  t

A join with aggregation shows how sub-clauses are handled — LEFT JOIN … ON stays under FROM:

SELECT
  u.id,
  count(o.id) AS orders
FROM
  users u
  LEFT JOIN orders o ON o.user_id = u.id
WHERE
  u.created_at >= '2024-01-01'
GROUP BY
  u.id
HAVING
  count(o.id) > 5

Subqueries are indented one level further inside their brackets, and several statements separated by ; are printed with a blank line between them.

Common errors and how to fix them

If sql-formatter cannot tokenise the input, the error appears above the panes and in the Output pane as Could not format SQL: Parse error: Unexpected "…" at line 1 column N. When Standard SQL is selected, the library adds a hint suggesting a more specific dialect.

Input that failsCauseFix
select a::int from t:: casts are PostgreSQL syntax, unknown in Standard SQL.Select PostgreSQL.
select a from t where x = @p@name parameters are T-SQL syntax.Select T-SQL (SQL Server).
select a from t where x = $1$1 placeholders are not part of Standard SQL.Select PostgreSQL.
select a from t where x = 'unterminatedA string literal with no closing quote.Add the missing '. Inside a string, write a quote as ''.
top stays lower case under UPPERNot an error: TOP is a T-SQL keyword, so Standard SQL treats it as a name.Select T-SQL (SQL Server).

Frequently asked questions

Is an SQL formatter the same as an SQL beautifier?

Yes. Formatting, beautifying, prettifying and pretty-printing SQL all mean laying a query out with one clause per line, indented joins and subqueries, and consistent keyword case, so it is easier to read and review. The query itself is not changed: the database runs the formatted version exactly as it ran the original.

Which dialect should I choose?

The one your query will run on. Standard SQL works for plain SELECT/INSERT/UPDATE statements, but database-specific syntax — PostgreSQL :: casts and $1 parameters, T-SQL TOP and @name parameters — formats correctly only with the matching dialect, and may fail to parse otherwise.

Does formatting change what my query does?

No. Beautify only changes whitespace, line breaks and keyword case, which databases ignore outside string literals and quoted identifiers. Minify collapses whitespace outside quotes and turns -- comments into /* … */ comments; text inside quotes is left alone. As with any tool, check the output before running it against a production database.

Does it check that my SQL is correct?

No. It formats layout and casing, but does not check that tables or columns exist, validate types or catch performance problems. A query that formats cleanly can still fail when it runs. Use your database's own tooling or a dedicated SQL linter for that.

Are comments kept when I format?

Beautify keeps both -- line comments and /* … */ block comments in place: -- active users above select id /* pk */ from users stays on its own line, and /* pk */ stays next to id. Minify keeps comments too, rewriting -- active users as /* active users */ so the one-line query still runs.

Should SQL keywords be uppercase?

SQL keywords are case-insensitive, so it is a matter of convention. Uppercase keywords are a long-standing habit that makes them stand out from table and column names; many teams prefer lowercase. Pick one with the Keyword case option and use it consistently.

Is my SQL sent to a server?

No. Formatting runs in your browser using sql-formatter loaded from cdn.jsdelivr.net. The query you paste or upload is processed locally and is not sent anywhere by the tool, so it is safe to format queries that mention internal table names.

Guides

Related tools