Skip to content

SQL Formatter

Beautify or minify SQL with clause indentation and consistent keyword casing.

Formatted locally — your queries never leave your browser.

How should SQL be formatted for readability?

Start each major clause — SELECT, FROM, WHERE, GROUP BY, ORDER BY — on its own line at the left margin, indent AND and OR conditions beneath their clause, and increase indentation inside brackets. Uppercase keywords against lowercase identifiers makes a query’s structure visible at a glance.

Understanding your result

Tokenising before formatting is what separates a reliable formatter from a naive find-and-replace. A query containing the literal string “select from where” must come through untouched, and a comment mentioning a keyword must not be re-cased — both fall out naturally once strings and comments are recognised as their own token types. On style, uppercase keywords against lowercase identifiers is the long-standing convention precisely because it lets you see the shape of a query at a glance, which matters most when you are scanning something written by someone else.

Formula and method

The query is tokenised into strings, comments, identifiers, numbers and punctuation. Only keyword tokens are re-cased, major clauses trigger a line break at the base indent, AND and OR indent one level, and brackets raise and lower the indent depth.

Assumptions and limitations

This is a formatter, not a parser or validator — it will happily lay out a query your database would reject, and it does not check table or column names. Dialect-specific syntax such as window function frames, vendor hints or procedural blocks may not be indented ideally. Formatting is cosmetic only: the query text is re-laid-out, never rewritten or optimised.

Worked example

A single-line SELECT with a LEFT JOIN, three WHERE conditions, a GROUP BY and an ORDER BY becomes about ten readable lines, with each clause starting at the left margin and the AND conditions indented beneath WHERE.

How to use this tool

  1. Paste your SQL.
  2. Choose beautify or minify.
  3. Pick uppercase or lowercase keywords.
  4. Copy the formatted query or download it as a .sql file.

Common mistakes to avoid

  • Expecting the formatter to validate the query — it does not.
  • Assuming formatting changes performance; it changes nothing but whitespace.
  • Minifying a query that contains a line comment in a context where it matters.
  • Pasting production SQL into tools that upload it — this one does not.

About the SQL Formatter

The SQL Formatter lays out queries so they can be read: each major clause starts a new line, AND and OR are indented under their condition, nested brackets increase the indent, and keywords are cased consistently. It can also minify a query back to a single line.

Who should use this tool

Developers, analysts and DBAs reviewing, debugging or documenting SQL.

Benefits

  • Tokenises properly, so keyword casing never alters text inside quotes.
  • Comments are preserved exactly as written.
  • Handles multi-word clauses such as GROUP BY and LEFT OUTER JOIN as units.
  • Queries stay in your browser — safe for production SQL with real table names.

Practical use cases

  • Making an unreadable one-line query reviewable.
  • Tidying SQL before committing it or pasting it into documentation.
  • Minifying a query to embed in code or a config file.
  • Normalising style across a team’s queries.

Explore all Developer Tools tools

Frequently asked questions

Does formatting change what my query does?

No. Only whitespace and keyword casing change. SQL keywords are case-insensitive, so the query executes identically.

Are string literals safe?

Yes. Strings are tokenised separately, so text inside quotes — including words that look like keywords — is never re-cased or reformatted.

Why uppercase keywords?

It is the long-standing convention. Contrasting uppercase keywords with lowercase table and column names makes a query’s structure visible at a glance.

Does it support all SQL dialects?

It handles standard SQL and the common clauses of MySQL, PostgreSQL, SQL Server and SQLite. Highly vendor-specific or procedural syntax may not indent ideally.

Is my query uploaded?

No. Formatting happens entirely in your browser, so production queries with real schema names stay on your machine.

Share this tool

Free to use — copy the link, share it anywhere, or add the tool to your own website.

Embed this tool on your site (free)

Copy this code and paste it into any web page — it stays free and always up to date: