Last reviewed:
What is SQL formatting?
SQL is whitespace-insensitive: a query on one 400-character line runs exactly the same as the same query spread over twenty lines. That freedom is why SQL copied out of ORM logs, query plans, BI tools and monitoring dashboards is often unreadable, with every clause, join and condition run together. Formatting rewrites the layout without changing the meaning: each major clause (SELECT, FROM, WHERE, GROUP BY and so on) starts on its own line, the items under it are indented, and keywords get a consistent case.
Readable layout is more than cosmetic. A misplaced OR that turns a filtered query into a full table scan, a join condition attached to the wrong table, or a missing GROUP BY column is far easier to spot when each condition sits on its own line. Consistent formatting also makes code review and version control diffs meaningful, because only real changes show up.
SQL has many dialects. They share the core grammar but differ in quoting and operators: MySQL quotes identifiers with backticks, SQL Server uses square brackets, PostgreSQL has :: casts and JSON operators such as ->> and @>. A formatter must tokenise the query correctly before it can lay it out, so choosing the right dialect matters. This tool uses the open-source sql-formatter library, which supports eleven dialects.
How to use this tool
- Paste your query into Input, or click Paste to read it from the clipboard. Sample loads a reporting query with a join and aggregates.
- Choose your database in the dialect menu: Standard SQL, MySQL, MariaDB, PostgreSQL, SQL Server, Oracle PL/SQL, SQLite, BigQuery, Snowflake, Redshift or IBM Db2.
- Pick a keyword style: KEYWORDS UPPER, keywords lower or Keep keyword case, and an indent of 2 spaces, 4 spaces or a tab.
- With Format as I type ticked, the output updates as you edit. Otherwise click Format. Changing any option re-formats immediately.
- Read the status line under the input. If the query cannot be tokenised for the chosen dialect, it shows the parse error with the line and column.
- Click Copy, or Download to save
formatted.sql. Several statements separated by semicolons are formatted together with a blank line between them.
Examples
A one-line reporting query (Standard SQL, upper case, 2 spaces)
Input:
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.active = true and o.created_at >= '2026-01-01' group by u.id, u.email having count(o.id) > 2 order by orders desc limit 10;
Output:
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.active = TRUE
AND o.created_at >= '2026-01-01'
GROUP BY
u.id,
u.email
HAVING
count(o.id) > 2
ORDER BY
orders DESC
LIMIT
10;
Keywords are upper-cased, but function names such as count keep the case you wrote. String literals are never changed.
PostgreSQL operators and a CTE
With the PostgreSQL dialect, this input:
select id::text, data->>'name' as name from events where payload @> '{"type":"signup"}' ;
becomes:
SELECT
id::text,
data ->> 'name' AS name
FROM
events
WHERE
payload @> '{"type":"signup"}';
The same input with Standard SQL selected fails with Parse error: Unexpected "::text fro" at line 1 column 10., because :: is not part of standard SQL.
Comments and several statements
-- active users
SELECT
id /* pk */
FROM
users
WHERE
active = 1;
UPDATE users
SET
active = 0
WHERE
last_login < '2025-01-01';
Both comment styles are kept where they were, and each statement is separated by a blank line.
Common errors and how to fix them
- Parse error on
::,->>or@>. These are PostgreSQL syntax. Select PostgreSQL (or Redshift for::). - Parse error on
[column name]or`column`. Square brackets are SQL Server identifier quotes and backticks are MySQL/MariaDB ones. Choosing the wrong dialect makes them unexpected characters; switch to the matching dialect. - Parse error on a named parameter such as
:id. Placeholder syntax varies by dialect and driver. Try the dialect your driver targets, or temporarily replace the placeholder with a literal value. - Error pointing at a quote. An unterminated string, like
'unterminated, swallows the rest of the query. Check that every'is closed and that quotes inside strings are doubled ('it''s'). - Template variables. Queries containing
{{ var }}from dbt or Jinja, or${...}from code, are not SQL yet. Format the rendered query, or replace the placeholders first. - Valid-looking output, wrong query. The formatter only checks that the text can be tokenised. It does not know your tables or check semantics, so a misspelt column passes. Run the query, or
EXPLAIN, against the database to be sure.
When developers use this
- Reading a long query captured from ORM debug logs or
pg_stat_statementsto understand why it is slow. - Cleaning up a query before pasting it into a pull request, a ticket or documentation.
- Normalising two versions of a migration or view definition so a diff shows only the real changes.
- Reviewing a query generated by a BI or reporting tool before adding it to an application.
- Checking which
ANDandORconditions belong together in a complicatedWHEREclause.
Privacy and how it works
The sql-formatter library runs inside your browser tab, and your query is never uploaded or logged. That matters because real queries often contain table names, customer emails, IDs or literal values copied from production. Nothing connects to a database, either; the tool only rearranges text.
Limitations
- Formatting is not validation. A query that formats cleanly can still fail on the database because of a missing table, wrong column or type mismatch.
- Procedural code, such as long PL/pgSQL or T-SQL procedures with control flow, is laid out less neatly than plain queries.
- Some words that are keywords in your database, such as
intervalorcurrent_date, may be treated as names and keep their original case.
FAQ
Does formatting change what my query does?
No. Only whitespace, line breaks and keyword case change. Identifiers, string literals, numbers and comments are kept as written, so the database runs the same statement.
Which dialect should I pick if mine is not listed?
Pick the closest relative: Standard SQL for most ANSI-style databases, PostgreSQL for systems built on it, MySQL for compatible engines such as TiDB. If you see a parse error on syntax specific to your database, try another close dialect.
Should SQL keywords be upper or lower case?
It is a team convention, not a technical requirement; SQL keywords are case-insensitive. Upper case makes keywords stand out from names in plain text, while many modern style guides prefer lower case. Pick one and keep it consistent.
Will it keep my comments?
Yes. Both -- line comments and /* block comments */ stay in the output at their original positions.
Why are function names not upper-cased?
The keyword case option applies to SQL keywords such as SELECT and JOIN. The formatter treats function names like count or max separately and keeps them as you wrote them. Write them in the case you prefer and they will stay that way.
Can it format several statements at once?
Yes. Separate them with semicolons and they are formatted in order with one blank line between statements, which suits migration files and scripts.
Does it check that my query is correct?
Only that it can be split into tokens for the chosen dialect. It does not know your schema and does not run anything, so semantic errors only show up when the database executes the query.