SQL Formatter
Paste messy SQL and get clean, readable SQL back: keywords uppercased, one clause per line, conditions indented. String values are left untouched.
SQL Formatter
Rules: keywords are uppercased; SELECT, FROM, WHERE, GROUP BY, ORDER BY, JOIN, and other major clauses start on a new line; AND and OR conditions are indented two spaces. Text inside single quotes is never changed.
Formatting rules:
Keywords UPPERCASED | One major clause per line (SELECT, FROM, WHERE, GROUP BY, ORDER BY, JOIN, LIMIT, ...) | AND / OR indented two spaces | Text in single quotes is never changed.
Why Format SQL?
SQL is unusual among programming languages because it does not care about layout at all. A query written as one long line runs exactly the same as the same query spread across twenty lines with careful indentation. The database ignores whitespace, line breaks, and keyword case entirely. That freedom is convenient for machines and terrible for people: unformatted SQL is hard to read, hard to debug, and hard to review, and the longer the query, the worse it gets. A single-line query with three joins and five conditions is a wall of text where a wrong table name or a misplaced condition can hide for hours.
Formatting fixes that by giving every query the same predictable shape. Keywords stand out in uppercase, each major clause starts on its own line, and conditions line up underneath the WHERE they belong to. A formatted query can be scanned in seconds: the eye finds the FROM to see the tables, the WHERE to see the filters, and the ORDER BY to see the sort. That readability pays off every time someone debugs a slow query, reviews a colleague's change, or returns to their own code six months later and needs to remember what it does.
How to Use This Formatter
Paste your SQL into the input box and the formatted version appears in the output box as you type. Click Format SQL to reformat after pasting a large query, and use Copy formatted SQL to copy the result to your clipboard. The formatter is deterministic: the same input always produces the same output, so you can format a query, share it with a colleague, and know you are both looking at the same layout.
The default example shows the basic transformation. The input "select id, name from users where age > 30 order by name" becomes four lines: SELECT id, name, then FROM users, then WHERE age > 30, then ORDER BY name. Nothing about the query's meaning has changed: the same columns, the same table, the same filter, the same sort. Only the layout is different, and the difference is the whole point.
The Formatting Rules
This formatter applies a small set of rules, deliberately kept simple so the output is predictable. First, keywords are uppercased: select becomes SELECT, from becomes FROM, where becomes WHERE, and so on, including multi-word keywords such as GROUP BY, ORDER BY, LEFT JOIN, and INSERT INTO. Uppercase keywords are the most widely used SQL style convention, and they make the structure of a query visible at a glance because the keywords stand out from the table and column names, which keep their original case.
Second, each major clause starts on a new line: SELECT, FROM, WHERE, GROUP BY, ORDER BY, HAVING, JOIN and its variants, LIMIT, OFFSET, UNION, SET, and VALUES. A query that was one long line becomes a short stack of clauses, each doing one job. Third, AND and OR conditions are placed on their own lines, indented by two spaces, so a WHERE clause with several conditions reads as a list instead of a run-on sentence. Extra whitespace is collapsed to single spaces, and blank lines are removed.
Fourth, and most important, text inside single quotes is never changed. A string value such as 'shipped' or 'ann@example.com' passes through the formatter untouched, even if it contains words that look like keywords. A product named "fromage" stays lowercase, because the formatter recognizes that it is data, not SQL. This protection also handles escaped quotes written as two single quotes, the standard SQL way to include a quote inside a string.
Worked Example: A Join Query
Take a typical reporting query: "select u.name, o.total from users u left join orders o on u.id = o.user_id where o.total > 50". As one line, it is 93 characters of undifferentiated text, and the join condition hides in the middle. Formatted, it becomes four lines. The first line, SELECT u.name, o.total, says what the query returns. The second, FROM users u, says where the data comes from. The third, LEFT JOIN orders o ON u.id = o.user_id, says how the second table connects, with the join condition right there on the same line. The fourth, WHERE o.total > 50, says which rows survive. Each line answers one question about the query, and a reader can check each answer independently.
The same query with several conditions shows the indentation rule. "select * from t where a = 1 and b = 2 or c = 3" formats to five lines: SELECT *, FROM t, WHERE a = 1, then two spaces plus AND b = 2, then two spaces plus OR c = 3. The indentation makes it obvious that the AND and OR conditions belong to the same WHERE clause, and it makes the logic easier to check: with this layout, a reader can spot at once that the OR applies to the whole condition, which is a common source of logic bugs when parentheses are missing.
What This Formatter Does Not Do
This is a layout tool, not a SQL validator. It will happily format invalid SQL: a query with a misspelled keyword, a missing comma, or unbalanced parentheses comes out neatly formatted and still broken. Formatting makes errors easier to spot, because the structure is visible, but it does not check your SQL. Run the formatted query against your database to verify it, and use your database's error messages to find the real problems.
The formatter also does not handle comments specially. A comment containing a word like "where" may be uppercased and moved to a new line, because the formatter treats it as SQL text. If your query has comments you want to keep exactly as written, remove them before formatting and add them back afterwards. Similarly, the formatter does not split long SELECT lists across lines or align commas into columns: it keeps each clause on one line, which is the right level of formatting for most queries and keeps the output compact.
Finally, the formatter does not change the meaning of your SQL in any way. SQL keywords are case-insensitive in every major dialect, so uppercasing them is safe, and adding line breaks changes nothing about how the query executes. You can paste the formatted output straight back into your application, your migration file, or your database client, and it will run identically to the original.
Tips for Readable SQL
Format before you share. Paste formatted SQL into pull requests, documentation, and chat messages. Your reviewers will understand it faster, and you will get better feedback, because they can see the structure instead of decoding a wall of text.
Keep one style across a project. Whether you uppercase keywords or not matters less than doing it the same way everywhere. A formatter like this one enforces consistency for free, which is especially valuable when several people write SQL in the same codebase.
Use aliases, but keep them short. Short table aliases such as u for users and o for orders make join queries much more readable, as the worked example shows. Avoid single letters that collide, and avoid aliases that are longer than the table names they replace.
Break up very long queries. If a query is still hard to read after formatting, the problem is the query, not the layout. Consider a common table expression, a WITH clause, to split it into named steps. A query that reads as three clear steps is easier to debug than one giant SELECT, however well formatted.
Format generated SQL too. SQL produced by query builders and ORMs is often a single line with inconsistent case. Pasting it into a formatter before debugging it turns an unreadable string into a query you can actually reason about, and it often reveals the bug immediately.
Frequently Asked Questions
Does formatting SQL change how it runs?
No. SQL keywords are case-insensitive and whitespace is ignored, so uppercasing keywords and adding line breaks changes nothing about execution. The formatted query returns exactly the same results as the original.
Will the formatter change text inside quotes?
No. String values in single quotes pass through untouched, even if they contain words that look like keywords. A value like 'fromage' stays lowercase, and escaped quotes written as two single quotes are handled correctly.
What formatting rules does this tool apply?
Keywords are uppercased, each major clause (SELECT, FROM, WHERE, GROUP BY, ORDER BY, JOIN, LIMIT, and others) starts on a new line, AND and OR conditions are indented two spaces, and extra whitespace is collapsed.
Can it format INSERT, UPDATE, and DELETE statements?
Yes. INSERT INTO, VALUES, UPDATE, SET, and DELETE FROM are all recognized: for example, an UPDATE statement formats with SET on its own line. CREATE TABLE is handled the same way.
Does the formatter check whether my SQL is valid?
No, it only changes the layout. Invalid SQL comes out neatly formatted and still invalid. Formatting makes mistakes easier to spot, but you should always run the query against your database to verify it.