Developer · Formatter

SQL formatter.

Beautify or minify SQL for standard SQL, PostgreSQL, MySQL and SQLite. Pick the keyword case, indent and comma position. Runs in your browser; your query is never uploaded.

Advertisement

Input

Format and Minify only change whitespace, case and comments. They do not run the query or check it against a database.

Output

Paste SQL, then press Format or Minify.

Advertisement

Worked example

This 20-line result is what the page produces for one PostgreSQL query typed on a single line (Load example fills it in). With UPPER keywords, 2-space indent and leading commas:

SELECT
  u.id
, u.email
, count(o.id) AS orders
, sum(o.total) AS spend
FROM
  users u
  LEFT JOIN orders o ON o.user_id = u.id
WHERE
  u.created_at >= '2026-01-01'
  AND u.status IN ('active', 'trial')
GROUP BY
  u.id
, u.email
HAVING
  count(o.id) > 0
ORDER BY
  spend DESC
LIMIT
  20;

Minify turns the formatted version into one line: 291 bytes become 260, a 10.7% saving. SQL has little whitespace to remove, so the saving is modest. The main use of minifying is putting a query on one line in a config file, log line or string literal.

How it works

Format uses the open-source sql-formatter library, version 15.9.0, MIT licence, served from this site rather than a third-party CDN. It parses the query with a dialect-specific grammar, so PostgreSQL, MySQL and SQLite syntax (for example :: casts, backtick identifiers, ILIKE) is recognised. Choose Standard SQL if your dialect is not listed. If the library cannot parse the text it reports an error instead of guessing.

Comma position. The library always puts commas at the end of a line. For Leading, this page moves each line-ending comma to the start of the next line, using a scanner that skips string literals, quoted identifiers and comments so commas inside them are never moved. A comma that is followed by a comment line is left in place.

Minify is this page's own scanner. It removes comments, collapses runs of whitespace to one space, and removes the space around commas, semicolons and just inside parentheses. It never changes anything inside a quoted string or identifier. It keeps PostgreSQL dollar-quoted strings ($$...$$) and MySQL executable and hint comments (/*! ... */, /*+ ... */) because removing those changes what the query means. It also treats -- as a comment in MySQL only when followed by whitespace, and # as a comment only in MySQL, as that dialect defines.

Sources and as-of date (checked 2026-09-30): sql-formatter on GitHub (library and supported dialects); PostgreSQL: Lexical Structure (comments, nested block comments, dollar quoting); MySQL 8.4: Comments (#, -- and executable comments); SQLite: SQL Comment Syntax.

How to format SQL for PostgreSQL, MySQL or SQLite

Pick the Dialect first. The dialect decides which keywords, quoting rules and operators the parser expects, so PostgreSQL’s :: casts and ILIKE, MySQL’s backtick identifiers and SQLite’s syntax are read correctly instead of failing as unknown tokens. Then choose keyword case, indent and comma position, paste the query and press Format. Use Standard SQL if your database is not listed.

A second worked example: orders by customer region

This query arrives as one 295-character line from an application log:

select o.id, c.name, sum(oi.qty*oi.price) as total from orders o join customers c on c.id=o.customer_id left join order_items oi on oi.order_id=o.id where o.created_at >= '2026-01-01' and c.region in ('ON','QC') group by o.id, c.name having sum(oi.qty*oi.price)>100 order by total desc limit 10;

With the PostgreSQL dialect, UPPER keywords, 2-space indent and trailing commas, each clause starts on its own line, joins are indented under FROM, and each condition in WHERE gets a line:

SELECT
  o.id,
  c.name,
  sum(oi.qty * oi.price) AS total
FROM
  orders o
  JOIN customers c ON c.id = o.customer_id
  LEFT JOIN order_items oi ON oi.order_id = o.id
WHERE
  o.created_at >= '2026-01-01'
  AND c.region IN ('ON', 'QC')
GROUP BY
  o.id,
  c.name
HAVING
  sum(oi.qty * oi.price) > 100
ORDER BY
  total DESC
LIMIT
  10;

Notice that sum stays lowercase while SELECT, AS and JOIN are uppercased: the keyword-case option applies to keywords, not to function names or identifiers. Minify returns the query to a single line of 292 characters, only three fewer than the input, because the input had little white space to begin with. Minifying matters when a query is pretty-printed, not when it is already compact.

Choosing between trailing and leading commas

Trailing commas (a, at the end of the line) are the usual style. Leading commas (, a at the start) make it easy to comment out or add the last column without touching the previous line, which is why some teams use them in long select lists. The two layouts are the same query. When you pick leading commas this page moves the commas itself, and it skips commas inside string literals, quoted identifiers and comments, so 'a,b' is never split.

What the formatter will not do

Comments and string literals

PostgreSQL allows nested block comments and dollar-quoted strings such as $$ ... $$, and MySQL treats # and -- (with a space) as line comments and /*! ... */ as code that the server may run. Minify understands these rules, so it will not delete a hint comment that changes behaviour or collapse spaces inside a string such as 'it''s ok'.

Using formatted SQL in code review

Formatting a query before you commit it makes diffs readable: a change to one condition shows up as one changed line instead of a rewritten 300-character line. Agree on one dialect, one keyword case and one comma style in the team, and keep the formatted text in your repository. Use minify only for the copy that goes into a config value or log line.

Sources

Each source below was opened and checked in October 2026 (retrieved 2026-10).

Advertisement

Frequently asked questions

Is my SQL uploaded anywhere?

No. Formatting and minifying run in your browser using JavaScript served from this site. Nothing you paste is sent to a server or stored.

Does formatting change what my query does?

It should not. Formatting changes whitespace and, if you choose, the case of keywords. Identifiers and strings are left as typed. Always review the result before running it on real data, and keep your original.

Does this validate my SQL?

No. A successful format means the text could be parsed by the formatter, not that the query is valid for your database, that the tables exist or that it will run. Only your database can tell you that.

Why did minify keep a comment?

Comments that start with /*! or /*+ are executable or optimizer-hint comments in MySQL, so they are kept on purpose.

How do I format a PostgreSQL query online?

Set Dialect to PostgreSQL, paste the query and press Format. The parser then understands PostgreSQL syntax such as :: casts and ILIKE, and prints each clause (SELECT, FROM, WHERE, GROUP BY and so on) on its own line with joins and conditions indented.

Should SQL keywords be uppercase or lowercase?

Either works; the database does not care. Uppercase keywords are the long-standing convention because they stand out from table and column names, while some teams prefer lowercase for readability. The option here only changes keywords, never identifiers or function names.

Why are function names like sum and count not uppercased?

The keyword-case option applies to reserved words such as SELECT, FROM and JOIN. Function names are treated like identifiers by the formatter, so sum(x) stays as you typed it.

What is the difference between leading and trailing commas?

Only the position of the comma in a multi-line list. Trailing puts it at the end of each line, leading puts it at the start of the next. The query means the same. Leading commas make it easier to comment out the last column.

Will minified SQL still run the same?

It should. Minify removes comments, collapses white space and leaves quoted strings, quoted identifiers, dollar-quoted strings and MySQL hint comments alone. Test it on a copy of your data before relying on it, because this page cannot run the query.

Which dialects are supported?

Standard SQL, PostgreSQL, MySQL and SQLite. The formatting library itself supports more dialects, but this page offers those four. If yours is missing, Standard SQL is the safest choice for ordinary SELECT, INSERT, UPDATE and DELETE statements.

Formatting only

This tool rearranges the text of a query. It does not execute SQL, check it against a schema or review it for safety. Nothing you type is stored or sent anywhere.