Nifty Dev Tools
  1. Home
  2. SQL Formatter
Format

Paste a messy query and get it neatly indented, with keywords in the case you like, right in your browser.

Input SQL
Output

Your neatly formatted SQL will show up here.

How to use the SQL Formatter

  1. Paste your SQL into the input, press Open file, or drag a .sql file onto the input box. Press Example to just try it out.
  2. Pick the Dialect (the database the SQL is for), how Keywords should look (UPPER, lower or as written) and the Indent (2 spaces, 4 spaces or a tab). The tool remembers these choices.
  3. We read the SQL as you type. If something is off, the message below the input says what and where: the line, the column and a plain reason. Press Show me to jump to the spot; it's underlined in red.
  4. Press Format (or Ctrl + Enter, ⌘ + Enter on a Mac), then Copy or download the result. Changing an option re-formats it right away.

Example

One long line, the way a query often looks in a log or in code:

select o.id, c.name, sum(i.quantity * i.unit_price) as total from orders o join customers c on c.id = o.customer_id join order_items i on i.order_id = o.id where o.status = 'paid' and o.placed_at >= '2026-01-01' group by o.id, c.name order by total desc;

Formatted as Standard SQL with UPPER keywords and 2 spaces:

SELECT
  o.id,
  c.name,
  SUM(i.quantity * i.unit_price) AS total
FROM
  orders o
  JOIN customers c ON c.id = o.customer_id
  JOIN order_items i ON i.order_id = o.id
WHERE
  o.status = 'paid'
  AND o.placed_at >= '2026-01-01'
GROUP BY
  o.id,
  c.name
ORDER BY
  total DESC;

Each clause starts a line, the columns and conditions under it are indented, and every JOIN gets its own line.

Keyword case

Keywords changes the case of SQL's own words only: keywords like SELECT, data types like INT and built-in functions like COUNT. Names you made up, strings and anything in quotes keep every letter. This query:

Select count(*) as orders, max(placed_at) as latest from Orders where status = 'Paid'

with UPPER:

SELECT
  COUNT(*) AS orders,
  MAX(placed_at) AS latest
FROM
  Orders
WHERE
  status = 'Paid'

with lower:

select
  count(*) as orders,
  max(placed_at) as latest
from
  Orders
where
  status = 'Paid'

As written leaves every word as you typed it (Select stays Select) and changes only the layout.

What formatting changes, and what it never touches

Only the layout changes: line breaks, indentation and the spaces between words and symbols, plus keyword case if you pick UPPER or lower. Everything else is copied exactly as written:

  • Strings like 'it''s', E'tab\there' or $$ … $$, also over several lines.
  • Quoted names like "Order Total", `order total` or [Order Total].
  • Numbers, parameters (?, $1, :id, @id) and operators (::, ->>, ||).
  • Comments, with their text. A comment over several lines moves with the code as a block, and the layout inside it (indented notes, a small table) stays as you wrote it.

We check every result before showing it: the input and the output are read again, word by word, and if anything other than spacing and keyword case would differ, you get an error instead of a result. Want a part left exactly as it is? Put /* sql-formatter-disable */ before it and /* sql-formatter-enable */ after it.

Which dialect to pick

Pick the database the SQL runs on. SQL is mostly the same everywhere, but each database quotes names, writes comments and escapes strings a little differently, and the formatter has to know which way to read them:

DialectWhat it reads that others don't
Standard SQL"double quotes" for names, '' for a quote inside a string. Good when you aren't sure.
SQL Server (T-SQL)[square brackets] around names, @variables, N'unicode' strings.
MySQL, MariaDB`backticks` around names, # comments, \' inside strings, "double-quoted" strings.
PostgreSQL$$ … $$ function bodies, :: casts, E'…' strings, $1 parameters, JSON operators like ->>.
SQLite"…", `…` and […] names, ?, :name, @name and $name parameters.
Oracle PL/SQLq'[ … ]' strings, :name bind variables.
BigQuery`project.dataset.table` names, """triple-quoted""" and r'raw' strings, # comments.
Snowflake$$ … $$ strings, // comments, :: casts.
Amazon Redshift, DuckDB, Spark SQL, Trino / PrestoTheir own keywords and functions; Spark uses backticks like MySQL.

