SQL Formatter

Data Stays on Your Device

Format and beautify complex SQL queries for better readability.

SQL Input
Loading...
Formatted SQL
Loading...

What is the SQL Formatter?

The SQL Formatter takes a query written on one line, or with inconsistent indentation, and rewrites it with keywords, clauses, and joins laid out on their own lines, so the structure of the statement becomes visible.

SQL is whitespace-insensitive, so a query can be written entirely on one line. Queries generated by ORMs, pulled from application logs, or accumulated through repeated edits frequently are, and a non-trivial join across several tables becomes very difficult to follow in that form.

Formatting aligns clauses so that SELECT, FROM, WHERE, GROUP BY, and each JOIN start at a predictable position. Reading a query then becomes a matter of scanning a vertical list of clauses rather than parsing a paragraph of text.

This matters for correctness as much as readability. Subtle mistakes, a join condition accidentally placed in the WHERE clause, a misplaced parenthesis in a nested condition, are much easier to see once the query is laid out.

How to use the SQL Formatter

  1. Paste your query. Any SQL statement works, including multi-statement scripts.
  2. Select your dialect if offered. Dialect awareness improves handling of vendor-specific keywords and functions.
  3. Click Format. Clauses are placed on their own lines with consistent keyword casing and indentation.
  4. Copy the result. Paste the formatted query back into your editor, migration, or ticket.

Worked examples

Formatting a join

The relationship between tables and the filtering conditions are hard to separate on one line, and immediately clear once formatted.

Input
select u.id, u.name, count(o.id) as order_count from users u left join orders o on o.user_id = u.id where u.active = true group by u.id, u.name having count(o.id) > 3 order by order_count desc;
Output
SELECT
  u.id,
  u.name,
  COUNT(o.id) AS order_count
FROM
  users u
  LEFT JOIN orders o ON o.user_id = u.id
WHERE
  u.active = TRUE
GROUP BY
  u.id,
  u.name
HAVING
  COUNT(o.id) > 3
ORDER BY
  order_count DESC;

Revealing nesting in a subquery

Indentation shows which conditions belong to the inner query and which to the outer one.

Input
select * from products where category_id in (select id from categories where parent_id is null);
Output
SELECT
  *
FROM
  products
WHERE
  category_id IN (
    SELECT
      id
    FROM
      categories
    WHERE
      parent_id IS NULL
  );

Common use cases

  • Reading ORM-generated SQL. Query logs from an ORM are usually single-line. Formatting is the first step in understanding what was actually executed.
  • Reviewing migrations. Formatted DDL in a pull request is far easier to review carefully.
  • Debugging slow queries. Seeing the join order and filter placement clearly is necessary before reasoning about a query plan.
  • Standardising team style. Consistent formatting across a codebase keeps diffs focused on real changes rather than layout.

Features and limitations

  • Places each major clause on its own line with consistent indentation.
  • Normalises keyword casing, conventionally to uppercase.
  • Indents subqueries and parenthesised expressions according to nesting depth.
  • Supports common dialects including PostgreSQL, MySQL, SQL Server, and SQLite.
  • It is a formatter, not an optimiser or a linter, it changes layout only and will not alter the semantics of a query.

Frequently asked questions

Does formatting change what my query does?

No. Only whitespace and keyword casing change. SQL ignores both, so the formatted query is semantically identical to the original.

Which SQL dialects are supported?

The common ones, PostgreSQL, MySQL, SQL Server, SQLite, and standard SQL. Selecting the right dialect improves handling of vendor-specific syntax and functions.

Will it preserve my comments?

Yes. Both single-line and block comments are retained, though they may be repositioned to fit the new line structure.

Can it format a script with several statements?

Yes. Statements separated by semicolons are each formatted in turn.

Does it optimise my query?

No. Formatting is purely cosmetic. For performance work, use EXPLAIN or EXPLAIN ANALYZE against your database to see the actual execution plan.

Is my query sent to a server?

No. Formatting runs in your browser, which matters because queries frequently contain table names, column names, and literal values you would not want to transmit.

All processing happens locally in your browser, your data never leaves your device.