SQL Beautifier

Format & beautify SQL. Free SQL formatter.

SQL
Formatted SQL

Output
characters

Rate this tool

How useful was this tool?

SQL Beautifier

Paste SQL into the box, or load the example, and the formatted result appears under Output. There are two ways to format:

  • Beautified lays the query out over several lines, each clause and column in its place.
  • Compact (one statement per line, comments removed) squeezes each statement onto a single line.

Choose the Dialect your database speaks: Standard SQL, MySQL, MariaDB, PostgreSQL, SQLite, SQL Server (T-SQL), Oracle PL/SQL, Google BigQuery, Snowflake, Amazon Redshift, Spark SQL, Hive, Trino / Presto, ClickHouse, DuckDB, IBM Db2, IBM Db2 for i, Couchbase N1QL, SingleStoreDB or TiDB. The other options set the case of keywords, functions and identifiers, the indentation, the layout and a few finer points.

A summary shows what went in and what came out, and how long it took. Mistakes are reported with their line and column.

Formatting happens entirely in your browser. Your SQL isn't sent anywhere.

Example

Two statements, written the way they often end up in code, logs or a quick test:

-- Revenue per active customer since January, top 20
select c.id, c.name, 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 in ('paid', 'shipped') where c.active = 1 and o.created_at >= '2026-01-01' group by c.id, c.name having sum(o.total) > 100 order by revenue desc limit 20;
update customers set tier = 'gold' where id in (select customer_id from orders group by customer_id having sum(total) > 1000);

Beautified with upper-case keywords and a 2-space indent:

-- Revenue per active customer since January, top 20
SELECT
  c.id,
  c.name,
  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 IN ('paid', 'shipped')
WHERE
  c.active = 1
  AND o.created_at >= '2026-01-01'
GROUP BY
  c.id,
  c.name
HAVING
  sum(o.total) > 100
ORDER BY
  revenue DESC
LIMIT
  20;

UPDATE customers
SET
  tier = 'gold'
WHERE
  id IN (
    SELECT
      customer_id
    FROM
      orders
    GROUP BY
      customer_id
    HAVING
      sum(total) > 1000
  );

The summary reads 2 statements, 3 lines, 475 characters in; 37 lines, 552 characters out, plus the time it took. Each clause starts a new line, each column sits on its own line, and the subquery in the UPDATE is indented inside its parentheses. The comment stays exactly where it was.

Formatting makes the logic readable

A query on one line hides its structure. Laid out, you can see it, and sometimes you see a mistake. In the example, o.created_at >= '2026-01-01' now stands out in the WHERE clause, under a LEFT JOIN. A condition on the joined table in WHERE throws out every customer without a matching order. That quietly turns the LEFT JOIN into an ordinary inner join.

Here the HAVING sum(o.total) > 100 removes those customers anyway, so the result is the same, but in another query it wouldn't be. When you mean to keep customers with no orders, the condition belongs in the ON clause, next to the status filter. That's the kind of thing that's easy to spot once every condition has its own line.

Pick the dialect

Each database speaks its own variety of SQL. The dialect decides:

  • which words count as keywords
  • how names are quoted: backticks in MySQL, double quotes in PostgreSQL, brackets in SQL Server
  • which operators exist

With the wrong dialect, valid SQL can fail to format. PostgreSQL's cast created_at::date is reported as a mistake under Standard SQL and formats cleanly with PostgreSQL. SQL Server names in brackets, such as [order id] or [dbo].[orders], are only understood with SQL Server (T-SQL). If a query that runs fine in your database is reported as wrong here, check the dialect first.

Case

Keywords, Functions and Identifiers each have their own case setting: UPPER CASE, lower case or As written. They're changed independently. Upper-case keywords with lower-case names is the classic style, but many teams now write everything in lower case. As written leaves that part exactly as you typed it.

Strings and comments are never changed. 'Paid' stays 'Paid', whatever the settings.

Be careful with identifier case. Some databases treat names as case-sensitive: quoted names in PostgreSQL, and table names in MySQL on Linux. There, changing their case can break the query, so As written is the safe choice for identifiers.

