SQL Formatter

Beautify or minify SQL queries for MySQL, PostgreSQL, SQL Server, SQLite and Oracle

How to format a SQL query online

  1. 1

    Paste your SQL

    Paste the query in the left box, or click the wand icon to load an example. The result appears on the right as you type.

  2. 2

    Pick the dialect

    Choose MySQL, PostgreSQL, SQL Server, SQLite, Oracle or Standard SQL so quoted names, comments and strings are read the way your database reads them.

  3. 3

    Set indentation and keyword case

    Use 2 spaces, 4 spaces or a tab, and choose UPPERCASE, lowercase or "keep as typed" for keywords such as SELECT and WHERE.

  4. 4

    Copy or download

    Click Copy to paste the query into your editor, or Download .sql to save it. Click Minify to get a compact one-line version instead.

What the formatter does to your query

Each major clause starts on its own line and its contents are indented underneath. Columns in SELECT, GROUP BY and ORDER BY go one per line, joins line up under FROM, and AND / OR conditions in WHERE, HAVING and ON are split so every condition is easy to find.

Sub-queries in parentheses are indented as a block, CASE expressions are laid out with each WHEN and ELSE on its own line, and common table expressions (WITH) keep one query per CTE. Short things stay on one line: function calls such as COUNT(*), IN (1, 2, 3) lists and LIMIT 10.

  • SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT and OFFSET on their own lines
  • INNER, LEFT, RIGHT, FULL and CROSS joins aligned under FROM
  • UNION, INTERSECT and EXCEPT separating the queries they combine
  • INSERT ... VALUES with one row per line, UPDATE ... SET with one assignment per line
  • CREATE TABLE with one column or constraint per line

Dialects: MySQL, PostgreSQL, SQL Server, SQLite and Oracle

The layout is the same for every dialect; the setting changes how the text is read. MySQL and MariaDB use `backticks` for names, # for comments and backslash escapes in strings. PostgreSQL supports $$ dollar-quoted strings and the :: cast. SQL Server and SQLite read [bracketed] names, and SQL Server also reads #temp tables and @variables.

If you are not sure, Standard SQL works for most queries. Switch the dialect when a name or string looks broken in the result.

Only whitespace and keyword case change

The tool never rewrites your query: string values, table names, column names and comments are kept exactly as you wrote them. Keywords and well-known functions (COUNT, SUM, COALESCE...) follow the case you choose, while names that merely look like keywords, such as a column called "name" or "date", are left alone.

It is a formatter, not a validator. It does not know your schema and does not check the syntax against a database, but it warns you about an unclosed quote, comment or parenthesis and shows the line and column, which is usually where the typo is.

Minify: a compact one-line query

Minify removes line breaks, extra spaces and comments, producing a single line you can paste into a script, a config file or a log. Comments are dropped because a "--" comment would otherwise swallow the rest of the line. Optimizer hints such as /*+ INDEX(t i) */ are kept, since they change how the query runs.

Private by design

Your SQL is formatted in your browser and never sent to a server. That makes it safe for queries that contain table names, customer data in literals or anything else you would not paste into an online service. Your dialect, indentation and keyword case are remembered in your browser for next time.

SQL formatter FAQ

How do I format a SQL query?

Paste it in the left box. It is formatted automatically as you type; you can also click Format. Then copy the result or download it as a .sql file.

Does it work with MySQL, PostgreSQL, SQL Server, SQLite and Oracle?

Yes. Pick the dialect from the list. It affects how quoted names, comments and strings are read, for example `backticks` in MySQL, [brackets] in SQL Server or $$ strings in PostgreSQL.

Does the formatter change what my query does?

No. Only spaces, line breaks and the case of keywords change. Strings, names and comments are kept as they are, so the query runs exactly as before.

Can it fix a query with errors?

No, it formats what you give it and does not check the syntax against a database. It does warn about an unclosed quote, comment or parenthesis and points to its line and column.

What does Minify do?

It turns the query into a single line without comments or extra spaces. Optimizer hints such as /*+ ... */ are kept because they affect execution.

Is my SQL uploaded anywhere?

No. Everything runs in your browser, so you can use it with production queries. Only your dialect, indentation and keyword choices are saved locally.

Is it free?

Yes. It is free, needs no sign-up and has no limit on the size or number of queries.