SQL Formatter & Beautifier Online

Format, indent, and clean up SQL queries instantly

Examples:
SQL Input
Formatted Output

SQL Dialects Supported

Dialect Database Key Syntax Differences
Standard SQLANSI / ISODouble-quote identifiers, standard types
MySQLMySQL, AuroraBacktick identifiers, LIMIT, AUTO_INCREMENT
PostgreSQLPostgreSQL, CockroachDBDollar-quoting, arrays, RETURNING, :: casting
T-SQLSQL Server, Azure SQLSquare bracket identifiers, TOP, NOLOCK
Oracle PL/SQLOracle DatabaseROWNUM, PL/SQL blocks, NVL, CONNECT BY
IBM DB2Db2 LUW, Db2 z/OSFETCH FIRST, WITH UR
Amazon RedshiftAmazon RedshiftDISTKEY, SORTKEY, COPY command
Spark SQLApache SparkLATERAL VIEW, TRANSFORM, Hive-compatible
Apache HiveHive, Presto, TrinoPARTITIONED BY, STORED AS, SERDEPROPERTIES
MariaDBMariaDBMySQL-compatible with extensions like RETURNING
Couchbase N1QLCouchbase ServerJSON-oriented queries, NEST, UNNEST
Tip: Selecting the correct dialect ensures the formatter recognizes database-specific keywords and syntax. If unsure, Standard SQL works for most common queries.

How SQL Formatting Works

A proper SQL formatter does not use simple find-and-replace. It works in three stages:

  1. Tokenization -- The raw SQL string is split into tokens: keywords, identifiers, operators, literals, and comments. String literals and comments are preserved verbatim so keywords inside strings are never modified.
  2. Structure analysis -- Tokens are grouped into clauses (SELECT, FROM, WHERE, JOIN, etc.) and nesting levels (subqueries, function calls, CASE expressions). This determines where to insert line breaks and indentation.
  3. Rendering -- The structured token tree is converted back to text with the selected formatting rules applied: keyword case, indent width, line breaks between clauses, and alignment.

This is why a parser-based formatter handles complex SQL correctly -- it understands structure, not just patterns. A regex-based approach would break on subqueries, string literals containing keywords, nested function calls, and CTE expressions.

Formatting vs. Linting vs. Optimization

  • Formatting changes how SQL looks. It never changes behavior.
  • Linting checks for potential issues like missing aliases, ambiguous column references, or deprecated syntax.
  • Optimization rewrites the query logic for better performance (adding indexes, restructuring joins, etc.).

This tool does formatting only. The query you get back runs identically to the one you put in.

SQL Formatting Best Practices

Keyword Case Convention

The dominant industry convention is uppercase keywords. This creates a visual separation between SQL structure (SELECT, WHERE, JOIN) and your data names (users, order_total, created_at). Some modern teams prefer lowercase for consistency with programming language conventions. Either works -- consistency across your codebase matters more.

Uppercase Keywords (traditional)
SELECT
  u.name,
  u.email,
  COUNT(o.id) AS order_count
FROM users u
JOIN orders o
  ON u.id = o.user_id
WHERE u.active = 1
GROUP BY u.name, u.email
HAVING COUNT(o.id) > 5
ORDER BY order_count DESC;
Lowercase Keywords (modern)
select
  u.name,
  u.email,
  count(o.id) as order_count
from users u
join orders o
  on u.id = o.user_id
where u.active = 1
group by u.name, u.email
having count(o.id) > 5
order by order_count desc;

Indentation Rules

  • Major clauses (SELECT, FROM, WHERE, GROUP BY, ORDER BY) start at the left margin or at the current nesting level.
  • Column lists and conditions are indented one level deeper than their clause.
  • Subqueries are indented one level deeper than the surrounding query.
  • JOIN conditions (ON) are indented under the JOIN keyword.
  • CASE expressions indent WHEN/THEN/ELSE under CASE.

Line Breaks

  • Put each major clause on its own line.
  • One column per line in SELECT lists when there are more than 2-3 columns.
  • AND/OR on new lines in WHERE clauses, indented under WHERE.
  • Each JOIN on its own line with its ON condition below it.

Naming Conventions

  • Use snake_case for table and column names (not camelCase).
  • Table names: singular or collective nouns (user or staff, not users_table).
  • Suffix conventions: _id, _at (timestamps), _count, _total, _name.
  • Avoid prefixes like tbl_ or sp_.

Frequently Asked Questions

What does a SQL formatter do?

