SQL Formatter and Beautifier

Paste a messy query, pick your database, and get clean, indented SQL. Your query stays in your browser.

Loading the formatter (about 320 KB, downloaded once)...

How to format SQL with this tool

  1. Paste your query into Input SQL. The page opens with a one-line example so you can see a result straight away.
  2. Choose your database in Dialect (Standard SQL, MySQL, PostgreSQL, SQL Server, SQLite, Oracle and more). It decides how quotes, parameters and keywords are read.
  3. Set Indent (2 spaces, 4 spaces or tab) and Keywords (UPPERCASE, lowercase or Preserve).
  4. The result appears in Formatted SQL as you type, or click Format SQL. Click Minify for a one-line version and Copy to copy the result.

How the formatting works

Formatting is done by the open source sql-formatter library (version 15.9.0, MIT licence), which runs in your browser. It splits the query into tokens using the rules of the chosen dialect, puts each major clause (SELECT, FROM, WHERE, GROUP BY, ORDER BY, LIMIT) on its own line, indents lists and subqueries one level, and applies your keyword case to keywords, data types and built-in function names. Built-in functions print as COUNT(*) with no space before the bracket, which MySQL requires by default.

Minify is separate code written for this page. It removes comments and extra whitespace outside quoted text, keeps optimizer hints such as /*+ ... */, and keeps a space wherever two tokens would otherwise fuse, for example a - -1.

Worked example

With the default settings (Standard SQL, 4 spaces, UPPERCASE), the example input

select u.id,u.name,count(o.id) as orders,sum(o.total) as revenue from users u left join orders o on o.user_id=u.id and o.status='paid' where u.created_at>='2025-01-01' and u.country in ('US','CA') group by u.id,u.name having count(o.id)>1 order by revenue desc limit 10;

becomes

SELECT
    u.id,
    u.name,
    COUNT(o.id) AS orders,
    SUM(o.total) AS revenue
FROM
    users u
    LEFT JOIN orders o ON o.user_id = u.id
    AND o.status = 'paid'
WHERE
    u.created_at >= '2025-01-01'
    AND u.country IN ('US', 'CA')
GROUP BY
    u.id,
    u.name
HAVING
    COUNT(o.id) > 1
ORDER BY
    revenue DESC
LIMIT
    10;

Limits and edge cases

Frequently asked questions

Does formatting change what my SQL does?

No. Only whitespace and the letter case of keywords, data types and built-in function names change. Text in quotes, table names and column names stay as you typed them. Minify also removes comments, which never run, but keeps optimizer hints such as /*+ ... */.

Which SQL dialects does the formatter support?

Standard SQL, MySQL, MariaDB, PostgreSQL, SQL Server (T-SQL), SQLite, Oracle (PL/SQL), BigQuery, Snowflake, Amazon Redshift, Spark SQL, Hive, Trino and IBM DB2. Pick the one that matches your database so quotes, parameters and keywords are read correctly.

Why do I get a parse error?

The message gives the line and column where the formatter got stuck. The usual causes are an unclosed quote or bracket, or syntax from a different database, such as [square brackets] or @variables under the wrong Dialect. Choose your database and try again.

Should SQL keywords be uppercase or lowercase?

Both run the same. Uppercase keywords are the traditional style and make clauses stand out from table and column names, and Simon Holywell's SQL style guide (sqlstyle.guide) recommends them. Lowercase is common in newer codebases. Pick one style and use it across the project.

Working with other formats? Try the JSON formatter and validator, the XML formatter or the YAML formatter, or read the SQL formatting style guide.