Why format SQL?
SQL is often written as a single long string -- pulled from an ORM log, copied out of a monitoring dashboard, or generated by a migration tool. The query works, but reading it is painful. Formatting exists to solve five concrete problems:
- Readability. A formatted query makes the structure visible at a glance. You can see which tables are joined, where the filters are, and how subqueries nest without mentally parsing parentheses.
- Debugging. When a query returns wrong results, the first step is understanding what it actually does. Formatting separates clauses so you can isolate the problem -- a missing
WHEREcondition, a wrongJOINtype, or an aggregate applied to the wrong column. - Code review. Reviewers can spot logic errors faster when each clause starts on its own line. Diffs become meaningful too: a one-line change in a formatted query shows exactly which clause was modified, whereas a one-line change to a single-line query is unreadable.
- Documentation. Well-formatted queries in runbooks, wiki pages, and incident reports communicate intent without requiring the reader to reformat them mentally.
- Collaboration. Teams that enforce a consistent SQL style spend less time arguing about formatting in pull requests and more time discussing logic.
SQL dialects and formatting differences
SQL is standardized by ANSI/ISO, but every major database engine extends the language with its own syntax. A formatter needs to know which dialect you're targeting so it doesn't break vendor-specific constructs:
- MySQL / MariaDB -- uses backtick quoting for identifiers (
`table_name`), supportsLIMITwithoutOFFSETusing comma syntax, and has non-standard keywords likeSTRAIGHT_JOINandON DUPLICATE KEY UPDATE. - PostgreSQL -- uses double-quote identifiers, has
::type casting, dollar-quoted strings ($$body$$), and rich JSON operators like->>and@>. - SQLite -- mostly ANSI-compatible but supports both backtick and double-quote identifiers. No native
RIGHT JOINorFULL OUTER JOINuntil recent versions, and usesAUTOINCREMENTinstead ofAUTO_INCREMENT. - T-SQL (SQL Server) -- uses square-bracket identifiers (
[column]), hasTOPinstead ofLIMIT, and supports constructs likeMERGE,CROSS APPLY, andOUTPUTclauses. - PL/SQL (Oracle) -- adds procedural blocks (
BEGIN...END), usesROWNUMorFETCH FIRSTfor row limiting, and has its own string concatenation operator (||).
This tool supports multiple dialects via the sql-formatter library. Select your dialect from the dropdown to get correct keyword recognition and quoting behavior.
Common SQL formatting conventions
There is no single official SQL style guide, but several conventions have become near-universal in professional codebases. Most formatters, including this one, default to these rules:
- Uppercase keywords. Writing
SELECT,FROM,WHERE, andJOINin uppercase visually separates SQL syntax from table and column names. It is the most widely adopted convention, though some teams prefer lowercase for readability in long procedural blocks. - One clause per line. Each major clause (
SELECT,FROM,WHERE,GROUP BY,ORDER BY) starts on its own line. Column lists and conditions are indented beneath the clause keyword. - Consistent indentation. Two or four spaces per nesting level. Tabs work too, but spaces are more predictable across tools. Subqueries and
CASEexpressions are indented one level deeper than their parent clause. - Comma position. Leading commas (comma at the start of each line) make it easy to comment out a column without breaking the query. Trailing commas (comma at the end) are more common in application code. Both are valid -- pick one and stick with it across the project.
- Alias alignment. Aligning
ASaliases in a column list creates a visual table that makes the output schema obvious at a glance.
This formatter applies these conventions automatically. You can adjust indentation width via the tab-size control, and the dialect selector ensures keywords specific to your database are recognized and uppercased correctly.
When not to format
Formatting is not always the right move. There are legitimate cases where compact or unformatted SQL is preferable:
- Wire efficiency. If you're sending SQL strings over a network (some REST or GraphQL APIs accept raw queries), stripping whitespace reduces payload size. The difference is marginal for typical queries, but it adds up in high-throughput batch systems.
- ORM-generated queries. Most ORMs (Sequelize, SQLAlchemy, ActiveRecord, Prisma) generate SQL that humans rarely need to read directly. Formatting the output for debugging is useful, but reformatting it back into the ORM layer is pointless -- the ORM will regenerate it on the next run.
- Stored procedures under version control. If your team already has a linter or formatter integrated into the CI pipeline (like
sqlflufforpg_format), using a separate web formatter introduces style drift. Use the same tool everywhere. - Dynamic SQL in application code. Queries built with string concatenation or template literals in Python, JavaScript, or Java are often easier to read inline if they're short. Formatting a two-line
SELECTinto six lines can hurt readability rather than help it.
Related tools on CodeBoxTools
If you're formatting SQL, you're probably working with other code and data formats too. These tools follow the same client-side, privacy-first approach:
- JSON Formatter -- format, validate, and minify JSON payloads. Useful when your SQL query results come back as JSON from an API.
- CSS Minifier -- strip whitespace and comments from CSS for production builds.
- JavaScript Minifier -- minify JS with Terser, the same engine used by webpack and Vite.
- HTML Beautifier -- reindent and clean up HTML markup, handy when working on database-driven templates.