A SQL formatter that makes queries readable
A query typed in one long line runs fine but is hard to read, and that is usually what you get from an error log or an ORM debug output. Formatting SQL splits it into aligned, indented clauses, so you see at a glance which columns are read, which tables are joined and which filters apply. A shared layout also makes code review easier: a diff between two versions only shows the real changes.
How to use it
- Paste one or more statements into "Your SQL query", or open a .sql file. "Example" loads a query with a join, a subquery and a CASE.
- Pick the "Dialect": Standard SQL, MySQL / MariaDB, PostgreSQL, SQLite or SQL Server.
- Set "Keywords" (UPPERCASE, lowercase, Keep as typed), "Indentation", "Columns" (One per line or On a single line) and "Commas" (At the end or At the start of the line).
- Click "Format" or "Minify", then "Copy" or "Download (.sql)".
Example and conventions
Take select id, name from customers where city = 'Leeds' and active = 1 order by name;, which is 81 characters long. With the default settings the tool returns 10 lines: SELECT alone, then id, and name indented by two spaces, FROM and customers, WHERE followed by city = 'Leeds', then AND active = 1 on its own line, and finally ORDER BY and name;. Minified, the same query fits in 76 characters: SELECT id,name FROM customers WHERE city='Leeds' AND active=1 ORDER BY name;.
These choices follow the most common style guides:
- keywords in capitals, table and column names left as typed;
- one main clause per line: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT, UNION, INSERT INTO, VALUES, UPDATE, SET, DELETE FROM, CREATE TABLE;
- each JOIN starts a line with its ON condition, and each AND or OR in a filter goes on a new line, except the one in a BETWEEN;
- a subquery is indented one level, and a CASE puts each WHEN and the ELSE on their own lines.
What the formatter never changes
The tokenizer recognises string literals in single quotes, identifiers in double quotes, backticks or brackets, -- and /* */ comments, numbers, operators and bind parameters (?, :name, $1). The content of strings and comments is copied character for character: in WHERE note = 'select from where' the three words stay lowercase.
Dialects differ mostly in how they quote names: backticks and # comments in MySQL, brackets such as [Customer Name] in SQL Server, $$…$$ strings and the :: cast in PostgreSQL.
If a quote, a comment or a parenthesis is left open, the tool still formats the text and reports the line and column of the problem. This is not a full syntax check: a misspelt table name or a missing comma goes unnoticed, and nothing is ever run against a database.
Frequently asked questions
Can formatting change what my query returns?
No. Only whitespace and keyword case change, and SQL ignores keyword case. Watch out for an unquoted column named like a keyword (date, text, key): it gets the keyword case too. Quote it, or choose "Keep as typed".
Can I format a script with thousands of lines?
Yes. Statements separated by ";" are handled one after another, with a blank line between them. The work runs in the background without freezing the page: 5,000 lines take under a second on a recent computer. Files of up to 20 MB are accepted.
Does minifying and formatting again give the same layout?
Yes, with the same settings, because the layout depends only on the sequence of tokens. Minifying keeps -- comments and adds a line break after each one, so they do not swallow the rest of the query.