For example, select [order id] from orders is SQL Server's way to write a name with a space. As Standard SQL it stops with Hmm, something's off on line 1, column 8: Standard SQL doesn't read [square brackets] around names. Pick SQL Server (T-SQL) or SQLite if that's your database, or use "double quotes" 🤔. As SQL Server (T-SQL) it formats:

SELECT
  [order id]
FROM
  orders

A PostgreSQL function body between $$ is a string to the database, so it's kept exactly as written: only the code around it is laid out.

Common SQL formatting errors

Here's what the formatter says about the mistakes we see most (as Standard SQL unless it says otherwise), and how to fix each one.

  • A quote inside a string: select 'it's here' from orders

    It says Hmm, something's off on line 1, column 18: this string never ends. Add the closing ' 🤔

    Fix: The ' in it's ended the string early, so the quote after here starts a new one. Write a quote inside a string twice: 'it''s here' (MySQL also reads 'it\'s here').

  • A comment that never ends: select id, total /* old: , discount from orders

    It says Hmm, something's off on line 1, column 18: this /* comment never ends. Add */ where it should stop 🤔

    Fix: Close it: /* old: , discount */.

  • A bracket that's never closed: select * from orders where id in (1, 2, 3

    It says Hmm, something's off on line 1, column 34: this ( is never closed. Add a ) where it should end 🤔

    Fix: Add the ): in (1, 2, 3).

  • One bracket too many: select count(*)) from orders

    It says Hmm, something's off on line 1, column 16: this ) has no ( before it 🤔

    Fix: Delete the extra ).

  • CASE without END: select case when total > 100 then 'big' from orders

    It says Hmm, something's off on line 1, column 41: “from” doesn't fit here. A missing comma, END or ) just before it is the usual cause 🤔

    Fix: Every CASE ends with END: case when total > 100 then 'big' end.

  • Cut off at the end: select id from orders limit

    It says Hmm, something's off on line 1, column 23: the SQL stops right after “limit”, in the middle of a statement. Is something missing at the end? 🤔

    Fix: Finish it: limit 10.

  • Another database's quotes: select `order id` from orders as PostgreSQL

    It says Hmm, something's off on line 1, column 8: PostgreSQL doesn't read `backticks` around names. Pick MySQL, MariaDB, BigQuery, Spark SQL or SQLite if that's your database, or use "double quotes" 🤔

    Fix: Pick the dialect the SQL was written for (here MySQL), or write the name PostgreSQL's way: "order id".

FAQ

Is my SQL uploaded anywhere?

No. It runs in your browser, including opening files: a file you open or drop is read right here, and we never upload, log or store your SQL. The SQL Formatter remembers only your options (dialect, keyword case, indent), not your input: reload the page and it's gone. See the privacy page for details.

Does formatting change my query?

No. Only line breaks, indentation and spaces change, plus the case of keywords if you pick UPPER or lower (databases read keywords the same in any case). Strings, names, numbers, operators and comments are copied as written, and every result is checked against your input before you see it: if anything else would change, you get an error instead.

Does it check that my SQL is valid?

Not the way your database does. It's a formatter: it reads the SQL well enough to lay it out, so it finds strings, comments and brackets that never end, a CASE without END and other things it can't read. But it doesn't know your tables and columns, and it happily formats some SQL a database would reject, like a misspelled keyword (it reads it as a name). To be sure, run the query, or EXPLAIN it, on your database.

Which dialect should I pick?

The database you run the SQL on. If you aren't sure, start with Standard SQL: when it meets another database's quotes ([brackets], backticks, $$) or # comments, the error message says which dialects read them.

Why didn't a word turn uppercase?

Keyword case changes only words the formatter knows as keywords, data types or built-in functions of the chosen dialect. Names you made up keep their case on purpose (a table called Orders stays Orders; some databases treat names case-sensitively). A word that's a keyword in one database can be a plain name in another, so switching the dialect can change which words get uppercased.

Can it format stored procedures?

Plain statements work best: SELECT, INSERT, UPDATE, DELETE, CREATE TABLE, WITH and the like, also many in one file. Procedure and function bodies are laid out only roughly, a PostgreSQL $$ body is kept as written, and MySQL's DELIMITER command isn't supported (you get an error instead of a result). To keep such a part exactly as it is, wrap it in the two comments shown under “What formatting changes” above.

How big can my SQL be?

Files up to 20 MB open fine. Formatting runs in the background, so the page stays responsive: a few hundred KB take about a second, a few MB several seconds. Syntax colors switch off above 100 KB to keep typing smooth.