Smart SQL Formatter
Format, beautify, analyze, and optimize SQL queries with syntax highlighting, query insights, and multi-database support.
How to use
Pick a dialect & style
Choose your database (MySQL, PostgreSQL, SQL Server, SQLite, Oracle or MariaDB) and a formatting style — Standard, Compact, Expanded, or Minified.
Paste or load SQL
Paste your messy query into the left editor, or click any sample chip — Simple SELECT, JOIN Query, Aggregation, Window Function, Subquery, CTE.
Tune the output
Adjust keyword case (UPPER/lower/preserve), indent width (2–8 spaces), and comma placement (trailing or leading). The output updates instantly.
Analyze, copy, download
Review the Query Analyzer (tables, JOINs, subqueries), the Health Score with actionable suggestions, and the plain-English explainer. Copy or download the .sql file.
When to use SQL formatting
Code reviews
Reviewing a 200-line query is brutal when it's a single line. Formatting transforms it into structured, scannable code that reviewers can actually read.
Debugging queries
When a query returns wrong results or runs slow, a well-formatted version makes it dramatically faster to spot bad joins, missing conditions, or misplaced operators.
Version control
Expanded mode (one column per line) produces tiny, surgical diffs in git when you add or modify a single column — instead of a single huge changed line.
Team consistency
A shared formatter eliminates SQL style debates. Whether your team prefers leading or trailing commas, the formatter enforces it across every developer's queries.
Embedding in code
Minified queries fit cleanly into application strings, configuration files, and API payloads. Use minification when shipping queries inside source code.
Learning SQL
The Plain-English Explainer translates real queries into descriptive sentences — perfect for learning SQL by reverse-engineering production queries.
How it works
SQL tokenizer
A pure-JS tokenizer splits your query into typed tokens — keywords, functions, strings, numbers, comments, operators — so formatting decisions never touch the inside of string literals or comments.
Structural formatter
The formatter walks tokens and emits line breaks based on SQL structure: top-level clauses on new lines, JOIN ON clauses indented under their JOIN, AND/OR vertically aligned, subqueries indented.
Query analyzer
After parsing, the analyzer counts JOINs, WHERE clauses, GROUP BY / ORDER BY, subqueries, and aggregation functions, then extracts the list of tables referenced by each FROM and JOIN.
Rule-based scorer
The Health Score applies eight rules (SELECT * use, missing LIMIT, leading-wildcard LIKE, implicit joins, deep subqueries…) and surfaces specific, actionable suggestions for each issue.
Related tools
Frequently asked questions
Quick answers about this free online tool.
