How to use the sql formatter
- Paste your SQL into the SQL box, or choose a .sql or .txt file. You can paste several statements separated by semicolons.
- Pick the dialect that matches your database: Standard SQL, PostgreSQL, MySQL or SQL Server. The dialect decides how strings, quoted names and comments are read, so choose it before you trust the result.
- Choose Formatted lines to break the query into clauses, or One line to squeeze it onto a single line. Set the keyword case (upper, lower or as typed) and the indent.
- Press Copy result or Download .sql. If the text has an unclosed quote or bracket, the message gives the line and column and a button jumps the cursor to it.
One long line becomes a readable query
The text "select a, b as bee, count(*) as n from t left join u on t.id = u.t_id where a > 1 and b in (1,2,3) group by a order by a desc limit 10;" becomes SELECT on its own line with a, b AS bee and COUNT(*) AS n each on a line below it, then FROM with t and the LEFT JOIN, then WHERE with each AND condition on its own line, and so on through GROUP BY, ORDER BY and LIMIT.
Function calls such as COUNT(*) and lists such as IN (1, 2, 3) stay on one line, while subqueries in parentheses get their own indented block. A string such as 'a b' with two spaces inside keeps exactly those two spaces.
What the formatter changes and what it never touches
The formatter changes only three things: spaces and line breaks between pieces of the query, the indentation, and the letter case of reserved words such as select, from and where. Names that are not reserved words, quoted names, strings, numbers, comments, operators and parameters such as :name, $1 or ? are copied exactly as written.
After formatting, the result is read again and compared piece by piece with your text. If anything differs other than spacing and keyword case, the tool shows a message instead of a changed query. In testing, thousands of random statements and a set of real-looking queries were checked this way against a second, independent reader.
Dialects, strings and comments
In standard SQL and PostgreSQL a single quote inside a string is written twice, and a backslash is an ordinary character. MySQL also treats a backslash before a quote as an escape. If a MySQL string with an escaped quote is read as standard SQL, the quotes pair up the wrong way and the text between them could be reformatted, so the tool warns about strings that end with a backslash and refuses text with an unclosed quote.
PostgreSQL dollar-quoted text between $$ marks and E-prefixed strings are kept whole. SQL Server square-bracket names and @variables are understood, and MySQL hash comments are kept. In one line layout, ordinary comments are removed because a line comment would swallow the rest of the query; optimizer hints that start with /*+ and MySQL executable comments that start with /*! are kept.
Limits
This is a layout tool. It does not know every keyword of every database, does not check that tables or columns exist, and does not validate a query. Reserved words it does not know keep the case you typed. For related text and data work, see the CSV to SQL generator, the JSON to SQL table generator and the text line tools.
Assumptions and limits
- Only reserved words and common function names are changed in case. Names of tables, columns and aliases are never changed, because some databases treat them as case sensitive.
- Parentheses must balance and quotes must be closed. Text that does not meet this is refused with a line and column instead of being guessed at.
- Very long input is limited to 1,000,000 characters and 400,000 pieces, and parentheses may be nested up to 200 levels.
- The one thing trimmed inside a comment is the run of spaces at the very end of a line comment, which has no meaning.
Frequently asked questions
Will formatting change what my query does?
It is built not to. Only spaces, line breaks and the case of reserved words change, and the result is checked against your text before it is shown. Still, review important queries before running them, especially when you chose the wrong dialect.
Why did the tool refuse my query?
The usual causes are a missing closing quote or comment end, unbalanced parentheses, or a MySQL string with a backslash read as standard SQL. The message names the line and column, and the button beside it moves the cursor there.
Can I format several statements at once?
Yes. Separate them with semicolons and each statement is formatted on its own with a blank line between them.
Does it support stored procedures and vendor extensions?
It lays out the common parts of queries, inserts, updates, deletes and simple table definitions. Procedural code and vendor extensions are kept intact but may not get ideal line breaks, and that is safe because only spacing changes.