SQL Formatter

Format and beautify messy SQL queries into clean, indented, readable code with keywords uppercased. No signup. Ideal for developers, analysts and anyone writing SQL.

Remove Ads
Remove Ads

Result

Formatted SQL
Remove Ads

Share on Social Media:

An SQL formatter beautifies messy SQL queries into clean, readable code — free, with no account. Paste a query and get it back with clauses on their own lines, keywords in uppercase and consistent indentation, ready to read, review or share. It runs entirely in your browser, so nothing is uploaded or stored. Note the exact scope: it is a simple text formatter, not a SQL parser — it uppercases 38 keywords, breaks lines before clauses, and splits at every comma. It does not indent subqueries, it does not validate your SQL, and words inside quoted strings can get uppercased too, so check string literals after formatting.

On this page

  1. How to Format SQL Step by Step
  2. Why SQL Formatting Matters
  3. Formatting Versus Minifying SQL
  4. Who Needs an SQL Formatter
  5. Pro Tips for Clean SQL
  6. Common SQL Formatting Mistakes to Avoid
  7. Related Tools You May Need Next
  8. Frequently Asked Questions

How to Format SQL Step by Step

  1. Paste your SQL into the input box. It can be a messy, unformatted query, a long complex statement, or minified SQL that is hard to read.
  2. Click Format SQL. The tool reformats the query, applying consistent indentation, line breaks and keyword casing to produce clean, structured SQL. It does not format as you type — you must click the button.
  3. Review the formatted query in the Result box. Major clauses sit on their own lines, AND/OR conditions are indented, and keywords are cased consistently, making it far easier to read.
  4. Check any quoted strings. Keywords inside single quotes are uppercased too, which can change a string's value — fix those back before running the query.
  5. Click Copy and place the clean SQL into your code, script, or database tool, where its readability makes it easier to understand, review and maintain. There is no download button — Copy is the only export.

Illustration of messy SQL code becoming clean, formatted code beside a database

Why SQL Formatting Matters

SQL, the language used to query and manage databases, is often written quickly and can become an unreadable tangle, especially for complex queries with many clauses, joins and conditions. A query crammed onto one line, or inconsistently indented, is hard to read, understand and debug. SQL formatting solves this by imposing a clean, consistent structure that makes the query's logic visible at a glance.

This formatter applies a fixed set of rules. It collapses scattered whitespace to single spaces, uppercases 38 common keywords — SELECT, FROM, WHERE, GROUP BY, ORDER BY, JOIN, AND, OR, COUNT and the rest — puts each major clause on its own line, indents AND/OR conditions by two spaces, and puts every comma-separated item on its own indented line. The full keyword list is in the table below.

Formatting changes layout, not meaning — with one caveat. Because this is a text formatter and not a SQL parser, it cannot tell a keyword inside a quoted string from a real keyword, so a string like '%select where%' comes out uppercased and split across lines, which does change that string's value. Check quoted text after formatting, and fix it back before running the query.

Readability matters greatly for SQL because queries are frequently read, reviewed and modified by people. A well formatted query can be understood quickly, its logic followed, and errors spotted, whereas a messy one hides its structure and invites mistakes. In team settings, consistent formatting also makes everyone's SQL easier to work with. Formatting is the opposite of minifying: formatting adds whitespace, line breaks and indentation to make SQL readable for humans, while minifying strips these to make it compact. Since SQL is written and maintained by people, and the database does not care about the layout, formatting for readability is almost always what you want when working with SQL.

