Skip to content
ToolPlankDEV
Database Tools
100% In-Browser Execution

Updated:

SQL Formatter and Beautifier

Paste a query and this sql formatter lays it out as you type: one clause per line, indented subqueries, CASE blocks and joins. Pick your dialect, keyword case, comma style and line width, or minify it to one line. Nothing is uploaded.

Options:

SQL

Input
0 chars · 0 lines

Formatted SQL

Ready
Processing in browser...
0 chars · 0 linesUpdates as you type

SQL summary

Statement, token, comment and parameter counts, nesting depth and the size change appear here.

Processed on your device. Your text is not sent to our servers.

More free tools you may need next.

Quick Guide

How to use SQL Formatter

Simple step-by-step instructions to get the exact output you need.

  1. 01

    Paste or open your SQL

    Paste a query or a whole script into the left box, press Paste, or press Upload and pick a .sql file. Sample loads a one-line report query, and the other sample buttons load a CTE, an insert and update pair, a CREATE TABLE, a SQL Server query and a broken query.

  2. 02

    Pick the dialect and the style

    Choose the dialect your database uses so its quotes, comments and parameters are read correctly. Then set keyword case, indent, one column per line and AND / OR on new lines. More options holds comma style, line width, keyword alignment, function case and comment removal.

  3. 03

    Fix errors if there are any

    If a string, quoted name or comment is never closed, or a parenthesis has no partner, the message gives the line and column so you can fix it. The output updates as you type.

  4. 04

    Copy or download

    Check the summary for statement, comment and parameter counts and the size change, then press Copy, or Download to save formatted.sql or minified.sql.

What this SQL formatter does

A sql formatter takes a query that is hard to read, often one long line copied from a log, an ORM or a chat message, and lays it out so each clause starts on its own line and nested parts are indented. The query means exactly the same thing afterwards. Only the spaces and line breaks between words change.

Here is a one-line query and what the default settings give back:

sql
select c.name, count(o.id) as orders from customers c join orders o on o.customer_id = c.id where o.status = 'paid' and o.total > 50 group by c.name order by orders desc;
sql
SELECT
  c.name,
  COUNT(o.id) AS orders
FROM
  customers c
  JOIN orders o ON o.customer_id = c.id
WHERE
  o.status = 'paid'
  AND o.total > 50
GROUP BY
  c.name
ORDER BY
  orders DESC;

What is never changed

The formatter reads the SQL as a list of tokens first: words, numbers, operators, strings, quoted names, parameters and comments. It only rewrites the space between tokens, so these always come out exactly as you typed them:

  • Text inside 'strings', including line breaks and doubled quotes.
  • Quoted names such as "Order Date", `order date` in MySQL and [Order Date] in SQL Server.
  • Comments, both -- line and /* block */, unless you choose Remove comments.
  • PostgreSQL $$ ... $$ function bodies and Oracle q'[ ... ]' strings.
  • Table, column and alias names. Keyword case only touches reserved words such as SELECT, FROM and JOIN, and never a name written after a dot like t.order.

The tool also checks that reading the formatted result gives the same token list as your input. Formatting and then minifying a query always leads back to the same tokens.

The layout options explained

  • Keywords: UPPER, lower, or As typed. Names are never changed.
  • Indent: 2 spaces, 4 spaces or a tab for each nested level.
  • One column per line: puts every column, GROUP BY and ORDER BY item on its own line. Turn it off for a compact layout where items stay on the clause line.
  • AND / OR on new line: starts each condition in WHERE, HAVING and JOIN ... ON on a fresh line. The AND inside BETWEEN 1 AND 5 stays where it is.
  • Commas: trailing puts the comma at the end of the line, leading starts the next line with it, which makes it easy to comment out one column.
  • Line width: short parenthesized groups, IN lists and CASE expressions stay on one line when they fit in 60, 80, 100 or 120 characters. Longer ones are broken up. Off never wraps.
  • Align keywords: right-aligns clause keywords so the query reads down one column.
  • Uppercase functions: writes built-in functions such as COUNT(), COALESCE() and DATE_TRUNC() in capitals. Your own functions keep the case you used.

With Align keywords turned on, the same query looks like this:

sql
SELECT c.name,
       COUNT(o.id) AS orders
  FROM customers c
  JOIN orders o ON o.customer_id = c.id
 WHERE o.status = 'paid'
   AND o.total > 50
 GROUP BY c.name
 ORDER BY orders DESC;