Layout

Layout sets the overall shape.

Standard puts each clause keyword on its own line, with its contents indented below, as in the example.

Tabular, keywords left lines the keywords up in a column on the left:

SELECT    c.id,
          c.name,
          count(o.id) AS orders
FROM      customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE     c.active = 1
GROUP BY  c.id,
          c.name
ORDER BY  orders DESC
LIMIT     20;

Tabular, keywords right aligns them to the right, so the content starts in one straight column:

   SELECT c.id,
          c.name,
          count(o.id) AS orders
     FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
    WHERE c.active = 1
 GROUP BY c.id,
          c.name
 ORDER BY orders DESC
    LIMIT 20;

The finer options:

  • Indent: 2 spaces, 4 spaces or a tab.
  • AND / OR: at the start of the line, as in the example, or at the end.
  • Wrap expressions at a number of characters, so long expressions such as IN (...) lists break over lines.
  • Blank lines between statements: 0, 1 or 2.
  • No spaces around operators: a=1 instead of a = 1.
  • Semicolon on its own line, which some teams prefer because it makes added lines easier to see in a diff.

Compact mode

Compact is the opposite of beautifying. Whitespace collapses to single spaces, each statement goes on a line of its own, and comments are removed. They have to go: a -- comment runs to the end of the line, so on a single line it would comment out the rest of the statement.

Compact SQL is handy anywhere a query has to fit on one line: a log message, a configuration file, an environment variable, a URL parameter, or a search through log files for a particular statement.

Mistakes, with line and column

When something can't be read as SQL, the line under the editors says where the beautifier got stuck. The output keeps the last version that formatted, so you don't lose it. Two examples, with Standard SQL selected:

select * from t where (a = 1 gives:

Could not parse this as Standard SQL: parse error at the end of the text at line 1 column 29. The output above is the last formatted version. Check the dialect, or look at the place the message points to.

The parenthesis is never closed, so the problem only shows at the very end, where the ) should be. The real mistake is often just before the place the message points to.

select created_at::date from orders gives:

Could not parse this as Standard SQL: parse error: Unexpected "::date fro" at line 1 column 18. The output above is the last formatted version. Check the dialect, or look at the place the message points to.

This time there's nothing wrong with the query. :: is PostgreSQL's cast operator, and Standard SQL doesn't have it. Select PostgreSQL and it formats cleanly. That's why the message suggests checking the dialect first.

FAQ

Does formatting change what the query does?

No. Only whitespace and the case of keywords and functions change, and SQL ignores both. The exception is identifier case in databases where names are case-sensitive, so leave Identifiers on As written to be safe.

Why did my column day become DAY?

Words such as day, date, name, user and value are also keywords in many dialects. With upper-case keywords, a column with one of those names can be upper-cased too. In Oracle, name becomes NAME this way. For an unquoted name it's harmless in most databases, which ignore the case of unquoted names. To keep it exactly as written, quote the name, or set keywords to As written.

Which dialect should I choose for Aurora or Azure SQL?

Choose the database the service is built on. Amazon Aurora is MySQL or PostgreSQL underneath, and Azure SQL is SQL Server. MariaDB has its own option. When in doubt, Standard SQL handles plain queries, but not each database's own syntax.

Can it format stored procedures?

Partly. The queries inside a procedure are formatted properly, but the procedure's own structure isn't indented as a block. BEGIN stays on the CREATE line, END ends up next to the last statement, and a few procedural words, such as SET NOCOUNT ON, are split onto their own lines.

Choose the right dialect, too. Under Standard SQL, a T-SQL procedure is refused at its first parameter (@Id). With SQL Server (T-SQL) or Oracle PL/SQL, the statements inside come out well.

Is my SQL sent anywhere?

No. Formatting runs entirely in your browser, and the formatter is loaded from this site, not a third party. Your queries never leave your computer, so it's safe to paste production SQL.

Note: All processing is done client-side. We do not store or see your data.

Contact

Missing something?

Feel free to request missing tools or give some feedback using our contact form.

Contact Us