Paste messy SQL and read it clearly in seconds
Compressed or hand-typed SQL can be almost impossible to scan, especially when it spans dozens of columns and joins. Paste your query above, pick your dialect, and the formatter rewrites it with clean indentation, consistent keyword casing, and line breaks in all the right places — instantly.
Before
select u.id,u.name,o.total from users u inner join orders o on u.id=o.user_id where o.total>100 order by o.total desc
After (formatted)
SELECT
u.id,
u.name,
o.total
FROM
users u
INNER JOIN orders o ON u.id = o.user_id
WHERE
o.total > 100
ORDER BY
o.total DESC
Keywords go UPPER-case, each column on its own line, joins and conditions indented — nothing about the query's logic changes.
How to use the SQL formatter
- Paste your query into the editor above.
- Choose your dialect — MySQL, PostgreSQL, SQL Server (T-SQL), Oracle PL/SQL, SQLite, BigQuery, Snowflake, Redshift, or MariaDB. Pick the one closest to your database; standard SQL works for most queries.
- Set keyword case to UPPER (common convention) or lower, whichever your team prefers.
- Adjust indent width — 2 or 4 spaces are both popular. Pick whatever matches your existing codebase.
- Hit Format. Copy the result straight into your editor or code review.
Worked example
Say you inherited this report query from a colleague:
select p.name,sum(s.amount) as revenue,count(s.id) as sales from products p left join sales s on p.id=s.product_id where s.created_at>='2024-01-01' group by p.name having sum(s.amount)>5000 order by revenue desc limit 10
After running it through the formatter with PostgreSQL dialect and 2-space indent, you get:
SELECT
p.name,
SUM(s.amount) AS revenue,
COUNT(s.id) AS sales
FROM
products p
LEFT JOIN sales s ON p.id = s.product_id
WHERE
s.created_at >= '2024-01-01'
GROUP BY
p.name
HAVING
SUM(s.amount) > 5000
ORDER BY
revenue DESC
LIMIT
10
Suddenly the structure is obvious — you can see the HAVING clause at a glance without hunting for it inside a wall of text.
Format SQL in your own code (Python & JavaScript)
If you need to pretty-print SQL programmatically rather than via a web tool, here are two quick options.
Python — using sqlfluff (install with pip install sqlfluff):
# Python 3 — sqlfluff
import subprocess
query = 'select id,name from users where active=1'
result = subprocess.run(
['sqlfluff', 'fix', '--dialect', 'ansi', '-'],
input=query,
capture_output=True,
text=True
)
print(result.stdout)
JavaScript / Node.js — using sql-formatter (install with npm install sql-formatter):
// Node.js — sql-formatter
const { format } = require('sql-formatter');
const raw = 'select id,name from users where active=1';
const pretty = format(raw, { language: 'postgresql', tabWidth: 2, keywordCase: 'upper' });
console.log(pretty);
The sql-formatter library on GitHub is the same open-source engine powering this tool.
How it works
The formatter tokenises your SQL — meaning it breaks the text into keywords, identifiers, operators, and literals — then walks the token list to rebuild the query with consistent spacing and line breaks. It understands clause boundaries (SELECT, FROM, WHERE, etc.) and nests subqueries one extra indent level deeper.
Because it only rewrites whitespace and capitalisation, the output query is identical to the input in every database's eyes. No logic changes, no values rewritten, no risk of breaking your query.
Everything runs entirely in your browser. Your SQL is never sent to any server, never stored, and never logged. Queries with passwords, API keys, or sensitive table names stay on your machine.
When to use it (and when not to)
| Use it when… | Skip it when… |
|---|---|
| Reviewing or debugging a query someone else wrote | Your query is auto-generated by an ORM and you're checking the ORM output, not reading SQL yourself |
| Adding SQL to a pull request where teammates must review it | You need a linter that also catches logic errors or style rule violations — use sqlfluff for that |
| Pasting query results from a log or monitoring tool | You're working with NoSQL (MongoDB, DynamoDB, etc.) — those aren't SQL at all |
| Matching your team's keyword-case convention quickly | The query contains vendor-specific syntax the formatter doesn't recognise — pick a closer dialect or format manually |
Dialects supported
The tool handles MySQL, PostgreSQL, SQL Server / T-SQL, Oracle PL/SQL, SQLite, BigQuery, Snowflake, Amazon Redshift, and MariaDB. Each dialect has slightly different syntax rules (for example, Snowflake uses $$ for stored procedures; T-SQL uses square-bracket identifiers). Choosing the right one gives you the most accurate indentation for your database.
Cleaner SQL, zero effort. Paste your query above and get a formatted, readable version in one click.