SQL Formatter Online Free

💻 Developer Tools ✔ Free Forever

SQL Formatter Online Free

Paste any SQL query and get clean, readable, properly indented SQL instantly. Supports MySQL, PostgreSQL, SQL Server, Oracle, and SQLite. Runs entirely in your browser.

✔ MySQL ✔ PostgreSQL ✔ SQL Server ✔ Oracle ✔ Minify support ✔ 100% browser-based
Dialect
Indent
Input SQL
Formatted SQL
Choose a sample
About This Tool

A hand-built SQL tokenizer that reformats, minifies, and case-normalizes your query

This formatter does not send your query to a server or a database driver to interpret. It runs a small character by character tokenizer written directly in JavaScript, splitting the raw SQL string into typed tokens, comments, strings, identifiers, numbers, punctuation, operators, and words, before reassembling them with consistent indentation. Nothing leaves your browser.

The tokenizer’s rules, in order

The scanner walks the input one character at a time and checks a fixed sequence of conditions at each position, always taking the first one that matches.

Comments first A double hyphen starts a line comment that runs to the next newline. A slash-star starts a block comment that runs until it finds a matching star-slash, so keywords inside a comment are never treated as SQL.
Quoted strings and identifiers Single quotes mark a string literal, with a doubled single quote inside recognized as an escaped quote rather than a terminator. Double quotes, backticks, and square brackets each mark a quoted identifier, matching Postgres, MySQL, and SQL Server conventions respectively.
Numbers, punctuation, operators A leading digit, or a dot followed by a digit, starts a number token that continues through digits, a decimal point, and exponent notation. Parens, commas, and semicolons are single-character punctuation tokens. Two-character operators like >=, <=, <>, and :: are checked before falling back to single-character operators.
Everything else is a word Whatever remains, keywords, table names, column names, function names, is collected until the next whitespace or punctuation character and tagged as a generic word token.
// clause detection during reconstruction var CLAUSE_KEYWORDS = [‘SELECT’,’FROM’,’WHERE’,’JOIN’,’LEFT JOIN’,’RIGHT JOIN’, ‘INNER JOIN’,’FULL JOIN’,’CROSS JOIN’,’OUTER JOIN’,’GROUP BY’,’ORDER BY’, ‘HAVING’,’LIMIT’,’OFFSET’,’UNION’,’UNION ALL’,’INSERT INTO’,’VALUES’, ‘UPDATE’,’SET’,’DELETE’,’WITH’,’ON’]; // two-word clauses are checked before single-word ones if (isTwoWordClause) { result += ‘\n’ + pad(depth > 0 ? depth – 1 : 0) + twoWord; ti += 2; }

Depth tracking and comma placement

An opening parenthesis increments a depth counter and a closing one decrements it, and every clause keyword and comma is indented by that current depth multiplied by your chosen indent string. This is what lets a subquery inside a WHERE clause end up indented one level deeper than the query around it, without the formatter needing to understand what the subquery actually does.

SettingBehavior
Indent widthTwo spaces, four spaces, or a tab character, selectable and applied per nesting depth.
Comma placementTrailing comma (comma then newline, the default) or leading comma (newline then comma-space), a stylistic preference some teams standardize on for cleaner diffs.
Newline before clauseWhen enabled, every clause keyword like FROM or WHERE starts on its own line. When disabled, the reconstruction still tokenizes and re-cases but keeps clauses inline.
Uppercase / lowercaseRewrites every token whose uppercased form appears in a 70+ entry keyword list, covering clause words, joins, functions like COUNT and COALESCE, and constraint keywords, while leaving identifiers alone.
Minify is a separate, much simpler code path. The Minify button does not use the tokenizer at all. It strips block and line comments with two regex passes, then collapses all whitespace runs to a single space. This makes it fast but also means it has no awareness of string literals, so a comment-like sequence of characters sitting inside a quoted string could theoretically be affected. For formatting, the full tokenizer path is always used and does not have this limitation, since strings are captured as whole tokens.

Highlighting and stats

Syntax highlighting

A second, lighter pass colors keywords, function names, string literals, numbers, comments, and comparison operators for the highlighted view, using the same tokenization ideas but rendering spans instead of reformatted text.

Clause and table counts

The stats row counts clause keyword occurrences and counts identifiers following FROM or JOIN as a rough table count, both computed with simple regex matching over the formatted output.

70+ recognized keywords and functions Nested parenthesis depth tracking Postgres, MySQL, and SQL Server quoting styles Comma-first and trailing-comma modes

Style guides and formatters

  • ISO/IEC 9075 (SQL standard) is the base specification that most of these clause keywords trace back to, though every database vendor extends it differently.
  • sql-formatter is the widely used npm library if you need dialect-aware formatting (Postgres, BigQuery, Spark, and more) with a real parser rather than a keyword tokenizer.
  • SQL Style Guide by Simon Holywell documents the leading-comma and clause-per-line conventions this tool lets you toggle between.
  • MySQL identifier quoting rules explain the backtick convention the tokenizer recognizes.

When formatting saves a code review

Cleaning up a query copied from a monitoring dashboard or slow query log before analyzing it, standardizing formatting across a team’s saved queries, uppercasing keywords in a script written by someone who typed everything lowercase, minifying a query before embedding it as a string literal in application code, and getting a fast visual read on how many joins and tables a query touches before you run it against production.

Common Questions

FAQ: SQL Formatter Online Free

Yes, completely free. No account, no signup, and no payment required.

No. All formatting happens in your browser with JavaScript. Your queries never leave your device. Safe to use with production queries and sensitive data.

The formatter works with Standard SQL, MySQL, PostgreSQL, SQL Server, and Oracle. Syntax for common functions, joins, window functions, and CTEs is recognized across all dialects.

Yes. The formatter handles nested subqueries, common table expressions, and window functions with proper indentation at each nesting level.

Minify removes all whitespace, line breaks, and comments to produce the most compact version of your SQL. Useful when embedding SQL in application code or configuration files.

In comma first mode, each new item in a SELECT list starts with a comma at the beginning of the line instead of the end of the previous line. Some teams prefer this style because it makes it easier to comment out individual columns during debugging.

Yes. Click UPPERCASE Keywords to convert all SQL keywords to uppercase, or lowercase keywords to convert them all to lowercase. The output is formatted at the same time.

Privacy Overview

Cookies let this site remember your preferences and show us which tools people actually use. Full detail sits in our Privacy Policy.