SQL Formatter

Indent an SQL query so you can read it, entirely in your browser. Nothing is executed and no database is contacted.

Runs entirely in your browser. Nothing is uploaded, logged or stored.

Paste a query that arrived as one long line and read it properly. The formatting happens in your browser: the statement, the table names and whatever value is sitting in the WHERE clause never leave your machine.

How it works

The statement is split into pieces — strings, comments, quoted names, numbers, words, operators — and then written back out with line breaks and indentation chosen from that sequence. Clauses start a line, conditions line up under the clause they belong to, and a subquery is indented inside its brackets.

Every piece is written back exactly as it arrived. Only the spaces between them, and the case of words the tool recognises, can change — which means the statement that comes out is the statement that went in. That property is asserted by the tests rather than left to inspection.

A bracket that contains a statement of its own is broken across lines; a function call, an IN list or a bracket that only groups an expression stays on one line, because breaking those makes a query taller without making it clearer.

Examples

Case Input Result
A query on one line select id, name from users where active = 1 order by name SELECT id, name FROM users WHERE active = 1 ORDER BY name

Frequently asked questions

Is my query sent anywhere?

No. The formatter is JavaScript running in this page. Nothing is uploaded, nothing is logged and nothing is stored — you can turn off your network connection and the tool still works.

Does it check whether my SQL is valid?

No, and it is worth being clear about that. It rearranges whitespace. It has no schema, no dialect grammar and no database, so it will happily indent a statement that no server would accept. The only things it refuses are a string or a comment that never closes, because those leave it unable to tell code from text.

Which dialect does it understand?

It recognises the quoting styles of the common ones — single quotes for strings, and double quotes, backticks or square brackets for names — so it will not garble MySQL, PostgreSQL, SQLite or SQL Server. It does not check dialect grammar, because it does not parse.

What is the backslash option for?

MySQL treats a backslash inside a string as an escape, so 'it\'s' is one string. The SQL standard — and PostgreSQL, SQLite, SQL Server and Oracle — does not, so there the same text is a string followed by something else. There is no way to tell from the query alone, and guessing would corrupt one dialect or the other, so it is a switch. Leave it off unless you are working in MySQL.

Why did my column name change case?

Because it is spelled like a keyword. The casing option touches words the tool recognises, and it cannot tell a column called "key" from the keyword. Names that come straight after TABLE, INTO, FROM, JOIN or UPDATE are left alone, since those are the positions where the case of a name can actually matter. If you would rather nothing moved, set the keyword option to leave words as written, or quote the name.

Good to know

  • Nothing on this page executes SQL or connects to a database. The statement is text, and it is treated as text.
  • The layout is deterministic: the same statement and the same options always produce the same output, and formatting an already formatted statement changes nothing.
  • CASE expressions, joins, subqueries and CREATE TABLE column lists each have a rule. Anything the rules do not cover is left on the line it lands on rather than guessed at.

All Code tools