SQL Formatter

Turn a wall of SQL into something you can read — and minify it back to one line when you need to.

100% private — runs entirely in your browser. Your data is processed on your device and never sent to the internet.

                        
Download .sql

It formats as you type — there is nothing to press. Your query stays in this page, strings and comments are never looked inside, and identifiers keep the exact case you wrote.

About formatting SQL

SQL arrives as one enormous line. Out of an ORM's log, out of a bug report, out of somebody's clipboard. It runs perfectly and it is impossible to read, and the bit you are looking for is somewhere in the middle of it.

Paste it in and it comes back laid out — one clause per line, columns indented under SELECT, joins and their ON conditions lined up so the shape of the query is visible at a glance.

How to use it

  • Paste your SQL into the left box — the formatted, colour-coded version appears on the right as you type
  • Adjust the indent, keyword case and comma position if you like — the result updates straight away
  • Copy it, or download it as a .sql file
  • Switch to Minify to put it back on one line; it stays in that mode while you keep typing

There is no button to press to format. The only two buttons that change the result are

Format and Minify, and they pick which of the two you are looking at.

Colour comes from the same reading of the query

The output is coloured by running the same tokeniser that laid it out back over the

finished text — keywords cyan, strings yellow, numbers orange, comments grey, punctuation

pink, and your own table and column names left plain so they stand out from the SQL around

them.

That matters for more than looks. Because one reading of the query produces both the

layout and the colour, the two can never disagree: anything painted as a string is exactly

what the formatter treated as a string. If a quote is unbalanced somewhere, the colour runs

on and shows you where — which is usually faster than finding it by eye.

Copy and Download hand over the plain text, never the coloured markup.

Two things it is careful about

Most quick SQL formatters work with regular expressions, and both of these are where that approach falls apart.

  • Strings and comments are never looked inside. A query containing WHERE note = 'from a, b' has a keyword, a comma and a space inside a string literal. A regex formatter breaks the line there and changes what the query does. This one reads the query into tokens first, so the inside of a literal is copied out exactly as it went in — down to the spacing
  • Identifiers keep the case you wrote. Only keywords are upper- or lower-cased. On a case-sensitive server userId and userid are different columns, and a formatter that "tidies" them has broken the query to make it look neater

Quoting styles are all understood: 'strings' with '' escapes, "quoted identifiers", MySQL backticks , SQL Server [brackets], and PostgreSQL $$ dollar quoting $$.

The options

  • Indent with — 4 spaces, 2 spaces or a tab
  • Keywords — UPPERCASE, lowercase, or left exactly as you wrote them
  • Commas — at the end of the line, or leading commas at the start, if that is your house style

Minify

Minify collapses the query back to a single line. It keeps a space only where removing it would change the meaning or create a new token, so the result is as short as it can safely be.

Line comments are dropped, because -- like this would swallow the rest of the line once everything is on one line. Block comments are kept, exactly as written.

What people use it for

  • Making a query out of an ORM log readable
  • Tidying SQL before putting it in a pull request or a migration
  • Understanding a long query somebody else wrote
  • Getting a query out of a bug report into a shape you can work with
  • Making a query fit on one line for a config file or a shell command
  • Standardising the style of a folder of .sql files before committing them

Good to know

  • Your query stays in this page — nothing is sent anywhere. Handy when the query has real table and column names in it
  • This lays out SQL; it does not check that it is valid. A query with a syntax error is formatted as best it can be, not rejected
  • Dialect-specific quoting is supported, but this does not rewrite between dialects
  • Very large queries are fine — it is doing simple work on your own machine
  • Works in every browser, including on a phone

Related tools

We add new tools regularly — subscribe on YouTube to be notified when each one goes live.

More tools

View all

Recommended Apps

View all