DevLab
{}
JSONbeginner

SQL Formatter

format and beautify SQL queries with keyword uppercasing and indentation

By DevLab Editorial TeamLast updated

Paste raw or minified SQL and this tool reformats it with keywords uppercased and each major clause (SELECT, FROM, WHERE, JOIN, ORDER BY, etc.) on its own line. Also minifies SQL to a single line. All processing is client-side — no SQL is sent to a server.

Try it now — free, instant, no signup

What is SQL Formatter?

SQL formatting transforms SQL statements into a consistent readable style: keywords uppercased, each major clause on a new line, sub-expressions indented. Consistent formatting makes queries easier to review in pull requests, easier to debug when errors appear, and easier to optimize when query plans are shared with a DBA.

Most SQL formatters apply two transformations: keyword normalization enforcing uppercase for reserved words like SELECT, FROM, WHERE, and structural indentation placing each clause on its own line so the logical structure is immediately visible. This tool handles the common case — flat queries with JOINs, WHERE clauses, GROUP BY, and ORDER BY — without any external library.

SQL minification is the inverse: removing all whitespace and newlines to produce a one-line query. This is useful for embedding SQL in configuration files, comparing queries as strings, or reducing query size in network payloads. Minified SQL is functionally identical to formatted SQL since database SQL parsers treat all whitespace equivalently.

When to use SQL Formatter

Format a minified SQL query from a database query log before sharing it with a colleague for review or debugging.
Normalize capitalization in a legacy codebase where SQL was written inconsistently with lowercase keywords.
Minify a SQL query before embedding it in a JSON configuration file or environment variable where newlines would break parsing.

Expert Notes

A pattern-based formatter is still worth having for quick debugging — 80% of debugging sessions involve straightforward SELECT statements where you just need readable formatting fast. For production use with complex procedural SQL, the serious choices are pgFormatter (handles PL/pgSQL), sql-formatter (JavaScript/npm, configurable dialect), and Prettier with the SQL plugin.

DevLab's Take

Most useful at the start of a debugging session: paste the raw query from a slow query log or ORM debug output, format it in one click, and immediately see the JOIN chain and WHERE conditions clearly.

Frequently Asked Questions

Does the formatter handle subqueries and CTEs?
Basic subqueries are handled — the formatter adds newlines before major keywords inside subqueries as well. CTEs using the WITH clause are recognized. However, deep nesting may not indent perfectly since this is a pattern-based formatter rather than a full SQL AST parser. For complex production queries, tools like pgFormatter or sql-formatter.js with AST parsing produce more precise output.
Why does the dialect selector say cosmetic?
The formatter applies the same keyword uppercasing and clause formatting regardless of which dialect is selected. SQL syntax for SELECT, FROM, WHERE, JOIN, and ORDER BY is nearly identical across PostgreSQL, MySQL, and SQLite, so a single ruleset covers 95% of queries. The selector label is a display hint to remind you which database the query targets.
Does this work with stored procedures or PL/pgSQL?
No. This tool formats DML queries (SELECT, INSERT, UPDATE, DELETE) and basic DDL (CREATE TABLE, ALTER TABLE). Stored procedure bodies, PL/pgSQL blocks, triggers, and functions contain procedural constructs that require a full procedural SQL parser. Use pgFormatter or a dialect-specific IDE formatter like DataGrip for those.
How does SQL minification differ from just removing newlines?
SQL minification collapses all whitespace sequences (spaces, tabs, newlines) to a single space and trims the result. The database's SQL parser treats any whitespace sequence as a single token separator, so minified SQL executes identically to formatted SQL. The edge case is SQL string literals containing literal newlines — those are preserved unchanged since they are part of the data value.
Can I use this for SQL injection detection?
No. Formatting normalizes whitespace and keyword casing but does not parse SQL semantics. SQL injection detection requires parsing the query into an AST and checking for unintended structure. Use parameterized queries and prepared statements in your application code to prevent injection; use static analysis tools like sqlcheck or Semgrep SQL rules for detection in code reviews.

Related Tools