How the SQL Formatter works

The formatter first splits your SQL into tokens: keywords and names, numbers, operators, string literals, quoted identifiers and comments. The rules for the chosen dialect decide what counts as a string or a name, for example [brackets] and N'...' in SQL Server, $$...$$ strings and :: casts in PostgreSQL, or `backticks` and # comments in MySQL. Strings, quoted names and comments are kept as single tokens, so their contents are never changed.

The tokens are then written out again with new line breaks. Each major clause (SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, VALUES, SET) starts a new line with its contents indented below it, the select list has one column per line, each JOIN and each AND or OR condition gets its own line, and subqueries and CASE expressions are indented one more level. Statements separated by semicolons are separated by a blank line. The SQL is never run or checked against a database.

How to use the SQL Formatter

  1. Paste your SQL into the input box, open a .sql file, or select Load example.
  2. Choose the dialect (SQL Server, PostgreSQL or MySQL), the keyword case (UPPER, lower or as written) and the indentation (2 spaces, 4 spaces or tabs).
  3. Select Format SQL, or press Ctrl + Enter (Cmd + Enter on a Mac).
  4. Copy the formatted SQL or download it as a .sql file. Changing an option reformats the current input straight away.

Example

This single-line query:

select c.id, c.name, count(o.id) as order_count from customers c left join orders o on o.customer_id = c.id where c.country = 'DE' and c.active = 1 group by c.id, c.name order by order_count desc;

is formatted with UPPER keywords and 2-space indentation as:

SELECT
  c.id,
  c.name,
  COUNT(o.id) AS order_count
FROM
  customers c
  LEFT JOIN orders o ON o.customer_id = c.id
WHERE
  c.country = 'DE'
  AND c.active = 1
GROUP BY
  c.id,
  c.name
ORDER BY
  order_count DESC;

Common use cases

  • Reading a query captured from an ORM log, SQL Profiler or a slow query log, where everything is on one line.
  • Tidying a stored procedure body or a report query before a code review so the joins and conditions are easy to follow.
  • Making nested subqueries and CASE expressions readable by indenting them under the clause they belong to.
  • Applying one keyword case and indentation style to SQL written by several people.
  • Separating a script of several statements into clearly spaced blocks, one statement per block.

Common errors and how to fix them

Unterminated block comment: the closing */ is missing
A /* comment was opened but never closed, so everything after it is read as comment text. Add the closing */, or remove the opening marker.
Unterminated dollar-quoted string
A PostgreSQL $$ or $tag$ block was opened without a matching closing tag, so the rest of the script is treated as string content. Add the closing tag and make sure it matches the opening tag exactly.
A vendor-specific construct is laid out oddly
The formatter covers the common syntax of SQL Server, PostgreSQL and MySQL. Select the matching dialect first. Unusual vendor extensions are kept intact rather than reflowed, which is safer than rewriting syntax the formatter does not fully understand.
Spacing inside a string literal was left alone
That is deliberate. String literals, dollar-quoted strings and quoted identifiers are copied exactly, including their spaces and line breaks, because changing them would change what the query does.

Frequently asked questions

Does the formatter change what my SQL does?

No. It only changes the whitespace between tokens and, if you choose UPPER or lower, the letter case of SQL keywords. String literals, quoted identifiers such as [Order Details] or "Order Details", and comments are copied exactly as written.

Does it check that my SQL is valid or run it?

No. The formatter never runs your SQL and does not connect to a database. It reads the text as tokens to decide where lines should break, so a query with a mistake in it is still formatted. It only reports errors that stop it from reading the text, such as a string with no closing quote.

Why does the dialect matter?

Each database reads some characters differently. SQL Server uses [brackets] for names and allows GO between batches, PostgreSQL has $$ dollar-quoted strings, E'' strings and :: casts, and MySQL uses `backticks`, # comments and backslash escapes inside strings. Choosing the dialect makes sure these are recognised and left untouched.

How are multiple statements handled?

Statements separated by a semicolon are formatted one after another with a blank line between them. In SQL Server mode a GO line is kept on its own line as a batch separator.

Which statements are formatted best?

SELECT queries (including JOINs, WITH common table expressions, UNION, subqueries and CASE), INSERT, UPDATE, DELETE and CREATE TABLE. Procedural code such as BEGIN ... END blocks and IF statements is kept correct but receives simpler line breaking.

Is my SQL uploaded to your server?

No. Formatting is done by JavaScript in your browser, and the SQL you paste or open is not sent to our server.