SQL Formatter

Beautify and indent SQL queries for MySQL, PostgreSQL, and SQLite.

Data
Input SQL
Formatted SQL

Why format SQL?

Formatted SQL is dramatically easier to read, review, and debug than unformatted SQL. ORM-generated queries, minified API responses, and legacy code often contain SQL on a single line with inconsistent capitalisation and no indentation. Reformatting these queries reveals their logical structure — the JOINs, WHERE clauses, and subqueries — making it possible to understand what the query does without mentally parsing the syntax.

SQL formatting conventions include: capitalising keywords (SELECT, FROM, WHERE, JOIN, ON, AND); placing each major clause on its own line; indenting the column list under SELECT; and aligning ON conditions with their JOIN. These conventions are not enforced by any SQL engine, but following a consistent style reduces cognitive load during code reviews and audits.

Performance aside, readable SQL is also more secure: unformatted SQL in application code is harder to review for injection vulnerabilities. Proper formatting makes it easier to see whether parameterised queries are used consistently.

Common mistakes

  • Dialect differences — SQL has many dialects. MySQL uses backtick quoting (`column`), PostgreSQL uses double quotes ("column"), and SQL Server uses square brackets ([column]). Select the right dialect to avoid breaking formatted queries.
  • Comments getting stripped — Some formatters strip SQL comments. Inline comments (-- reason for JOIN) explain complex logic and should be preserved in production code.