Skip to content
AgileLogics

SQL Formatter

Reformats a SQL query by starting a new line at each major clause keyword (SELECT, FROM, WHERE, JOIN, GROUP BY, and so on). This is a lightweight, keyword-based formatter rather than a full SQL parser, so it won't validate or restructure the query itself.

How it works

Paste your SQL query into the input panel and click Format.

What is a SQL formatter?

A SQL formatter rewrites a query so each clause starts on its own line, which makes long statements from logs, ORMs or one-line scripts readable. This one is a lightweight keyword-based formatter: it puts major clauses like SELECT, FROM, WHERE and ORDER BY on new lines and lists columns one per line, without parsing the SQL.

How to format a SQL query

  1. Paste your query into the input box. It can be on one line or spread across several.
  2. Click Format.
  3. Copy the result back into your editor or database client.

Worked example

This query, copied from an application log:

select id, email, created_at from customers where country = 'UK' and active = 1 order by created_at desc limit 20

is formatted as:

SELECT id,
  email,
  created_at
FROM customers
WHERE country = 'UK'
AND active = 1
ORDER BY created_at desc
LIMIT 20

select, from, where, and, order by and limit were uppercased and moved to new lines. Each comma starts a new line indented by two spaces. desc stayed lowercase because it isn't one of the keywords the formatter looks for.

How it works

The formatter runs three passes over the text:

  1. Collapse whitespace. Every run of spaces, tabs and line breaks becomes a single space, so the original layout doesn't matter.
  2. Break at keywords. Each keyword from the list below gets a line break before it and is rewritten in uppercase. Matching is case-insensitive and whole-word only, so a column named updated_at or setting isn't touched.
  3. Break at commas. Every comma is followed by a line break and two spaces of indentation.

The keywords it breaks on:

Group Keywords
Queries SELECT, FROM, WHERE, AND, OR, GROUP BY, HAVING, ORDER BY, LIMIT, UNION, UNION ALL
Joins JOIN, LEFT JOIN, RIGHT JOIN, INNER JOIN, OUTER JOIN, ON
Writes INSERT INTO, VALUES, UPDATE, SET, DELETE FROM
Schema CREATE TABLE, ALTER TABLE, DROP TABLE, NOT NULL, PRIMARY KEY, FOREIGN KEY, DEFAULT

Limits to know about

Because it works on keywords instead of understanding the query, a few results look odd or need checking. These were all confirmed by running them through the tool:

  • Two-word joins split in two. LEFT JOIN comes out as LEFT on one line and JOIN orders o on the next. The same happens to RIGHT, INNER and OUTER joins, and DELETE FROM becomes DELETE then FROM .... The query still means the same thing.
  • Subqueries aren't indented. SELECT a FROM (SELECT a FROM b) x puts the inner SELECT on its own line at the same indentation as the outer one.
  • Every comma breaks a line, including commas inside VALUES (1, 'Ada') and function arguments.
  • AND inside BETWEEN breaks too. BETWEEN 1 AND 5 is split across two lines.
  • String literals aren't protected. WHERE description = 'Ships from Leeds' becomes 'Ships on one line and FROM Leeds' on the next, which changes the value. Commas inside strings are also followed by a line break.
  • Comments can swallow code. -- comments run to the end of the line (PostgreSQL, MySQL and SQLite all agree on this). Since the formatter removes line breaks first, code on the line after a comment can be pulled onto it: SELECT price -- in cents followed by * 100 comes out as SELECT price -- in cents * 100, so the multiplication is now commented out. The opposite also happens: a commented-out -- WHERE id = 2 can come back as live code on its own line. Remove -- comments, or convert them to /* */, before formatting.

For quick reading of a logged query these rarely matter. If you plan to run the formatted SQL, and the original had string literals with keywords or commas, or -- comments, compare the two with the code diff tool first.

Formatting conventions

There's no official SQL style, but most teams agree on a few things this tool already does: uppercase keywords, one clause per line, and one column per line in long SELECT lists, which keeps version-control diffs small when a column is added. Leading-comma style (, email) and right-aligned keywords are matters of taste the tool doesn't offer.

Related pages

Last updated

Frequently asked questions

Which SQL dialects does it support?
Any, in the sense that it doesn't parse SQL at all. It looks for a fixed list of common keywords such as SELECT, FROM, WHERE, JOIN, GROUP BY and LIMIT, which are shared by MySQL, PostgreSQL, SQLite and SQL Server. Dialect-specific clauses like TOP, RETURNING or WITH aren't on the list and stay where they are.
Does it check whether my query is valid?
No. It never runs or parses the query, so a misspelled table or a missing comma comes out formatted just like correct SQL. Test the query against your database to validate it.
Why are some keywords uppercase and others not?
Only the keywords the formatter breaks lines on are rewritten in uppercase. Others, such as AS, DESC, IN, IS NULL or function names like COUNT, are left exactly as you typed them.
Can formatting change what my query does?
Yes, in two situations. Keywords and commas inside quoted strings are split and uppercased too, so 'Ships from Leeds' becomes two lines with FROM in capitals, which changes the value. And line breaks are removed first, so anything after a -- comment can end up joined to the comment. Check queries that contain either before you run them.
Is my query sent to a server?
No. Formatting runs in your browser with a few string replacements. The query isn't uploaded or run anywhere.