About this tool
A query copied from logs, an ORM or a colleague’s message usually arrives as one long line. This SQL formatter (also called a SQL beautifier or pretty printer) puts each clause on its own line, indents subqueries, joins and CTEs, and makes keyword case consistent, so you can read, review and debug it. It works with 14 SQL dialects and runs entirely in your browser, so your queries and table names never leave your machine.
How to use it
- Paste your SQL, drop a
.sqlfile onto the box, or click Open file. - Choose your database’s dialect, then the keyword case and indent you prefer.
- The result updates as you type (for inputs up to 100 KB), or click Format SQL. Copy it, or download it as
query.sql.
Large dumps are formatted in a background worker, so the page stays responsive while it works.
Before and after
This one-line PostgreSQL query:
select c.id, c.email, count(o.id) as orders, sum(o.total) as revenue from customers c left join orders o on o.customer_id = c.id and o.status = 'paid' -- only paid
where c.created_at >= now() - interval '30 days' group by c.id, c.email having sum(o.total) > 100 order by revenue desc limit 10;
becomes:
SELECT
c.id,
c.email,
count(o.id) AS orders,
sum(o.total) AS revenue
FROM
customers c
LEFT JOIN orders o ON o.customer_id = c.id
AND o.status = 'paid' -- only paid
WHERE
c.created_at >= now() - interval '30 days'
GROUP BY
c.id,
c.email
HAVING
sum(o.total) > 100
ORDER BY
revenue DESC
LIMIT
10;
Only whitespace and keyword case change. Identifiers, string values, comments and function names stay exactly as you wrote them, so the formatted query does the same thing as the original.
Choosing the right dialect
Each database adds its own syntax, and a formatter set to the wrong dialect may stop with a parse error. These are typical examples:
| Dialect | Syntax it understands |
|---|---|
| PostgreSQL | :: type casts, $$ dollar-quoted strings, RETURNING |
| MySQL / MariaDB | `backtick` identifiers, LIMIT offset, count |
| SQL Server (T-SQL) | [bracket] identifiers, TOP, GO batches |
| Oracle PL/SQL | BEGIN … END blocks, :bind variables |
| Snowflake | column:path.to.field semi-structured access |
| BigQuery | `project.dataset.table` names, STRUCT and ARRAY |
| DuckDB, ClickHouse, Trino | Their own functions and table functions, such as read_parquet() |
If you’re not sure, start with Standard SQL and switch to your database if you get a parse error.
SQL formatting best practices
- One clause per line.
SELECT,FROM,JOIN,WHERE,GROUP BYandORDER BYeach start a line, which makes diffs in code review small and clear. - Uppercase keywords, lowercase names. The most common convention: it separates the SQL language from your tables and columns at a glance.
- Explicit joins. Write
JOIN … ON …rather than listing tables inFROMwith conditions inWHERE. - Meaningful aliases.
customers cis fine in a short query; in longer ones, use readable aliases and qualify every column. - CTEs over deep nesting. A
WITHclause with named steps is easier to read and test than subqueries three levels deep. - List columns instead of
SELECT *in application code, so schema changes don’t silently change your results. - Agree on one style as a team and enforce it automatically (see below), so nobody has to argue about commas in review.
Format SQL in your editor and CI
# Same formatter as this page (sql-formatter), via npx
npx sql-formatter -l postgresql query.sql # print formatted SQL
npx sql-formatter -l postgresql --fix query.sql # rewrite the file in place
npx sql-formatter -l mysql -c '{"keywordCase":"upper","tabWidth":4}' query.sql// npm install sql-formatter
import { format } from 'sql-formatter';
const pretty = format(sql, {
language: 'postgresql',
keywordCase: 'upper',
tabWidth: 2,
});# pip install sqlparse
import sqlparse
print(sqlparse.format(sql, reindent=True, keyword_case="upper"))
# For linting and formatting with style rules in CI: pip install sqlfluff
# sqlfluff format query.sql --dialect postgresMost editors, including VS Code, JetBrains IDEs and DBeaver, also have a built-in or extension-based “Format SQL” command.
Fixing parse errors
- Wrong dialect: the most common cause. For example,
::textfails as Standard SQL but works as PostgreSQL, and`backticks`fail as T-SQL but work as MySQL. - Template syntax: dbt or Jinja tags like
{{ ref('orders') }}and ORM placeholders aren’t SQL. Replace them with plain names or values before formatting. - Incomplete query: a missing closing parenthesis or quote stops the parser. The error message shows the line and column where it gave up.
FAQ
How do I format SQL online?
Paste the query into the box above, choose your database dialect, and the formatted SQL appears instantly. Copy it or download it as a .sql file.
Which databases are supported?
Standard SQL, PostgreSQL, MySQL, MariaDB, SQLite, SQL Server (T-SQL), Oracle PL/SQL, BigQuery, Snowflake, Redshift, Spark SQL, DuckDB, ClickHouse and Trino/Presto.
Is my SQL sent to a server?
No. Formatting runs in your browser with the open-source sql-formatter library, so queries, table names and data never leave your machine.
Does formatting change what my query does?
No. Only whitespace and keyword case change. Identifiers, strings and values are kept exactly as written.
Why do I get a parse error?
The query may use syntax from a different dialect. Switch the dialect to match your database.
Can I format multiple statements?
Yes. Separate statements with semicolons and each one is formatted.