SQL Formatter

Developer

Lay out a query to be read, without rewriting what is in quotes.

Formatted SQL
SELECT u.name, COUNT(o.id) AS orders FROM users u LEFT JOIN orders o ON o.user_id = u.id WHERE u.active = TRUE AND u.note <> 'select one from the list' GROUP BY u.name ORDER BY 2 DESC
35 tokens · 1 statements
Tokens35
Statements1
This lays SQL out to be read. It does not parse, validate or rewrite it — a formatter that claimed to validate SQL would be claiming to implement a dialect.
Text inside quotes was left exactly as it is. A formatter that uppercases keywords with a plain search would turn a note reading "select one from the list" into SQL-looking nonsense — silently changing data inside a query you asked only to tidy.
Only keywords were recased. Identifiers are never touched: in PostgreSQL an unquoted identifier folds to lower case and a quoted one does not, so changing the case of one can change which column it refers to.

How to use

What it's for

Lay out a long database query so a person can read it — each clause on its own line, the conditions indented under WHERE — and make the keywords one consistent case.

Before you start

Have the query to hand, copied from your code, a log or a database tool. It is formatted in your browser and nothing is sent anywhere, which matters when a query names your own tables or holds real values.

  1. Paste the query

    Replace the sample in Query with your own SQL. It can be one long line, or several statements separated by semicolons.

  2. Choose how keywords look

    Keywords are SQL's own words, such as SELECT, FROM and WHERE. In Keywords, pick UPPER CASE to make them stand out from your names, lower case if your team writes it that way, or Leave as written to change only the layout.

  3. Choose the indent

    Indent sets how far lines are pushed in — the AND and OR lines under WHERE, and a query inside brackets: Two spaces, Four spaces or Tab. Match the rest of your code.

  4. Format and copy

    Press Format. The tidied query is in the band at the top of the card — it also changes as you type — and Copy puts it on your clipboard.

Reading the result

  • Formatted SQL: your query, laid out again. Text inside quotes and the names of your tables and columns are left exactly as you wrote them; only keywords change case.
  • Tokens: how many pieces the query was split into — words, symbols and quoted values.
  • Statements: how many queries it holds, counted by the semicolons that end them.

If it goes wrong

  • It formatted a query that you know is broken: that is on purpose. It lays out the query but does not check it — a note on the page says so — so seeing the shape can help you find the mistake.
  • Read the result through before you run it, and if anything looks wrong, run the query exactly as you had it.
  • A column name kept its odd capitals: names are never changed, because in some databases changing the case of a name can point the query at a different column.

What this is

What is inside quotes is not code, and that is the whole difficulty. A formatter that uppercases keywords with a plain search turns a note reading 'select one from the list' into 'SELECT one FROM the list' — silently changing data inside a query you asked only to tidy.

So it tokenises first, and only a keyword is ever recased. Comments are treated the same way.

Column names are not touched either. In PostgreSQL an unquoted identifier folds to lower case and a quoted one does not — so recasing one can change which column a query means.

And it does not parse or validate. It lays a statement out to be read. A formatter claiming to validate SQL would be claiming to implement a dialect.

Questions

Will it change text inside my quotes?

No, and that is the main thing this tool gets right. A formatter that uppercases keywords with a plain search turns a note reading “select one from the list” into SQL-looking nonsense — silently changing data inside a query you asked only to tidy. This tokenises first, so only something identified as a keyword is ever recased. Comments are left alone for the same reason.

Why does it not uppercase my column names?

Because case can be meaningful. In PostgreSQL an unquoted identifier folds to lower case while a quoted one does not, so "Order" and Order are different columns — recasing one can change which column a query means. Only keywords are touched.

Does it handle backticks and square brackets?

Yes. Double quotes are the standard identifier delimiter, backticks are MySQL and square brackets are SQL Server, and all three are kept exactly as written. That is what makes a column called order work, and getting it wrong is how a formatter mangles a perfectly valid query.

Does it check my SQL is valid?

No. It lays a statement out so a person can read it, which is what you need when staring at a 400-character line from an ORM. It does not parse, validate or rewrite — a formatter claiming to validate SQL would be claiming to implement a dialect, and there are several that disagree.

What happens to a broken query?

It gets formatted anyway. Somebody with a query that will not run still wants to see its shape, and refusing to lay it out removes the one thing that might show them where it went wrong.

Is my query sent anywhere?

No. Everything happens in your browser and nothing is transmitted or stored — which matters here, because a query often carries table names, column names and sometimes real values from your own database.

The maths

What is changed and what is not
Keywordsrecased
Column namesuntouched
Text inside quotesuntouched
Commentsuntouched
Three dialects, three quotes
"order"the standard, PostgreSQL
`order`MySQL
[order]SQL Server

All three are kept as written. That is what makes a column called order work.

Keep going

Important

Runs entirely in your browser; nothing is transmitted. This lays a query out to be read — it does not parse, validate or rewrite it. Only keywords are recased; identifiers and anything inside quotes are left exactly as written. Nothing you type here is sent to our servers — the calculation runs entirely in your browser.