A SQL formatter restructures raw or compressed SQL with consistent indentation, line breaks, and keyword casing. It makes queries easier to read, review, and debug without changing what the query does.

Does formatting SQL change query results?

No. SQL formatting is cosmetic. The database ignores whitespace, indentation, and keyword case. Your formatted query runs identically to the original.

Should SQL keywords be uppercase?

Uppercase keywords (SELECT, FROM, WHERE) is the most common convention because it visually separates SQL structure from column and table names. Some teams prefer lowercase. The key is consistency across your project.

What is the best indent size for SQL?

2 spaces and 4 spaces are both common. Most style guides recommend 2-4 spaces. Tabs are less common in SQL but work fine if your team uses them. Match your project standard.

Can this formatter handle subqueries and CTEs?

Yes. This tool uses a parser-based formatter that understands SQL structure including nested subqueries, CTEs (WITH clauses), CASE expressions, window functions, and complex JOIN conditions.

Why does the formatter not fix my SQL error?

A formatter restructures layout, not logic. If your SQL has syntax errors (missing parenthesis, misspelled keyword), the formatter will produce output but the query remains invalid. Use a SQL linter or validator for error detection.

What SQL dialects are supported?

Standard SQL, MySQL, MariaDB, PostgreSQL, T-SQL (SQL Server), Oracle PL/SQL, IBM DB2, Amazon Redshift, Spark SQL, Apache Hive, and Couchbase N1QL. Select your dialect from the dropdown for best results.

Is my SQL sent to a server?

No. All formatting runs entirely in your browser using JavaScript. No data leaves your machine. Nothing is stored, logged, or tracked.

What is SQL minification?

Minification removes extra whitespace and line breaks to produce the most compact version of your query. Useful for embedding SQL in code strings, log entries, or configuration files where readability is not the priority.

How do I format a stored procedure?

Paste the full stored procedure including CREATE PROCEDURE and BEGIN/END blocks. Select the correct dialect (T-SQL for SQL Server, PL/SQL for Oracle) for best results with procedural syntax.

Related Tools

Privacy & Limitations

  • All calculations run entirely in your browser -- nothing is sent to any server.
  • Results are computed locally and should be verified for critical applications.

Related Tools

View all tools

SQL Formatter FAQ

What does a SQL formatter do?

A SQL formatter takes raw or messy SQL and restructures it with consistent indentation, line breaks, and keyword casing so it is easier to read, review, and maintain. It does not change what the query does -- only how it looks.

Does formatting SQL change how the query runs?

No. SQL formatting is purely cosmetic. The database engine ignores whitespace, indentation, and keyword casing. A formatted query produces the exact same result as the original.

Should SQL keywords be uppercase or lowercase?

The most common convention is uppercase keywords (SELECT, FROM, WHERE) because it visually separates SQL structure from table and column names. Some modern teams prefer lowercase for consistency. The important thing is to pick one style and use it everywhere.

What is the difference between SQL dialects for formatting?

Different databases extend standard SQL with their own syntax. MySQL uses backtick quoting, PostgreSQL supports dollar-quoting and arrays, T-SQL has square bracket identifiers and TOP, Oracle has PL/SQL blocks. A dialect-aware formatter handles these extensions correctly.

How should I indent SQL subqueries?

Indent the contents of a subquery one level deeper than the surrounding query. This makes nesting depth immediately visible. Most style guides recommend 2 or 4 spaces per indent level.

Should commas go at the beginning or end of the line in SQL?

Trailing commas (end of line) are the most common convention. Leading commas (start of line) make it easier to add or remove columns and immediately spot missing commas. Both are valid -- consistency within a team matters more than the choice itself.

Can a SQL formatter fix syntax errors?

A formatter restructures layout but does not fix logic or syntax errors. If your SQL has a missing closing parenthesis or misspelled keyword, the formatter will still try to produce readable output, but the query will remain invalid.

What SQL dialects does this formatter support?

This tool supports Standard SQL, MySQL, MariaDB, PostgreSQL, T-SQL (SQL Server), Oracle PL/SQL, Amazon Redshift, IBM DB2, Apache Spark SQL, Apache Hive, and Couchbase N1QL.

Does this tool store my SQL queries?

No. All formatting runs entirely in your browser using JavaScript. No SQL is sent to any server, stored, or logged. When you close the page, everything is gone.

What is SQL minification?

SQL minification removes unnecessary whitespace, line breaks, and indentation to produce the most compact single-line (or near-single-line) version of a query. This is useful for embedding SQL in code strings, logs, or configuration files where readability is not the priority.

Request a New Tool
Improve This Tool