Skip to main content

SQL Formatter

Format and validate SQL queries

SQL input and options

Uppercase keywords

SQL query

Up to 65,536 characters. Your query stays in this tab and is cleared when you leave.

Formatted SQL

Choose a dialect and format the query to see the result here. The source stays visible for comparison.

Was this SQL Formatter helpful?

Help us keep tools reliable, fast, and bug-free.

What you can do with SQL Formatter

  • Make sql query readable
  • Format select statement

How this data and SQL utility works

Beautify and indent SQL queries using Standard SQL, MySQL, PostgreSQL, SQLite, or T-SQL dialect rules.

Example input

select u.id,u.email,count(o.id) as total_orders from users u left join orders o on u.id=o.user_id where u.active=1 group by u.id,u.email having count(o.id)>5 order by total_orders desc;

Example output

SELECT
  u.id,
  u.email,
  COUNT(o.id) AS total_orders
FROM users u
LEFT JOIN orders o
  ON u.id = o.user_id
WHERE u.active = 1
GROUP BY
  u.id,
  u.email
HAVING COUNT(o.id) > 5
ORDER BY total_orders DESC;

Practical workflow

  1. Paste a SQL query into the source editor.
  2. Choose Standard SQL, MySQL, PostgreSQL, SQLite, or T-SQL formatting rules and set whether keywords should be uppercase.
  3. Select Format SQL, review the adjacent result, and copy it if it matches your intended style.

Common use cases

  • Refactoring dense, unreadable legacy database queries.
  • Formatting database migration scripts and ORM generated queries.
  • Standardizing SQL query conventions across engineering teams.

Important notes and limitations

  • Formats common SQL statements and nested clauses locally, including queries with comments and parameter markers. Queries are limited to 65,536 characters and stay in the open tab only; they are cleared when you leave or reload.
  • Does not connect to a database or execute queries; dialect formatting alone does not prove that a query is valid for a specific server version.
  • The casing control emits uppercase or lowercase keywords; there is no preserve-original-casing mode. Empty input shows an error and clears previous output.

Terminal & CLI equivalents

sqlfluff automated linter and formatter

sqlfluff format --dialect postgres query.sql

Industrial-grade CLI SQL formatter and linter supporting multiple database dialects.

pg_format for PostgreSQL queries

pg_format -s 2 -u 2 query.sql

Specialized formatter designed for complex PostgreSQL procedural functions and queries.

SQL Dialect Quoting & Syntax Matrix

DialectIdentifier QuotesString QuotesPaging SyntaxParameter Token
PostgreSQL"column_name"'string literal'LIMIT n OFFSET m$1, $2, $3
MySQL / MariaDB`column_name`'string' or "string"LIMIT offset, count? or :name
SQLite`col` or "col"'string literal'LIMIT n OFFSET m?, ?NNN, :name
SQL Server (T-SQL)[column_name]'string literal'TOP (n) or OFFSET FETCH@param1, @param2

Common errors & troubleshooting

SyntaxError: Unexpected token near identifier

Cause: Query uses reserved keywords as column names without proper dialect quote escaping.

Solution: Wrap column names in double quotes for PostgreSQL, backticks for MySQL, or square brackets for T-SQL.

Unmatched single quote in SQL string literal

Cause: Single quotes inside text data (e.g. O'Connor) break SQL syntax without escaping.

Solution: Escape single quotes by doubling them in standard SQL ('O''Connor').

Continue your workflow

  • CSV to SQL Converter — convert spreadsheet data and CSV files into executable SQL INSERT statements.
  • Format Converter — convert query result sets between JSON, CSV, YAML, and XML formats.
  • Mock Data Generator — generate synthetic relational datasets with realistic database foreign keys.
  • Diff Checker — compare SQL database migration scripts and schema definitions side by side.

Frequently asked questions

Does the SQL formatter support CTEs (WITH clauses) and window functions?

Yes. Common Table Expressions (WITH ... AS) and window functions (OVER PARTITION BY) are indented according to standard database engineering conventions.

Will formatting change query execution plans or query performance in the database?

No. SQL database query optimizers treat whitespace and line breaks as neutral tokens; formatting alters only visual presentation without affecting execution plans.

Can I format queries containing parameterized markers like $1 or :userId?

Yes. Parameterized placeholders from PostgreSQL ($1, $2), MySQL (?), and ORMs (:param) are preserved intact without syntax errors.