We use cookies

We use cookies and similar technologies to enhance your browsing experience, analyze site traffic, and personalize content and ads. By clicking "Accept", you consent to our use of cookies. Learn more in our Privacy Policy.

SQL

SQL Formatter

by Cosmovex

Indent:
SQL Input
SQL Output
Output will appear here

Free SQL Formatter & Minifier

Format and prettify SQL queries with proper indentation and keyword highlighting. Minify SQL to compress queries. No signup required.

More Developer Tools

View all tools →
{ }
JSON Formatter
Format, validate & explore JSON
YAML Formatter
Lint, format & convert YAML
</>
XML Formatter
Beautify & validate XML
CSV Formatter
View & format CSV tables
M↓
Markdown Preview
Live Markdown rendering
64
Base64
Encode & decode Base64 strings
JWT
JWT Decoder
Decode & inspect JWT tokens
%20
URL Encoder
Encode & decode URLs

SQL Formatter & Beautifier takes a SQL query written as one long line (or with inconsistent indentation) and rewrites it with predictable line breaks, indentation, and keyword casing so you can actually read it. Paste a SELECT pulled from an ORM log, a stored procedure, or a query someone sent you in chat, and it returns a clean, syntax-highlighted version you can scan in seconds.

It runs entirely in your browser. The query you paste is parsed and reformatted by JavaScript on the page, so nothing is sent to a server and nothing is stored. That matters for SQL specifically, because real queries often contain table names, column names, and sometimes literal values that reveal how a production database is shaped. You can paste a query straight from a log file without worrying about where it ends up. There is no signup, no account, and no upload step.

How it works

You paste SQL into the input area and the formatter parses it into its component parts (keywords, identifiers, operators, string and numeric literals, comments), then reassembles it using a consistent layout. The core controls are:

  • Indentation — choose 2 spaces, 4 spaces, or a tab per nesting level. Subqueries, CASE expressions, and parenthesised groups each indent one level deeper.
  • Keyword casing — force reserved words like SELECT, FROM, WHERE, JOIN, GROUP BY to uppercase (the common convention) or lowercase. Your identifiers (users, order_id) are left exactly as you typed them.
  • Line breaks — major clauses each start on their own line, and long SELECT column lists and IN (...) sets can be broken one item per line.

The output is syntax-highlighted so keywords, strings, and comments are visually distinct. Formatting is a pure text transformation: it never changes what the query does, only how it reads. Copy the result with one click.

A worked example

Here is the kind of single-line query you get out of a query log:

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 > '2024-01-01' group by u.id, u.email having count(o.id) > 5 order by orders desc;

Formatted with 2-space indent and uppercase keywords, it becomes:

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 > '2024-01-01'
GROUP BY u.id, u.email
HAVING COUNT(o.id) > 5
ORDER BY orders DESC;

Now the join condition, the filter, and the aggregate logic are each on their own line, so you can review the query clause by clause instead of parsing one wall of text.

Common use cases

  • Reading queries from logs — ORMs and frameworks emit SQL on a single line. Formatting makes a logged slow query reviewable.
  • Code review — a beautified query in a pull request is far easier to reason about, and reviewers can spot a missing JOIN condition or an accidental cartesian product.
  • Cleaning up before committing — apply one consistent style to migrations and view definitions so the whole repo reads the same way.
  • Learning and teaching — a clearly indented query shows nesting and clause order at a glance, which helps when explaining subqueries or window functions.
  • Untangling generated SQL — query builders and BI tools produce verbose, deeply nested output; reformatting reveals the actual structure.
  • Comparing two queries — format both the same way before a diff so the only differences shown are real logic changes, not whitespace noise.

Tips and gotchas

A formatter rearranges text; it does not understand your database. Keep a few things in mind:

  • It does not validate against a schema. A typo in a column name formats cleanly. The tool checks structure, not whether tables or columns exist.
  • String literals are preserved verbatim. Text inside quotes, including SQL keywords, is never re-cased or re-indented. WHERE status = 'select' keeps the lowercase select.
  • Dialect differences exist. SELECT, JOIN, and WHERE are universal, but vendor-specific syntax (PostgreSQL :: casts, MySQL backticks, T-SQL TOP, bracketed [identifiers]) is handled as generic tokens, so it stays intact but may not get bespoke layout.
  • Comments stay where they are. -- line comments and /* block comments */ are kept, though their exact horizontal position can shift.
  • One statement or many. Multiple statements separated by ; are each formatted; very large scripts simply take a moment longer.

