What does a SQL formatter do?
A SQL formatter rewrites a query with consistent line breaks, indentation and keyword case, without changing what it does. Long one-line queries, often copied from logs, ORMs or query builders, become easy to read, review and compare.
This query:
select id, name from users where active = true and created_at > '2026-01-01' order by name
becomes:
SELECT
id,
name
FROM
users
WHERE
active = TRUE
AND created_at > '2026-01-01'
ORDER BY
name
How to format SQL
- Paste your query into SQL input, open a
.sqlfile, or drop one onto the editor. - Choose your database under Dialect.
- Set the keyword case, indent and where
ANDandORgo. - Select Format or press Ctrl + Enter. Copy or download the result.
Changing a setting re-formats the query straight away. If the query cannot be parsed, the error shows the line and column, and Show in input moves the cursor there.
Supported SQL dialects
PostgreSQL, MySQL, MariaDB, SQLite, SQL Server (T-SQL), Oracle (PL/SQL), BigQuery, Snowflake, Amazon Redshift, Spark SQL, Trino and Presto, DuckDB, ClickHouse, IBM Db2, Hive and Standard SQL.
Dialects matter because databases differ in syntax. PostgreSQL uses $1 parameters and operators such as ->>, SQL Server quotes names with [brackets] and limits rows with TOP, and MySQL quotes names with `backticks`. Choosing the right dialect lets the formatter recognise these.
SQL style conventions
There is no single official SQL style, but most guides agree on a few points:
- Put each major clause on its own line:
SELECT,FROM,WHERE,GROUP BY,HAVING,ORDER BY. - Indent the contents of each clause so the structure is visible at a glance.
- Use one case for keywords. Upper case is traditional; lower case is increasingly common. Consistency matters more than the choice.
- Start lines with
ANDandORin long conditions, so each condition can be commented out or reordered easily. - Give tables short, meaningful aliases (
customers c) and qualify columns in joins (c.id).
When a query won’t format
The formatter has to understand a query’s structure before it can lay it out, so it stops at anything it can’t parse and shows where. The usual causes are:
- The wrong dialect. Vendor syntax such as PostgreSQL’s
::casts or SQL Server’s[bracketed names]only parses in the dialects that support it. Choose the database the query runs on. - Query parameters. Placeholders are written differently in each database:
?,:name,$1or@name. Standard SQL only accepts?, soWHERE id = :idneeds a dialect that supports named parameters. - Template tags. dbt models and other templated SQL contain tags such as
{{ ref('orders') }}or{% if %}that are replaced before the query runs. They aren’t SQL, so swap them for real table names before formatting. - An unclosed quote or bracket. The error points at where the formatter gave up, which is usually just after the missing character.
Formatting SQL in your editor or CI
To format SQL files automatically, use a command-line tool. This page uses the open-source sql-formatter library, which also has a command line:
# Print the formatted query
npx sql-formatter --language postgresql query.sql
# Rewrite the file in place
npx sql-formatter --language postgresql --fix query.sql
SQLFluff goes further: it lints SQL against style rules and can fix many of them, which suits teams that want every query checked in CI. For PostgreSQL, pgFormatter is another long-standing option. Whichever you choose, run the same tool and settings in your editor and in CI, so formatting never shows up as noise in code review.
Privacy
Queries often contain table structures, customer data and sometimes credentials. This formatter runs entirely in your browser: the query is never uploaded, logged or stored.