OnlineTextLab
Esc

SQL Formatter

One long query turned into readable clauses, with strings and comments left exactly alone.

Keywords
Indent

Formatted SQL

The formatted query appears here. Strings and comments are never changed.

Runs on your device. Your files and text are not uploaded.

About SQL Formatter

SQL copied from an ORM log, a slow query report or a colleague's message tends to arrive on one line. Reading that means hunting for where the joins begin and which conditions belong where. Paste it here and each major clause starts its own line, with select lists and conditions indented beneath.

You choose uppercase, lowercase or as-typed keywords and an indent of 2 or 4 spaces. Because formatting a query should never change it, text inside strings, quoted identifiers and comments is never touched. A 'select from' inside quotes keeps both spaces and its lowercase.

How it works

The formatter splits the text into tokens, and it understands strings, quoted names in double quotes, backticks and square brackets, line and block comments, parameters such as $1 and :name, and Postgres $$ blocks. Each token is carried over as written, and only the whitespace between them changes. Keywords get their case changed unless they sit next to a dot, so a column called t.order is safe.

It then lays those tokens out by clause: SELECT, FROM, joins, WHERE, GROUP BY and so on, with subqueries indented and CASE branches on their own lines. It does not check your SQL against a grammar, so it is not a validator, but an unclosed string or an unmatched parenthesis stops it with the line number.

How to use SQL Formatter

  1. Paste one or more statements into the SQL query box, or press Upload to open a .sql file. Try an example loads a join with a date filter.
  2. Under Keywords, pick UPPER, lower or As typed.
  3. Under Indent, pick 2 spaces or 4 spaces.
  4. Press Copy, or Download to save formatted.sql.

A one-line report query

Input
select u.id, u.name, count(o.id) as orders from users u left join orders o on o.user_id = u.id and o.status <> 'cancelled' where u.active = true and u.created_at between '2025-01-01' and '2025-12-31' group by u.id, u.name having count(o.id) > 3 order by orders desc limit 20;
Output
SELECT
  u.id,
  u.name,
  COUNT(o.id) AS orders
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
  AND o.status <> 'cancelled'
WHERE u.active = TRUE
  AND u.created_at BETWEEN '2025-01-01' AND '2025-12-31'
GROUP BY u.id, u.name
HAVING COUNT(o.id) > 3
ORDER BY orders DESC
LIMIT 20;

The AND inside BETWEEN stays on its line, since it is part of that range and is not another condition.

When it helps

  • Reading a query printed by an ORM or a slow query log
  • Tidying a statement before pasting it into a pull request or a ticket
  • Making two versions of a query comparable before a diff
  • Cleaning up a long report query someone wrote on one line

Tips for a better result

  • Format both sides first when you compare two queries. A Diff Checker then shows real changes and not spacing.
  • Several statements separated by semicolons are each formatted, with a blank line between them.
  • Check the output only for layout. If a query fails to run, the cause is elsewhere, because this tool never changes the logic.

Good to know

  • It does not parse your dialect or check that a query is valid, so SELEC passes through as an ordinary word.
  • Only keywords on its list change case. Dialect words such as QUALIFY, and type names such as int, stay as you typed them.
  • Stored procedure bodies inside $$ blocks are treated as one string and are not reformatted.
  • Very long or unusual statements can come out less neatly, since layout rules target common queries.

Questions people ask

Will formatting change what my query does?

It should not. Only whitespace and the case of recognized keywords change, and SQL keywords are not case sensitive. Strings, quoted names and comments are carried across unchanged.

Which databases does it support?

It is not tied to one. It understands quoting from Postgres, MySQL and SQL Server, including backticks, square brackets and $$ blocks, and it lays out the clauses those dialects share.

Why do my comments stay where they are?

Comments often explain the line beside them, so they are kept in place, and a line comment always ends its line so it cannot swallow the next token.

Can it check my SQL for errors?

Only for unclosed strings, comments and parentheses. Other mistakes need a real database, or a linter that knows your dialect.

What to use next

Last reviewed . Read our editorial policy.