SQL Formatter
Format SQL queries with keywords on new lines.
Was this tool helpful?
Thanks for the feedback!
This SQL formatter breaks a one-line statement into readable lines by starting each main clause on a new line and writing its keyword in capitals. It is meant for quickly reading a query copied from application logs, an ORM debug message or a colleague's chat, rather than for enforcing a full style guide.
Exactly what changes
When you click Format SQL, all runs of spaces, tabs and line breaks are collapsed to single spaces. A line break is then inserted before each of these keywords, which are also converted to upper case:
SELECT, FROM, WHERE, JOIN, LEFT JOIN, RIGHT JOIN, INNER JOIN, ON, GROUP BY, ORDER BY, HAVING, LIMIT, UNION, INSERT INTO, VALUES, UPDATE, SET and DELETE FROM.
Everything else, including table names, functions, AND, OR, AS and DESC, keeps the case you typed. No indentation is added.
Before and after
select c.city, count(*) as orders from orders o join customers c on o.customer_id = c.id where o.status = 'paid' group by c.city order by orders desc limit 5
becomes
SELECT c.city, count(*) as orders FROM orders o JOIN customers c ON o.customer_id = c.id WHERE o.status = 'paid' GROUP BY c.city ORDER BY orders desc LIMIT 5
An update such as update products set price = price * 1.18 where category = 'books' becomes three lines starting with UPDATE, SET and WHERE.
Check these spots before running the output
- Single-line comments. Because line breaks are removed first, a
--comment swallows whatever followed it on the next line.select a, -- first columnfollowed byb from tcomes out asSELECT a, -- first column b, which silently drops column b. Delete--comments or turn them into/* */comments before formatting. - Text inside quotes. Keywords surrounded by spaces are moved even inside string literals:
'ships from Pune'gets split across two lines with FROM in capitals, which changes the value. Compare literals with the original. - Subqueries. A
FROMinside brackets also starts a new line, without extra indentation, while aselectdirectly after an opening bracket is left as typed. - Join types.
LEFT JOIN,RIGHT JOINandINNER JOINcurrently come out with the join type on one line andJOINon the next; put them back together if that bothers you.
What it does not do
It does not check syntax, so a missing comma or bracket passes through unnoticed, and it treats MySQL, PostgreSQL, SQL Server and SQLite alike because it only looks at keywords. CREATE TABLE and other schema statements stay mostly on one line. For turning spreadsheet rows into INSERT statements, try the CSV to SQL Converter; when an API hands the data back as JSON, the JSON Formatter covers that side.