🗄️ SQL Formatter
By ToolNimba Web Dev Team · Updated 2026-06-19
Paste SQL above, then choose Format or Minify.
Paste a cramped one-line query and this SQL formatter turns it into clean, readable code. It uppercases the reserved keywords, puts the major clauses (SELECT, FROM, WHERE, JOIN, GROUP BY, ORDER BY, HAVING, LIMIT) on their own lines, indents subqueries, and can break the SELECT column list so each column sits on its own line. There is also a one-click Minify option that collapses everything back to a single tidy line. Everything runs in your browser, so your query never leaves the page.
What is the SQL Formatter?
SQL written in a single long line is hard to scan, hard to review, and hard to debug. A formatter (or beautifier) applies consistent rules so that the structure of a query is visible at a glance: clauses line up, keywords stand out, and nested logic is indented. This makes it far quicker to spot a missing JOIN condition, a stray comma, or a WHERE clause that was meant to be a HAVING. Formatting does not change what a query does, it only changes how the text is laid out, so the result is byte-for-byte equivalent in behaviour.
This tool is a pragmatic token formatter rather than a full SQL parser. It scans the text into tokens (strings, comments, words, and symbols), leaves anything inside single quotes, double quotes, backticks, or comments untouched, and then re-cases the words it recognises as reserved keywords. Major clause keywords trigger a new line, opening parentheses increase the indent level so subqueries step inward, and commas in a list can start a new line so each selected column is easy to read. Because it is rule-based and not a grammar-aware parser, it handles the vast majority of everyday queries cleanly but will not validate your SQL or reformat exotic vendor-specific syntax perfectly.
The two main actions are Format and Minify. Format expands the query for reading and editing, which is what you want in source control, documentation, or a code review. Minify does the opposite: it collapses whitespace down to a single line, which is handy when you need to paste a query into a config file, a log line, a JSON string, or anywhere multi-line text is awkward. You can choose the indent width (2 spaces, 4 spaces, or a tab), pick uppercase or lowercase keywords, and toggle whether each column gets its own line.
When to use it
- Cleaning up a one-line query copied out of application code or an ORM log so you can read and review it.
- Standardising the SQL style across a team before committing migrations or queries to source control.
- Making a long JOIN-heavy query readable while you debug a wrong or missing result set.
- Minifying a formatted query to embed it in a JSON payload, a YAML config, or a single log line.
How to use the SQL Formatter
- Paste your SQL query into the input box.
- Choose the indent width (2 spaces, 4 spaces, or tab) and the keyword case (UPPERCASE or lowercase).
- Tick or untick "One column per line" depending on whether you want each SELECT column on its own line.
- Click Format to beautify, or Minify to collapse the query to a single line.
- Use Copy to grab the result, or Load sample to see an example.
Formula & method
Worked examples
Format a simple cramped query with the column list broken out.
- Input: select id, name, email from users where active = 1 order by name
- Keywords select, from, where, order by are uppercased and each starts a new line
- List clauses (SELECT and ORDER BY) put each comma-separated item on its own indented line
Result: SELECT id, name, email FROM users WHERE active = 1 ORDER BY name
Minify a formatted multi-line query back to one line.
- Input is a query spread over several indented lines
- All unquoted whitespace is collapsed to single spaces
- Spacing around commas and parentheses is tidied
Result: SELECT id, name FROM users WHERE name = 'O''Brien' AND active = 1
Major clause keywords that start a new line when formatting
| Keyword | Role in a query |
|---|---|
| SELECT | Lists the columns or expressions to return |
| FROM | Names the source table or subquery |
| JOIN (and INNER, LEFT, RIGHT, FULL) | Combines rows from another table |
| WHERE | Filters rows before grouping |
| GROUP BY | Aggregates rows into groups |
| HAVING | Filters groups after aggregation |
| ORDER BY | Sorts the final result set |
| LIMIT / OFFSET | Restricts how many rows are returned |
What the Format and Minify actions do
| Action | Effect |
|---|---|
| Format | Uppercases keywords, breaks clauses onto new lines, indents subqueries |
| Minify | Collapses the query to a single line with tidy spacing |
| One column per line | Puts each SELECT column on its own indented line |
| Keyword case | Switches recognised keywords to UPPERCASE or lowercase |
Common mistakes to avoid
- Expecting it to validate or fix your SQL. This is a formatter, not a parser or linter. It re-lays out the text you give it but does not check that the query is valid, so a syntax error in your input will still be a syntax error in the output.
- Assuming keywords inside strings get changed. Text inside quotes, backticks, or comments is left exactly as you typed it. A value like 'select one' stays lowercase, because the formatter never touches the contents of a string literal.
- Treating the column-per-line break as SELECT only. The "one column per line" option breaks on commas at the current nesting level, so commas in a GROUP BY or a function argument list may also wrap. Untick it if you only want a compact single-line clause.
- Relying on it for one specific dialect. The keyword list covers common ANSI SQL plus widely used JOIN and set operators. Vendor-specific keywords (window functions, JSON operators, and the like) are left as written rather than re-cased.
Glossary
- Formatter (beautifier)
- A tool that re-lays out code with consistent indentation and casing without changing what it does.
- Minify
- To collapse code to the smallest readable form, here a single line with normalised spacing.
- Clause
- A major section of a SQL statement such as SELECT, FROM, WHERE, or ORDER BY.
- Keyword
- A reserved word with special meaning in SQL, like SELECT, JOIN, or GROUP BY, that this tool re-cases.
- Token
- A single unit the formatter reads: a word, symbol, string literal, or comment.
- Subquery
- A query nested inside another, usually wrapped in parentheses, which this tool indents one level deeper.
Frequently asked questions
Does this SQL formatter send my query anywhere?
No. The whole tool runs as JavaScript in your browser. Your query is never uploaded, logged, or sent over the network, which makes it safe to use even for queries that mention internal table or column names.
Will formatting change what my query does?
No. Formatting only changes whitespace and the letter case of recognised keywords. SQL keywords are case-insensitive and whitespace between tokens does not affect meaning, so the reformatted query behaves identically to the original.
Which SQL dialects does it support?
It handles common ANSI SQL used by MySQL, PostgreSQL, SQLite, SQL Server, and others, including the usual JOIN types and set operators like UNION. It is a pragmatic token formatter, not a dialect-aware parser, so unusual vendor-specific syntax is laid out but not specially recognised.
Why are some words not uppercased?
Only words in the built-in keyword list are re-cased. Table names, column names, aliases, and anything inside quotes or comments are treated as your data and left exactly as written, so they keep whatever case you used.
What does the Minify button do?
Minify collapses your query onto a single line, normalising the spacing around commas and parentheses. It is useful when you need to paste a query into a config file, a JSON string, or a single log line where multi-line text is awkward.
Can it format an INSERT or UPDATE, not just SELECT?
Yes. It recognises INSERT INTO, VALUES, UPDATE, SET, and DELETE FROM as clause keywords too, so common data-modifying statements are reformatted along the same lines as a SELECT query.