EthicalFusion

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 column followed by b from t comes out as SELECT 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 FROM inside brackets also starts a new line, without extra indentation, while a select directly after an opening bracket is left as typed.
  • Join types. LEFT JOIN, RIGHT JOIN and INNER JOIN currently come out with the join type on one line and JOIN on 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.

Frequently asked questions

Why did my LEFT JOIN split onto two lines?
The formatter adds a break before JOIN after it has already placed LEFT JOIN on a new line, so the join type ends up alone. Rejoin the two words; the meaning of the query is unaffected.
Can formatting break my query?
Yes, in two cases: a -- comment can end up commenting out the text that followed it, and keywords inside quoted strings can be split. Review those spots before executing.
Does it indent subqueries or CASE blocks?
No. It only adds line breaks before the main clause keywords and never indents anything.
Will it capitalise every SQL keyword?
Only the clause keywords it breaks on, such as SELECT, FROM, WHERE and ORDER BY. Words like and, as, count and desc stay as you typed them.
Which SQL dialects does it support?
It works on keywords rather than a full parser, so MySQL, PostgreSQL, SQLite and SQL Server queries are all formatted the same way.

Related tools

Embed this tool on your site

Paste it into any blog or website. Free, with a credit link.