Why uppercase keywords is the usual convention

SQL is case-insensitive for keywords: select, SELECT, and SeLeCt all parse identically. So why do most style guides put reserved words in uppercase? It is purely a readability convention. Uppercasing the keywords (SELECT, FROM, WHERE, INNER JOIN) makes the structure of a statement pop out from the data (your table and column names, which stay lowercase or snake_case). When you scan a long query, your eye lands on the clause skeleton first.

This is the same reasoning behind indenting subqueries: the language does not require it, but a human reading the query benefits enormously. The formatter applies the casing rule only to recognised reserved words. It deliberately leaves identifiers untouched, because object names can be case-sensitive depending on the database and quoting ("MyTable" in PostgreSQL is not the same as mytable). Re-casing those could change meaning, so the tool never does it.

Privacy and how your query is handled

All formatting happens in your browser using JavaScript that ships with the page. Your SQL is never transmitted, logged, or stored anywhere. There is no backend that receives the text, so closing the tab is the only cleanup needed.

This is the practical reason to prefer a client-side formatter for SQL over a server-based one. Production queries leak information about your data model: table names hint at your domain, column names reveal what you collect, WHERE clauses sometimes contain real IDs or values, and join structure exposes relationships. Pasting that into a tool that uploads it means handing a stranger a map of your schema. Because this runs locally, you can format a query copied directly from a production log or an incident channel without that risk. You can verify it yourself: format a query, then disconnect from the network and format another. It still works.

Tips

  • Pick one indent style (2 spaces is common) and apply it across a repo so migrations and view definitions read consistently.
  • Format both versions of a query the same way before diffing, so the diff shows logic changes and not whitespace.
  • Use uppercase keywords to make the clause skeleton stand out from your lowercase table and column names.
  • Remember it formats but does not validate: a misspelled column or wrong table name still produces tidy output.
  • Anything inside quotes is left exactly as written, so a literal like '2024-01-01' or 'select' is never altered.
  • For a query pulled from a production log, format here rather than in a server tool so the schema details never leave your machine.

How to use SQL Formatter & Beautifier

  1. 1Paste your SQL query or script.
  2. 2Choose your formatting style and keyword casing.
  3. 3It reformats with clean indentation and highlighting.
  4. 4Copy the formatted SQL — nothing leaves your browser.

Frequently asked questions

Does formatting change what my query returns?

No. It only adds line breaks, indentation, and (optionally) keyword casing. The parsed statement is logically identical, so it returns the same rows it did before.

Will it uppercase my table and column names too?

No. Only recognised reserved keywords are re-cased. Identifiers are left exactly as you typed them, because object names can be case-sensitive depending on the database and quoting.

Does it check my SQL for errors or against my schema?

It checks structure, not validity. It cannot know whether a table or column exists, so a query with a typo'd name will still format cleanly. Use it to read SQL, not to verify it.

Which SQL dialects does it support?

Universal clauses like SELECT, JOIN, WHERE, GROUP BY, and CASE are handled for any dialect. Vendor-specific syntax such as PostgreSQL casts, MySQL backticks, or T-SQL brackets is preserved as-is, though it may not get dialect-specific layout.

Is my query sent to a server?

No. Formatting runs entirely in your browser. The SQL you paste is never uploaded, logged, or stored, which is why it is safe to paste a query straight from a production log.

Can it format multiple statements at once?

Yes. Statements separated by semicolons are each formatted independently. Large scripts work too; they just take slightly longer to process.

What happens to my comments?

Both line comments (-- ...) and block comments (/* ... */) are kept in the output. Their text is preserved, though their horizontal position may shift to fit the new indentation.

Can I choose tabs instead of spaces for indentation?

Yes. You can pick 2 spaces, 4 spaces, or a tab per nesting level, so the output matches whatever style your project uses.

← All toolsRead our guides →