🗄️
Developer/SQL Formatter

SQL Formatter

Format and beautify SQL queries with syntax highlighting. Supports 6 dialects — Standard SQL, MySQL, PostgreSQL, T-SQL, PL/SQL, and SQLite.

100% LOCAL4 dialectsSyntax highlightCompress modeCopy result
Generic ANSI SQL
Input SQL
Formatted SQL
SELECT
  u.id,
  u.name,
  u.email,
  count(o.id) AS order_count,
  sum(o.total) AS total_spent
FROM
  users u
  LEFT JOIN orders o ON u.id = o.user_id
WHERE
  u.created_at >= '2026-01-01'
  AND u.status = 'active'
GROUP BY
  u.id,
  u.name,
  u.email
HAVING
  count(o.id) > 0
ORDER BY
  total_spent DESC
LIMIT
  10;
Query Stats
Characters286
Lines22
Keywords8
DialectStandard SQL
ModeFormatted
Data Source & Legal Disclaimer
Effective: 2026
Sources: Browser-side processing (no external API)

This tool runs 100% in your browser. All computation happens locally on your device — your input is never uploaded to any server. Results are for reference only.

See all data sources & update policy →

How SQL formatting works — illustrated

A formatter is a small compiler pipeline with two stages. The lexer reads the raw query and classifies each run of characters as a keyword, identifier, string literal, number, or operator — the same tokens a database parser would see. The printer then re-emits that token stream: it converts keywords to the chosen case (UPPERCASE, lowercase, or preserve), puts each major clause — SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY — on its own line, indents nested expressions by 2 spaces (or a tab), and always leaves string literals byte-for-byte intact. That last rule is what makes compress mode safe: newlines are stripped outside literals, so a multi-line string inside a query survives minification. Dialect support (MySQL backticks, T-SQL brackets, Postgres dollar quotes) tunes both stages.

The tokenizer-and-printer pipeline
SQL FORMATTING PIPELINEselect u.id,u.name from users u where u.age>18LEXER — TOKEN STREAMselect u.id, u.name from users u where u.age > 18PRINTER: keyword case · 2-space indent · one clause per line · literals preservedSELECT u.id, u.nameFROM users uWHERE u.age > 18

Formatting never changes what the query means — it only changes how the token stream is laid out.

Worked example

Ken maintains an analytics dashboard and pastes a minified SQL query his colleague copied straight out of the database console.

  1. Paste:He drops in "select u.id,u.name,count(o.id) as c from users u left join orders o on u.id=o.user_id where u.created_at>='2026-01-01' group by u.id order by c desc limit 10;" as one cramped line.
  2. Dialect:He selects PostgreSQL (dollar quotes, ARRAY, RETURNING) and sets keyword case to UPPERCASE with a 2-space indent.
  3. Format:The formatter reflows it — SELECT, FROM, LEFT JOIN, WHERE, GROUP BY, ORDER BY each on their own line, with the string literal '2026-01-01' left byte-for-byte intact.
  4. Verify:The query stats show lines jump from 1 to 7; he copies the formatted version back into the query editor and it runs with identical results.
↩ Back to calculator

To format SQL: paste your query — the tool adds proper indentation, capitalizes keywords, and aligns clauses for readability. Supports MySQL, PostgreSQL, SQLite, and SQL Server.

FreeToolHub SQL Formatter is a free browser-based tool that beautifies and validates SQL queries, no signup, no upload.

About this tool

What is this tool?

Format and beautify SQL queries with syntax highlighting. MySQL, PostgreSQL, T-SQL. Compress mode for production. Free, no signup.

4 dialectsSyntax highlightCompress modeCopy result

What Is the SQL Formatter?

This formatter beautifies SQL queries: it parses the statement, applies consistent indentation and casing to keywords, breaks long clauses onto readable lines, and adds syntax highlighting so structure jumps out. It handles standard SQL plus the dialects that matter daily — MySQL, PostgreSQL, and T-SQL — and includes a compress mode that strips whitespace for shipping queries through APIs or embedding them in code where size counts. Formatting runs entirely in your browser; queries never touch a server, which matters for anything touching production data.

Who Should Use This Tool?

Analysts inheriting thousand-character single-line queries pasted from logs or BI tools who need them readable before editing. Developers reviewing queries in code review where formatting noise drowns out the logic changes. DBAs documenting saved queries and views with consistent style. Data engineers moving queries between dialects who want dialect-appropriate keyword handling. Anyone pasting SQL into tickets, docs, or Slack where a wall of text loses the reader.

How Does It Work?

(1) Paste your query — single statement or a batch. (2) Pick the dialect: standard SQL, MySQL, PostgreSQL, or T-SQL, so keywords and functions format per that flavor. (3) The formatter indents nested subqueries and JOIN clauses onto their own lines, uppercases keywords, and aligns column lists; adjust indent width if your team's style differs. (4) Use compress mode to minify instead when you need the query compact. (5) Copy the result; formatting is semantics-preserving, so the output runs identically.

Why Does SQL Formatting Matter?

Unformatted SQL is where bugs hide. A WHERE clause jammed onto one line makes a missing AND invisible; aligned ON conditions make wrong join keys obvious at a glance; consistent keyword casing lets the eye separate structure from data instantly. The conventions that have converged across teams: keywords uppercase and on their own lines for major clauses (SELECT, FROM, WHERE, GROUP BY, ORDER BY); one column per line in SELECT lists for long queries, with a trailing comma convention your team picks once; JOINs with their ON condition indented under the join; subqueries indented one level and given a blank line above. Beyond readability, formatting is a collaboration contract: code review on consistently formatted queries reviews logic instead of arguing whitespace, and diffs on formatted queries show actual changes rather than reflow noise. The dialect choice matters more than people expect — T-SQL and PostgreSQL treat certain functions and identifiers differently — so format with the dialect you run, and when shipping queries through APIs or embedding them in application code, compress mode strips the whitespace back out.

Frequently Asked Questions

Which SQL dialects does it support?

Standard SQL (ANSI), MySQL, PostgreSQL, and T-SQL (SQL Server). It handles complex queries including JOINs, subqueries, CTEs (WITH clauses), window functions, and UNION/INTERSECT/EXCEPT. The formatter applies consistent indentation, keyword capitalization (SELECT/FROM/WHERE), and aligns column lists for readability. A compression mode minifies whitespace for production queries.

Does it preserve comments and string literals?

Yes. The parser tokenizes SQL to distinguish string literals (single quotes), line comments (--), block comments (/* */), and identifiers from keywords. Comments and string contents are preserved exactly as-is. This prevents the formatter from breaking inline SQL hints or altering string values during formatting.

Related tools

Joke of the Day
Sep 6

What do you call a crab that plays baseball?

100% Free, Forever

Keep Tools Free for Everyone

No paywalls, no signups, no data sold. Built by a solo developer who believes useful tools should be accessible to everyone.

Support me on Ko-fi— keep tools free

100% of proceeds go towards hosting & building more free tools.