About SQL Formatter
SQL produced by an ORM or grabbed from logs is often crammed onto one long line, with keywords, columns, and subqueries all jammed together and hard to read. This tool formats SQL locally in your browser — wrapping, indenting, and aligning keywords so SELECT, JOIN, and WHERE structures stand out for review and tuning. For instance, SELECT id,name FROM users WHERE age>18 is re-indented across lines with SELECT, FROM, WHERE highlighted, making the query structure obvious.
The first thing to protect during formatting is strings and comments, whose contents follow no syntax rules. A string may contain fragments identical to keywords or operators, and folding them into layout logic produces wrong results, with comments just as liable to shift meaning when moved or merged. Identify those regions, preserve them verbatim, and adjust indentation only outside.
Keyword case and function name case are usually insensitive in execution yet differ when reading and searching. Uppercasing keywords while leaving identifiers as written is a common convention that separates grammar from custom names at a glance, while forcing function names to uppercase may clash with some database conventions. Confirm the target database practice rather than a generic rule.
Dialect differences mean one grammar cannot drive all formatting. Databases differ in identifier quoting, string concatenation, pagination syntax and auto-increment definitions, and a tool unaware of the dialect may treat valid syntax as broken or fail outright on vendor-specific constructs. Use an implementation that supports the dialect at hand.
Subqueries and joins show formatting at its best and are most easily made unreadable. Indenting subqueries to their own level, giving each join condition a line and listing selected columns one per line makes the logic visible, while piling it all on one line hides the relationship between conditions. The goal is to reveal structure, not minimise line count.
Long lists need particular care or readability suffers badly. With many values in a condition, one per line fills the screen and a single line defeats checking, so wrapping at a fixed count per line balances readability and compactness. Do not reorder the elements, since their sequence sometimes carries business meaning.
Finally, formatting changes appearance and not results, yet that does not license arbitrary reordering. Meaning rests on structure and conditions, some databases let hint comments or statement order influence the plan, and moving such a comment can visibly change performance. Confirm hints stay in place and compare the plan on important statements.
How It Works Internally
Formatting re-lays out a statement by syntax: keywords uppercase, clauses on their own lines, indentation mirroring nesting. It changes layout only, never meaning, so it cannot substitute for reading the execution plan — the same statement with different indentation performs identically. Its value is making "which tables does this touch and what are the conditions" visible at a glance during review, especially with nested subqueries.
select a,b from t where id=1SELECT
a,
b
FROM t
WHERE id = 1Measured on This Page
| Fixed: literals flattened | The old code collapsed \s+ to a single space across the whole text, so two spaces inside a literal became one and a multi-line literal was joined into one line — semantics changed. Literals are now stashed as placeholders before whitespace collapsing |
|---|---|
| Fixed: keywords inside literals | The word from inside 'from the start' was upper-cased to FROM by the keyword rule, rewriting string content. Placeholders keep literals verbatim |
| Fixed: comments broken up | A comment like -- keep it was flattened, its keywords upper-cased and its body split by the AND/OR rule; comments and block comments are now protected too |
| Formatting preserved | Upper-cased keywords, line breaks before FROM/WHERE/ORDER BY and one column per line after SELECT all behave as before on the SQL skeleton |
A formatter's first rule is to leave semantics alone, and in SQL the 2 easiest places to break it are literals and comments: both may contain arbitrary whitespace and arbitrary keyword words, and an implementation that flattens the whole text and then rewrites keywords hits both traps at once. The original collapsed two spaces inside a literal into one and turned from into FROM inside a quoted string — changes that are hard to spot by eye yet can alter results. Stashing literals and comments as placeholders, formatting only the skeleton and restoring verbatim fixes it. Still a simplified formatter: no dialect features such as dollar quoting or bracket identifiers, and no JOIN reordering.
How to Use
- Open "SQL Formatter"
- Paste the SQL statement to tidy
- Keywords are uppercased and clauses split onto lines
- Confirm strings and comments stayed untouched
- Click "Copy" to take the formatted query
Use Cases
- Beautify log SQL — Format a one-line slow query from logs to see its JOIN and subquery structure.
- Review ORM output — Format ORM-generated SQL to confirm it produces the query you expected.
- Prep for tuning — Lay out a complex query clearly to spot redundant subqueries or missing index conditions.
- Unify team style — Normalize indentation across colleagues' varied SQL for easier code review.
- Teaching — Expand nested queries into layers to explain execution logic to newcomers.
- Format embedded SQL — Extract inline SQL from application code and format it to review the query logic before debugging.
- Align migration scripts — Reformat multiple SQL migration files consistently to maintain a uniform style across the codebase.
FAQ
Does it change the query result?
No. Formatting only adjusts whitespace, line breaks, and indentation; keywords, columns, and logic are untouched, so semantics and results stay identical.
Which SQL dialects are supported?
It formats standard SQL and common dialects like MySQL and PostgreSQL well. A few vendor-specific syntaxes may not be fully recognized but usually still format acceptably.
Are keywords uppercased?
It typically normalizes keywords to uppercase for readability, while column names and values keep their original case.
Can it minify to one line?
This tool focuses on pretty-printing. To collapse multi-line SQL for embedding, remove the line breaks manually or use a compact mode where available.
Are comments preserved?
Single-line (--) and multi-line (/* */) comments are generally kept and placed sensibly, though comments in odd positions may be re-wrapped — verify quickly after formatting.
Will a very long SQL statement make it lag?
This tool runs locally in your browser and only rewraps whitespace and indentation at the token level — it never executes a query or parses the data — so SQL reaching thousands or tens of thousands of lines still formats smoothly. A statement with an extreme number of deeply nested subqueries may show a brief pause during the first highlight pass; usually a single re-render resolves it. If it feels stuck, split the statement into chunks, or strip non-SQL noise from the log line first, rather than trying to format one giant statement all at once.
If I paste production SQL, is it uploaded anywhere?
No. Formatting and syntax highlighting happen entirely in your browser; the SQL text is never sent to a server and is not logged or used for training, so you can safely paste slow-query logs or statements containing sensitive column names. For intranet or highly regulated environments, still follow your company's policy — e.g., redact schema and table names — since local processing only keeps the data from leaving your browser and can't protect you from other network-layer risks.
SQL keywords are not uppercased after formatting?
Indentation and line breaks are formatting; keyword casing is a separate style option and is off by default because rewriting case can touch string literals and aliases. Enable keyword uppercasing in the options and verify string constants were not altered.