Skip to main content
L
Loopaloo
Buy Us a Coffee
All ToolsImage ProcessingAudio ProcessingVideo ProcessingDocument & TextPDF ToolsCSV & Data AnalysisConverters & EncodersWeb ToolsMath & ScienceGames
Guides & BlogAboutContact
Buy Us a Coffee
L
Loopaloo

Free online tools for developers, designers, and content creators. Your files are processed in your browser and are never uploaded - no accounts required. A few network utilities (like What's My IP and Currency Converter) call public APIs to do their job and say so on their pages.

support@loopaloo.com

Tool Categories

  • Image Tools
  • Audio Tools
  • Video Tools
  • Document & Text
  • PDF Tools
  • CSV & Data
  • Converters
  • Web Tools
  • Math & Science
  • Games

Company

  • About Us
  • Contact
  • Blog
  • FAQ

Legal

  • Privacy Policy
  • Terms of Service
  • Disclaimer

Support

Buy Us a Coffee

© 2026 Loopaloo. All rights reserved. Built with privacy in mind.

Privacy|Terms|Disclaimer
  1. Home
  2. Document & Text
  3. SQL Formatter
Add to favorites

SQL Formatter

Format SQL from a real tokenizer, so a keyword inside a string literal stays a value, with five dialects whose string and identifier rules differ, comments kept, and the output checked by running it in sqlite

Text processed locallyMore document & textJump to full guide

Related reading

  • SQL Formatting and Best Practices: Writing Readable, Maintainable Queries10 min read

Initializing in your browser…

You might also like

XML Formatter & Tools

Format and validate XML without changing the document: CDATA, xml:space="preserve" and mixed content all survive, with errors carrying a line, a caret and a next step, plus XPath queries, a tree view and JSON conversion that names what JSON cannot carry

YAML Formatter & Converter

Format YAML without rewriting it: comments, anchors, quoting, block scalars, multi-document streams and the exact spelling of every number all survive, with YAML 1.1 and 1.2 both offered because the same file means different things under each

Code Minifier & Beautifier

Minify or beautify JavaScript, TypeScript, CSS, HTML, JSON, XML and SQL with a real lexer per language, so a comment inside a string is not deleted and a line break the code depends on is kept, with the byte saving measured

About the SQL Formatter

Format a SQL statement into clauses and indentation, with the one rule that matters: the statement has to still do the same thing afterwards. That is measured rather than assumed. Every statement in the test corpus is executed by `sqlite3` before and after formatting and the rows are compared; the version this replaces kept the result on 13 of 18, and this one keeps it on all 18. Everything runs over a token stream, so a string literal is a token and nothing can reach inside one. The version this replaces ran over the raw text, and that is not a difference of style.

How it works

Measured on 2026-09-03, here is what the previous formatter did to ordinary SQL.

**It rewrote the data.** Keyword casing was one case-insensitive word-boundary replacement per keyword across the whole statement, so `SELECT note FROM t WHERE note = 'select from the menu'` came back with the note reading `SELECT FROM the menu`. The note in the database now says something else. A regular expression cannot tell a keyword from the same letters inside a quoted string; only a tokenizer can.

**It broke a value that spanned lines.** Collapsing every run of whitespace to one space turns a newline inside a string literal into a space, and the value changes. Breaking a line after every comma broke the one inside `'a, b'` too.

**It renamed columns.** `SELECT 1 AS "select"` came back with the quoted identifier rewritten, and a column really can be called select: quoting it is exactly how you say so.

**It edited comments.** A word inside `-- what this query is for` was upper-cased along with everything else.

**And it produced SQL that does not parse.** The rule that put `ON` on its own line had no word boundary, so `UNION ALL` became `UNI` followed by `ON ALL`, and sqlite refused the statement outright. Two of the eighteen corpus statements came back unrunnable.

This version tokenizes with `src/lib/text/sqlLex.ts`, whose per-dialect rules come from the same module the CSV tools use, already validated against real sqlite. **The dialect is a real setting now**, and it has to be, because it decides where a string ends: only MySQL treats a backslash inside a string literal as an escape; the identifier delimiter is a backtick in MySQL, a double quote in PostgreSQL, SQLite and Oracle and a square bracket in SQL Server; and PostgreSQL alone nests block comments and has dollar quoting. The dialect used to be stored in the URL with no control on the page and no effect on the output.

The syntax highlighting comes from the same tokens and is rendered as elements rather than as an HTML string, so a keyword inside a string is coloured as part of the string. The keyword COUNT is counted from the tokens too, so the words inside `'select from the menu'` do not inflate it. Identifier casing is offered but defaults to leaving it alone, and the page says why: PostgreSQL folds an unquoted identifier to lower case and Oracle to upper, so changing it is safe there and is not safe in a dialect that preserves it.

How to use

  1. 1Paste your SQL, or load one of the seven samples
  2. 2Choose the dialect, which decides where a string literal ends and what quotes an identifier
  3. 3Set the keyword case, the indent, the comma position and whether clauses get a blank line between them
  4. 4Press Format SQL, or Minify to put it back on one line
  5. 5Copy or download the result

Key features

  • A real tokenizer per dialect, so a keyword inside a string literal is left alone
  • Scored by executing the output: 18 of 18 corpus statements return the same rows, against 13 before
  • Five dialects whose string, identifier and comment rules genuinely differ
  • Comments kept and never re-cased; quoted identifiers never rewritten
  • A script split on the semicolons that really are statement terminators, not on the character
  • Clause layout with joins, conditions and subqueries indented, and brackets that line up
  • Syntax highlighting from the same tokens, rendered as elements rather than as an HTML string
  • Minification that keeps every string exactly as written

When to reach for this

  • Reading a query somebody else wrote

    Paste a one-line query and get clauses, indentation and aligned brackets, with the confidence that the query still returns the same rows.

  • Tidying a query before committing it

    Apply one house style to keyword case, indentation and comma position, with the comments that explain the query left intact.

  • Checking what a dialect does to a string

    Switch the dialect to see where a backslash or a bracket changes what counts as a string or an identifier, which is where a formatter usually goes wrong.

Frequently asked questions

Will formatting change what my query returns?

It must not, and here that is measured rather than promised: every statement in the test corpus is executed by sqlite3 before and after formatting and the rows are compared. All 18 come back identical. The version this replaces managed 13, and two of the five it got wrong did not parse at all.

Why does it ask for a dialect?

Because the dialect decides where a string literal ends. Only MySQL treats a backslash inside one as an escape; the identifier delimiter is a backtick in MySQL, a double quote in PostgreSQL, SQLite and Oracle and a square bracket in SQL Server; and PostgreSQL alone nests block comments and has dollar quoting. Get that wrong and a formatter can cut a string in half.

What happens to my comments?

They are kept, on their own lines, and never re-cased. A comment is the only part of a query that says why it exists.

Why is my column called "select" left alone?

Because it is quoted, and quoting is how you say a name is exactly those characters. Rewriting a quoted identifier renames the column.

Can it change the case of my table and column names?

It can, and it is off by default with the reason stated. PostgreSQL folds an unquoted identifier to lower case and Oracle to upper, so changing it there is harmless; MySQL, SQLite and SQL Server preserve it, so changing it there can change which column a case-sensitive server sees. The page says which applies to the dialect you picked.

Does it validate my SQL?

No. It formats what you give it and does not connect to a database or check that the query is valid. What it guarantees is that it does not make a valid query invalid, which is the property the corpus measures.

Related tools and how they differ

  • JSON Formatter & Validator: Formats, queries, diffs, and schema-validates JSON data; use it for API payloads and config objects rather than SQL statements.
  • XML Formatter & Tools: Formats and validates XML and converts it to JSON; use it for markup documents like RSS, SVG, or config XML, not query text.
  • YAML Formatter & Converter: Converts and validates YAML and JSON config with auto-fix for common YAML mistakes; use it for settings files, not database queries.

Further reading

  • SQL Formatting and Best Practices: Writing Readable, Maintainable Queries10 min read

Private by design

Your text is processed locally in the browser. Nothing you paste or open is transmitted or logged.