{ sniptools }
Data

SQL Formatter

Format and beautify SQL queries instantly

What is SQL?

SQL (Structured Query Language) is the standard language for managing and querying relational databases, used by virtually every application that stores structured data. From simple SELECT queries to complex multi-table JOINs with subqueries and window functions, SQL can express sophisticated data operations in a single statement. However, the language's flexibility means there is no enforced formatting — a valid query can be written as a single line or spread across dozens of lines with arbitrary indentation.

In practice, SQL formatting matters far more than in most languages because queries are often read by people who did not write them. A DBA reviewing a slow query, a developer debugging an ORM-generated statement, a data analyst reading a colleague's report query — all benefit from consistent, readable formatting. Yet most SQL arrives unformatted: application logs concatenate queries into single lines, ORM debug output uses minimal whitespace, and hand-written queries accumulate idiosyncratic style over time.

A SQL formatter applies consistent rules to any query: major clauses (SELECT, FROM, WHERE, JOIN, GROUP BY, ORDER BY) start on new lines, subqueries are indented, keywords are uppercased, and expressions are aligned. The result is a query that reads like structured prose — each clause clearly delineated, each condition visible, each join relationship obvious.

How it works

SQL formatting begins with tokenization: the input string is broken into a stream of tokens — keywords (SELECT, FROM, WHERE), identifiers (table and column names), operators (+, =, <>), literals (strings, numbers), and punctuation (commas, parentheses). The tokenizer must be dialect-aware because keywords differ between MySQL, PostgreSQL, SQLite, and SQL Server — for example, LIMIT is standard in MySQL but not T-SQL, which uses TOP instead.

The token stream is then processed by a formatting engine that maintains a state machine tracking the current clause context and nesting depth. When it encounters a major clause keyword like FROM or WHERE, it inserts a newline and resets indentation to the base level. When it encounters an opening parenthesis (indicating a subquery or expression group), it increases the nesting depth. Commas between column expressions trigger newlines within the SELECT clause to place each column on its own line.

Keyword casing is applied during the output phase: recognized SQL keywords are uppercased while identifiers and literals are left unchanged. The engine distinguishes between keywords and identifiers by checking against the dialect's keyword list, which is why selecting the correct dialect matters — a word that is a keyword in PostgreSQL might be a valid unquoted identifier in MySQL. The final output is a string with consistent indentation, casing, and line breaks that can be directly pasted into an editor or documentation.

How to use this tool

  1. Paste your SQL query into the input area. The tool accepts queries of any complexity — simple SELECTs, multi-table JOINs, subqueries, CTEs, and DDL statements.
  2. Select the appropriate SQL dialect (MySQL, PostgreSQL, SQLite, or SQL Server) to ensure correct keyword recognition and formatting rules.
  3. Click "Format" to apply consistent formatting with uppercase keywords, clause-per-line layout, and proper indentation of subqueries and nested expressions.
  4. Review the formatted output. Major clauses start on new lines, JOIN conditions are aligned, and nested subqueries are indented to show their scope clearly.
  5. Copy the formatted query for use in your SQL client, code review, documentation, or version control. The formatting changes only whitespace — the query logic is identical.

Examples

Formatting a dense ORM-generated query

ORMs often produce queries like "SELECT u.id,u.name,o.total FROM users u JOIN orders o ON u.id=o.user_id WHERE o.status='completed' AND o.total>100 ORDER BY o.total DESC". The formatter breaks this into clearly delineated clauses with each JOIN, WHERE condition, and ORDER BY on separate lines.

Formatting a query with Common Table Expressions

CTEs (WITH clauses) define named subqueries that can be referenced later. The formatter places each CTE definition in its own indented block, separates them with commas, and indents the final SELECT that references them, making the data flow through the CTEs visually clear.

Standardizing a team's SQL for code review

When multiple developers write SQL with different styles — some lowercase keywords, some inline everything, some indent inconsistently — running all queries through the formatter before committing ensures consistent style in version control, making diffs meaningful and code reviews focused on logic rather than formatting.

Readable window functions and aggregations

Complex analytical queries with PARTITION BY, ORDER BY within OVER clauses, and multiple aggregate functions become much clearer when formatted. The formatter indents window specifications and aligns aggregate expressions so you can see the grouping logic and sort order at a glance.

About this tool

Paste messy SQL to get it formatted with consistent indentation, keyword casing, and clause alignment. Supports MySQL, PostgreSQL, SQLite, and MSSQL dialects.

Common use cases

  • Formatting complex SQL queries from application logs for easier debugging
  • Standardizing SQL style before committing queries to version control
  • Making dense one-line queries from ORMs or query builders human-readable
  • Preparing SQL for code reviews and documentation with consistent formatting

Frequently asked questions

Which SQL dialects does this tool support?
The formatter supports MySQL, PostgreSQL, SQLite, and Microsoft SQL Server (T-SQL) dialects. Each dialect has specific keyword recognition and formatting rules.
Does formatting change the behavior of my SQL?
No. The formatter only modifies whitespace, indentation, and keyword casing. The query logic, table references, conditions, and results remain identical.
Can I customize the formatting style?
You can choose the SQL dialect and configure indentation style. The formatter applies consistent clause alignment (SELECT, FROM, WHERE, JOIN on separate lines) and keyword uppercasing by default.
Does the formatter handle subqueries and CTEs?
Yes. The formatter correctly indents subqueries within parentheses and formats Common Table Expressions (WITH clauses) with each CTE on its own indented block. Nested subqueries receive additional indentation levels so the query structure remains clear even with multiple levels of nesting.
Can I format stored procedures or DDL statements?
The formatter handles DDL statements like CREATE TABLE, ALTER TABLE, and CREATE INDEX with appropriate clause alignment. For stored procedures and functions, it formats the SQL statements within the body but may not fully parse procedural language constructs (IF/ELSE, loops) that vary significantly across dialects.
Why do my column aliases disappear or change?
The formatter preserves all aliases exactly as written — it never modifies identifiers, only whitespace and keyword casing. If an alias appears to change, check whether the formatter uppercased a keyword-like alias (e.g., "count" to "COUNT"). Using quoted identifiers for aliases that collide with SQL keywords avoids this issue.