SQL Formatter

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

5 lines ยท 369 B

Runs entirely in your browser. Nothing you type or upload is sent to us or stored.

Results are for general guidance. Please check anything important before relying on it. Terms of use

How to Use

  1. Paste your query into Your SQL, tap Open .sql file to load one from your device, or tap Sample to try a ready-made one. Several statements separated by ; are fine.
  2. Pick the SQL dialect your database uses: MySQL, PostgreSQL, SQL Server (T-SQL), SQLite, Oracle (PL/SQL), BigQuery or standard SQL. The right dialect matters for quotes, brackets and special keywords.
  3. Choose Format / beautify, then set Keyword case (UPPER, lower or keep as typed) and the Indent. The formatted query appears on the right as you type.
  4. Choose Minify instead to remove comments and extra spaces and get a compact query for code, logs or config files.
  5. Tap Copy or Download .sql. If the query has a syntax error, a red message shows the line and column to check.

Why format SQL?

Queries copied from application logs, ORMs or old reports often arrive as one long line. Formatted SQL puts each clause (SELECT, FROM, WHERE, GROUP BY) on its own line and indents the columns and conditions under it. Now you can check joins and filters at a glance, and a Git diff shows exactly which condition changed.

This SQL formatter uses the open-source sql-formatter library and runs in your browser. Your queries, table names and data are never uploaded, which matters when the SQL has customer or production details in it.

Worked example

A one-line query from a log:

select c.city, sum(o.amount) as total from orders o join customers c on c.id = o.customer_id where o.status <> 'cancelled' group by c.city order by total desc limit 5;

With the MySQL dialect, UPPER keyword case and a 2-space indent, it becomes:

SELECT
  c.city,
  SUM(o.amount) AS total
FROM
  orders o
  JOIN customers c ON c.id = o.customer_id
WHERE
  o.status <> 'cancelled'
GROUP BY
  c.city
ORDER BY
  total DESC
LIMIT
  5;

Formatting only changes spaces, line breaks and the letter case of keywords, function names and data types. Table names, column names and everything inside quotes stay as you wrote them, so the query does exactly the same thing.

Why the dialect matters

Each database quotes names and limits rows differently. If you pick the wrong dialect, the formatter may not recognise a bracket or keyword and report an error.

DatabaseQuoted nameFirst 10 rows
MySQL / MariaDB`order date`LIMIT 10
PostgreSQL"order date"LIMIT 10
SQL Server[order date]SELECT TOP 10 โ€ฆ
Oracle (12c and later)"order date"FETCH FIRST 10 ROWS ONLY
SQLite"order date" (also accepts [ ] and ` `)LIMIT 10
BigQuery`project.dataset.table`LIMIT 10

How minify works

Minify reads the query one character at a time, so it knows when it's inside a string, a quoted name or a comment. It then:

  • removes -- line and /* block */ comments (and # comments in MySQL and BigQuery),
  • keeps optimizer hints such as /*+ INDEX(e emp_idx) */ and MySQL /*!50100 โ€ฆ */ comments, because they change how the query runs,
  • turns every run of spaces and line breaks into a single space, and removes spaces next to commas, brackets and semicolons,
  • never touches text inside 'strings', quoted names or PostgreSQL $$ โ€ฆ $$ function bodies.

SQL Server GO batch separators and MySQL DELIMITER lines are kept on their own lines, since client tools need them there.

Tips

  • UPPER-case keywords with lower-case names is the most common team style, and it makes clauses stand out.
  • Most teams use 2 or 4 spaces. Pick one and stick to it everywhere so diffs stay small.
  • Formatting doesn't check table or column names. A neatly formatted query can still fail if a column doesn't exist.
  • Stored procedures with a lot of procedural code (PL/SQL blocks, T-SQL control flow) come out reasonably well but not perfectly. Look over the result before you commit it.

Frequently Asked Questions

No. The formatter only changes spaces, line breaks and the letter case of keywords, functions and data types. Table names, column names and text inside quotes are left exactly as written, so the query returns the same result.

The one your query runs on: MySQL or MariaDB, PostgreSQL, SQL Server (T-SQL), SQLite, Oracle (PL/SQL) or Google BigQuery. Use Standard SQL if you're not sure. The dialect tells the formatter how names are quoted, for example `name` in MySQL and [name] in SQL Server.

Usually because of an unclosed bracket or quote, or because the query uses syntax from a different database than the one selected. The message shows the line and column where the formatter got stuck. Check that spot, or switch the dialect, for example to SQL Server for queries with [brackets] or TOP.

Yes. Formatting and minifying happen in your browser, in JavaScript. Nothing you paste is uploaded to a server or saved, so customer data, table names and credentials in the query stay on your device.

Comments and extra whitespace. The query ends up on one line. Optimizer hints like /*+ INDEX(...) */, MySQL /*! ... */ comments, text inside quotes, SQL Server GO lines and MySQL DELIMITER lines are kept, so the minified query behaves the same.