And with One column per line turned off, it becomes a compact block:

sql
SELECT c.name, COUNT(o.id) AS orders
FROM customers c
JOIN orders o ON o.customer_id = c.id
WHERE o.status = 'paid'
  AND o.total > 50
GROUP BY c.name
ORDER BY orders DESC;

Choosing a dialect

SQL databases agree on most syntax but not on quotes, comments and parameters. The dialect setting only changes how those are read:

  • Standard SQL: 'strings', "names", plus `backtick` names and $$ bodies, so most scripts format safely without picking anything.
  • PostgreSQL: $tag$ dollar quotes, E'...' escape strings, nested block comments, :: casts and operators such as @>, <-> and !~*.
  • MySQL / MariaDB: backtick names, double-quoted strings, backslash escapes, # comments, <=> and @user variables.
  • SQL Server: [square bracket] names, N'unicode' strings, @variables, #temp tables and GO batch separators.
  • SQLite: square brackets, backticks and the ?1, :name, @name and $name parameters.
  • Oracle: q'[...]' quoting, :1 bind variables and table@dblink names.

If the SQL contains something that suggests another dialect, such as backticks while PostgreSQL is selected, the summary shows a note.

Minify SQL to one line

Minify mode removes line breaks and every space that is not needed, which is handy when a query has to fit in a config value, a log line or a string in code. It still keeps a space where removing it would join two tokens, for example between - and -1 so it never turns into a -- comment:

sql
SELECT c.name,COUNT(o.id) AS orders FROM customers c JOIN orders o ON o.customer_id=c.id WHERE o.status='paid' AND o.total>50 GROUP BY c.name ORDER BY orders DESC;

A -- comment runs to the end of its line, so minified output keeps a line break after each one. Turn on Remove comments to get a true single line.

Errors the formatter catches

Before formatting, the tool checks that every string, quoted name and block comment is closed and that parentheses are balanced. The error names the line and column. For example, a missing closing parenthesis:

Text
Line 1, column 34: This ( is never closed. Add a ) where the group should end.

It does not check table names, column names or whether the database would accept the query. For that, run it against your database or use the SQL query optimizer, which reads the query more deeply.

Limits worth knowing

  • Input is capped at 2,000,000 characters. A script of that size usually formats in about a second or less, depending on the device.
  • Procedural code in stored procedures (IF, LOOP, BEGIN ... END blocks) is kept in order and spaced, but it is not indented as blocks.
  • Pasted text uses LF line breaks on this page, so Windows CRLF endings are not kept.
  • Removing comments keeps optimizer hints such as /*+ INDEX(t i) */ and MySQL /*! ... */, because deleting them changes how the query runs.

Working with data next to your SQL? JSON to SQL turns JSON rows into INSERT statements, and SQL to JSON turns INSERT statements back into JSON.

Privacy

Your SQL is formatted in your browser. Nothing is uploaded or stored on a server. See the Privacy Policy for details.

Frequently Asked Questions

Technical Q&A for SQL Formatter

Common questions about edge cases, character encoding, and privacy guarantees.

No. It only changes spaces and line breaks between tokens, plus the case of reserved keywords if you ask for it. Strings, quoted names, comments and table names stay exactly as typed.

They are the same thing. Both names describe laying out SQL with line breaks and indentation so it is easier to read.

Set Mode to Minify. The query goes on one line with only the spaces it needs, and Remove comments drops comments too.

Standard SQL, PostgreSQL, MySQL and MariaDB, SQL Server (T-SQL), SQLite and Oracle. The dialect decides how quotes, comments and parameters are read.

Yes. Statements separated by semicolons, and SQL Server batches separated by GO, are each formatted with a blank line between them.

Only reserved words such as SELECT and WHERE change case. Changing names could break queries on databases where names are case sensitive, such as MySQL tables on Linux.

Leading commas start each new column line with a comma instead of ending the previous line with one. Many people use them so a single column can be commented out without touching the line above.

It checks that strings, quoted names and comments are closed and that parentheses balance, and gives the line and column of any problem. It does not check table or column names.

Yes. Positional and named parameters, @variables and SQL Server @@ system variables are kept as they are, and the summary lists the ones it found.

No. Formatting runs in your browser and the SQL never leaves your device.