SQL Formatter - Beautify & Format SQL Queries Online (Free)

Paste any SQL query and format it instantly. Supports MySQL, PostgreSQL, T-SQL, BigQuery, Snowflake & more. Free, browser-only — your data never leaves

Tool options

SQL dialect

Keywords

Indent

Input
338 chars · 1 lines · 338 bytes
Output

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

  1. Paste your query into the editor above.
  2. 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.
  3. Set keyword case to UPPER (common convention) or lower, whichever your team prefers.
  4. Adjust indent width — 2 or 4 spaces are both popular. Pick whatever matches your existing codebase.
  5. 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 wroteYour 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 itYou need a linter that also catches logic errors or style rule violations — use sqlfluff for that
Pasting query results from a log or monitoring toolYou're working with NoSQL (MongoDB, DynamoDB, etc.) — those aren't SQL at all
Matching your team's keyword-case convention quicklyThe 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.

Frequently asked questions

Is my SQL uploaded to a server?+
No. The formatter runs entirely in your browser using JavaScript. Your query never leaves your machine, so sensitive table names, column names, or hardcoded values stay private.
Does formatting change what the query does?+
Never. The formatter only changes whitespace (spaces, line breaks, indentation) and optionally the capitalisation of keywords. The database engine sees an identical query and produces identical results.
Which SQL dialect should I pick?+
Choose the database you're actually running — MySQL, PostgreSQL, SQL Server, etc. If you're unsure or writing generic SQL, 'SQL' (standard) works for most common queries. The dialect setting mainly affects how the formatter handles vendor-specific syntax like MySQL backtick identifiers or T-SQL square brackets.
How do I format SQL in VS Code?+
Install the 'SQLTools' extension or the dedicated 'sql-formatter-vscode' extension from the VS Code marketplace. You can then use Format Document (Shift+Alt+F on Windows/Linux, Shift+Option+F on Mac) just like you would for any other language.
What's the difference between a SQL formatter and a SQL linter?+
A formatter (or SQL beautifier) only fixes whitespace and casing — it makes the query easier to read but doesn't catch logic bugs. A linter like sqlfluff or SonarQube also checks for rule violations, naming conventions, and potential errors. Use a formatter for readability, a linter for code quality enforcement.
Can it handle very long or complex queries?+
Yes. The formatter works on queries of any length — including complex ones with multiple CTEs (WITH clauses), subqueries, and window functions. Very large queries with thousands of lines may take a second or two to process in the browser, but there's no hard limit.
Is the tool free? Do I need to sign up?+
Completely free, no account required. Just paste and format.
What does 'keyword case' mean and which should I use?+
SQL keywords like SELECT, FROM, and WHERE can be written in UPPER-case or lower-case — databases accept both. UPPER-case is the traditional convention and makes keywords visually distinct from table or column names. Many modern teams use lower-case for a cleaner look. Pick whichever matches your codebase or team style guide.