Worked example — this input (the tool's own placeholder):

LineSQL input
1select id, name from users where age > 18 order by name

produces exactly this output:

LineFormatted SQL
1SELECT id,
2␣␣name
3FROM users
4WHERE age > 18
5ORDER BY name

Formatting Versus Minifying SQL

AspectFormattingMinifying
DoesAdds structure and indentationStrips whitespace
ResultReadable, clear SQLCompact SQL
ForHumans reading and editingRare size-sensitive cases
MeaningUnchanged — except quoted strings, which this formatter can alter (see warning above)Unchanged

What this formatter does, exactly:

RuleDetail
Keywords uppercased (38)SELECT, FROM, WHERE, GROUP, ORDER, BY, HAVING, LIMIT, INSERT, INTO, VALUES, UPDATE, SET, DELETE, INNER, LEFT, RIGHT, OUTER, JOIN, UNION, ON, AND, OR, AS, NULL, NOT, IN, LIKE, BETWEEN, IS, DISTINCT, COUNT, SUM, AVG, MIN, MAX, DESC, ASC
Keywords NOT uppercasedCASE, WHEN, THEN, ELSE, END, EXISTS, CREATE, TABLE and everything else — they stay exactly as typed
New line beforeSELECT, FROM, WHERE, GROUP BY, ORDER BY, HAVING, LIMIT, INSERT INTO, VALUES, UPDATE, SET, DELETE FROM, INNER/LEFT/RIGHT/OUTER JOIN, JOIN, UNION, ON
Indentation2 spaces — only on AND/OR lines and comma continuations. No nesting indentation for subqueries
CommasEvery comma starts a new indented line — including commas inside IN lists and function calls

A join example, so you can see the JOIN quirk before you meet it:

LineSQL input
1select u.id, u.name, count(o.id) from users u left join orders o on u.id = o.user_id where u.age > 18 and (o.total > 100 or o.total is null) group by u.id, u.name having count(o.id) > 5 order by u.name desc limit 10

becomes:

LineFormatted SQL
1SELECT u.id,
2␣␣u.name,
3␣␣COUNT(o.id)
4FROM users u
5LEFT
6JOIN orders o
7ON u.id = o.user_id
8WHERE u.age > 18
9␣␣AND (o.total > 100
10␣␣OR o.total IS NULL)
11GROUP BY u.id,
12␣␣u.name
13HAVING COUNT(o.id) > 5
14ORDER BY u.name DESC
15LIMIT 10

Note LEFT on one line and JOIN on the next — every multi-word JOIN splits this way — and ON on its own line. The query still runs the same; only its layout changed.

Who Needs an SQL Formatter

Developers writing queries. A developer formats their SQL to keep complex queries readable and maintainable, making the logic clear for themselves and others who read the code.

Data analysts. An analyst working with SQL formats their queries so the structure is visible, helping them build, understand and debug the analysis they are writing.

People reviewing SQL. Someone reviewing another person's query formats it to make the logic clear, so they can understand and check it more easily during review.

People debugging queries. A person troubleshooting a query formats it to see its structure clearly, which helps locate errors that a messy layout would hide.

Teams standardising SQL. A team formats their SQL consistently so everyone's queries follow the same clear style, making shared code easier to work with.

Laptop showing messy SQL code becoming clean, formatted code beside a database

Pro Tips for Clean SQL

Format complex queries for clarity. The more complex a query, with many joins, conditions and subqueries, the more formatting helps. Format complex SQL so its structure is visible and its logic followable at a glance.

Use consistent keyword casing. The formatter uppercases 38 keywords, which distinguishes them from table and column names. CASE, WHEN and THEN stay lowercase, so case those by hand if you want them uppercase too.

Check quoted strings after formatting. Keywords inside single quotes get uppercased and split across lines, which changes the string's value — a LIKE pattern like '%select where%' comes out as '% SELECT WHERE%'. Fix quoted text back before running the query.

Expect IN lists and function arguments to split. Every comma starts a new indented line, so IN (1, 2, 3) becomes one value per line and concat(a, b) splits across two lines. That is the formatter's only comma rule, applied everywhere.

Format before a code review or sharing. Formatting SQL before a code review or sharing it with others makes it easier for them to read and understand, smoothing collaboration and review.

Format to help debugging. When a query is not working, formatting it reveals its structure, making errors in the logic or clauses easier to spot than in a cramped, messy layout.

Remember it does not validate. A broken query formats silently — the tool only rearranges text and never tells you the SQL is invalid. Always test the formatted query in your database before relying on it.

Common SQL Formatting Mistakes to Avoid

Leaving complex queries unformatted. A complex query crammed together or inconsistently laid out is hard to read, understand and debug, inviting errors. Formatting complex SQL to reveal its structure is important for maintainability.

Assuming formatting can never change a query. It can change string-literal values: keywords inside quotes are uppercased and split across lines. Check quoted text after formatting and restore it before running the query.

Expecting subqueries to be indented. There is no nesting indentation at all — a subquery gets no extra indent relative to its parent. The only indentation is the 2 spaces on AND/OR lines and comma continuations.

Expecting JOIN to stay on one line. LEFT JOIN, INNER JOIN, RIGHT JOIN and OUTER JOIN all split across two lines (LEFT, then JOIN). The query still runs the same; only the layout is unusual.

Expecting CASE/WHEN to be uppercased. Only the 38 listed keywords are uppercased. CASE, WHEN, THEN, ELSE and END stay exactly as you typed them.

Expecting validation. Invalid SQL is "formatted" silently — the tool never warns you. A formatted query can still be broken, so test it in your database.

Minifying SQL you need to maintain. Minifying SQL to make it compact strips the readability that maintenance depends on. Since SQL is written and maintained by people and the database ignores layout, format for readability instead unless size genuinely matters.

Inconsistent formatting within a team. When team members format SQL differently or not at all, shared queries become a patchwork that is harder to read and maintain. Agreeing and applying a consistent style avoids this.

Formatting with an empty input box. With nothing pasted, clicking Format SQL shows an "Invalid Input" notice and leaves any previous result unchanged. Paste your SQL first.

Related Tools You May Need Next

JSON Formatter. Beautify, minify and validate JSON in one tool.

XML Formatter. Format XML into clean, readable code.

JSON Beautifier. Format and indent JSON into a clear, readable layout.

Line Counter. Count lines, words and characters in any text.

HTML to Markdown. Convert HTML into clean, readable Markdown.

3D illustration of SQL code converting to clean, formatted code

Frequently Asked Questions

How do I format SQL?

Paste your SQL query into the input box and click Format SQL. The tool reformats the query with consistent indentation, line breaks and keyword casing to produce clean, readable SQL: major clauses go on their own lines, AND/OR conditions are indented two spaces, and keywords are uppercased. Copy the formatted query into your code or database tool. This makes complex queries far easier to read, understand, review and debug.

Which keywords does it uppercase?

Exactly 38: SELECT, FROM, WHERE, GROUP, ORDER, BY, HAVING, LIMIT, INSERT, INTO, VALUES, UPDATE, SET, DELETE, INNER, LEFT, RIGHT, OUTER, JOIN, UNION, ON, AND, OR, AS, NULL, NOT, IN, LIKE, BETWEEN, IS, DISTINCT, COUNT, SUM, AVG, MIN, MAX, DESC and ASC. Everything else — including CASE, WHEN, THEN, ELSE, END, CREATE and TABLE — stays exactly as you typed it.

Does it indent subqueries?

No. There is no nesting indentation at all. The only indentation in the output is two spaces on AND/OR lines and on comma continuations. A subquery inside parentheses gets no extra indent relative to the outer query, and parentheses get no special handling.

Why did my LEFT JOIN split across two lines?

Because the formatter's JOIN rule runs after its multi-word JOIN rules and inserts a newline before the word JOIN itself. So LEFT JOIN, INNER JOIN, RIGHT JOIN and OUTER JOIN all come out as two lines — LEFT on one line, JOIN on the next — with ON on its own line after that. The query still runs the same; only the layout is unusual.

Why did words inside quotes change?

Because this is a text formatter, not a SQL parser: it cannot tell a keyword inside a quoted string from a real keyword. So a string like '%select where%' is uppercased and split across lines, and 'order' becomes 'ORDER' — which changes the string's value. Always check quoted text after formatting and restore it before running the query.

Why did my IN list split across lines?

Because every comma in the query starts a new indented line — there is no exception for commas inside IN lists or function calls. So IN (1, 2, 3) becomes one value per line, and concat(a, b) splits across two lines. That is the formatter's single comma rule, applied everywhere.

Does formatting change what my query does?

Almost never — but with one real exception. Formatting only rearranges whitespace and keyword casing, so the database executes the formatted and unformatted versions identically, except for keywords inside quoted strings, which get uppercased and split. That changes string values, so a LIKE pattern can match different rows after formatting. Check quoted text; everything else is safe.

Can it minify SQL?

No. This tool only formats — it adds whitespace, line breaks and indentation to make SQL readable. There is no minify mode, no compact output and no option to strip whitespace. Since SQL is written and maintained by people and the database ignores layout, formatting for readability is almost always what you want.

Which SQL dialects does it support?

None specifically — and all of them, in the same limited way. The tool does not parse SQL or know any dialect's grammar; it only applies text rules (uppercase 38 keywords, break lines, split commas) to whatever you paste. That makes it dialect-neutral for layout, but it also means it cannot validate dialect-specific syntax.

Is the SQL formatter free, and is my SQL uploaded anywhere?

Yes, it is completely free with no account and no usage limit. It runs entirely in your browser, so your SQL is processed locally and never uploaded or stored, and the clean, formatted query is ready to copy the moment you click Format SQL.