SQL Formatter & Beautifier

Turn a one-line query copied from a log or an ORM debug output into something you can read and review. It is a lightweight, rule-based formatter that works best on ordinary single statements.

Why format SQL?

Production SQL rarely arrives tidy. ORMs emit single-line statements, log files squash whitespace, and hand-written queries accumulate inconsistent casing over years. Formatting puts the main clauses on their own lines and uppercases keywords, which makes it much easier to see what a query really does — and to spot the missing WHERE clause before you run it.

What the formatter does and does not do

The tool collapses whitespace, uppercases a fixed list of common keywords (SELECT, FROM, JOIN, WHERE, GROUP BY, ORDER BY, CASE, WITH and similar), starts a new line before main clauses and puts a line break after every comma. A Minify button collapses the query to one line. It does not parse SQL: there is no sub-query indentation, no special handling for MERGE or DDL layout, and no dialect selection, although syntax it does not recognise (:: casts, backtick or bracket identifiers) passes through as written.

Two cautions. Keywords inside string literals are uppercased as well ('select' becomes 'SELECT'), so literal values can change. And because all line breaks are collapsed first, a -- comment can swallow the rest of the statement; remove line comments or use /* */ before formatting.

Reading a formatted query

After formatting, scan top to bottom: the SELECT list tells you the shape of the result, FROM/JOIN shows the data sources and how they relate, WHERE filters rows, GROUP BY/HAVING aggregate, and ORDER BY/LIMIT finish.

How to use it

  1. Paste your SQL into the input box.
  2. Click Format / Beautify. Keywords are uppercased and main clauses are broken onto separate lines; Minify does the reverse.
  3. Check the output (especially string literals and comments), then use Copy to take it back to your editor.

Before and after

Input:

select u.id,u.email,count(o.id) as orders from users u left join orders o on o.user_id=u.id where u.created_at>'2026-01-01' and u.status='active' group by u.id,u.email having count(o.id)>3 order by orders desc limit 20;

In the output each main clause (SELECT, FROM, JOIN, WHERE, AND, GROUP BY, HAVING, ORDER BY, LIMIT) starts on its own line and every comma-separated item goes on a new line. Keywords are uppercased, but desc is not on the keyword list and stays lowercase, and LEFT JOIN is split over two lines.

Common use cases

What the formatter really does

This formatter is a lightweight, rule-based tidy-up, not a full SQL parser. It collapses all whitespace to single spaces, uppercases a fixed list of common keywords (SELECT, FROM, WHERE, JOIN, GROUP BY, ORDER BY, LIMIT, CASE, WITH and similar), starts a new line before the main clauses, and puts a line break after every comma. The result is highlighted and the badge shows the line count. A separate Minify button collapses the query to a single line.

Because it is pattern-based, it is best on ordinary single statements. Expect a readable query, not the exact layout a dedicated formatter or your team's style guide would produce.

Worked example

This one-line query:

select u.id, count(o.id) as n from users u left join orders o on u.id=o.user_id where u.status = 'select' group by u.id order by n desc limit 10

is formatted as:

SELECT u.id,
  count(o.id) AS n
FROM
  users u
LEFT
JOIN
  orders o ON u.id=o.user_id
WHERE
  u.status = 'SELECT'
GROUP BY
  u.id
ORDER BY
  n desc
LIMIT
  10

Things to check in the output

The example shows the limits honestly. Keywords inside string literals are uppercased too: 'select' became 'SELECT', so formatting can change the data in a literal and you should not use the output where exact string values matter. LEFT JOIN is split over two lines, desc was not in the keyword list and stays lowercase, and commas inside function calls and IN (…) lists also start new lines.

Comments need care too. All line breaks are collapsed before formatting, so a -- comment only stays harmless if a clause keyword follows it; otherwise it can swallow the rest of the statement. Remove -- comments (or switch to /* */) before formatting, and compare the result to your original in the Text Diff Checker.

When to use something else

For very long scripts, stored procedures, or when you need dialect-specific layout rules, use the formatter built into your database client or editor. This page is a fast way to make a pasted one-liner from a log readable.

Frequently asked questions

Does formatting change what the query does?

Mostly not, but it can. Whitespace and keyword casing change, and keywords inside string literals are uppercased too, so a literal like 'select' becomes 'SELECT'. A -- comment can also swallow later text because line breaks are collapsed. Check the output before running it.

Which SQL dialects are supported?

There is no dialect setting. The formatter applies the same keyword list and line-break rules to every query, so dialect-specific syntax such as :: casts or backtick identifiers is left as you wrote it, but layout may not follow your dialect's conventions.

Can it format multiple statements at once?

It will process the whole text, but it does not treat semicolons specially, so statements are not separated onto their own lines. Format one statement at a time for the cleanest result.

Is my query sent to a server?

No. Formatting runs in your browser, so table names and literals in your queries stay private.

Why did a word inside quotes change to uppercase?

The keyword pass runs over the whole text, including string literals, so a literal such as 'select' becomes 'SELECT'. Check literals in the output before running the formatted query.

How do I get the query back onto one line?

Press Minify. It collapses all whitespace into single spaces. It does not remove comments, so a line comment can still comment out the rest of the line.

Related tools

Explore all developer tools →