SQL Formatter

Format long SQL queries into readable, indented code with each clause on its own line. Pick your database dialect and keyword case.

Updated
Runs in your browser. Your data is not uploaded.
The formatter loads on first use (about 75 KB).

How to use the SQL Formatter

  1. Paste one or more queries into SQL input, or upload an .sql file.
  2. Choose the Dialect of your database, the Keyword case, and the Indent.
  3. Press Format, then copy the result or download it.

How it works

The query is split into tokens with the rules of the chosen dialect, so identifiers in backticks, brackets, or double quotes and dialect-specific operators are read correctly. Formatting uses the open-source sql-formatter library.

  • Main clauses (SELECT, FROM, WHERE, GROUP BY, ORDER BY, LIMIT) start at the left margin, and their contents are indented below them.
  • Each selected column and each AND/OR condition goes on its own line.
  • Subqueries and CASE expressions are indented one more level.
  • Keyword case applies to keywords, built-in functions such as COUNT, and data types. Table names, column names, and string values keep their case.

Examples

Input:

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.active=1 group by u.id,u.email order by orders desc limit 10

Formatted with uppercase keywords and 2 spaces:

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.active = 1
GROUP BY
  u.id,
  u.email
ORDER BY
  orders DESC
LIMIT
  10

Limitations

  • The formatter doesn't check whether the SQL is valid or run it. A misspelled column still formats without complaint.
  • Stored procedures with complex control flow (BEGIN ... END blocks, loops) are formatted, but the indentation inside them is basic.
  • Comments are kept, but a comment in the middle of an expression can move to a new line.
  • The first run loads the formatter (about 75 KB). Files are limited to 5 MB.

Frequently asked questions

Which SQL dialects are supported?

Standard SQL, MySQL, MariaDB, PostgreSQL, SQLite, SQL Server (T-SQL), Oracle PL/SQL, and Google BigQuery.

Does formatting change what my query does?

No. Only whitespace, line breaks, and the case of keywords change. Identifiers and string values are never changed.

Should SQL keywords be uppercase?

It's a style choice; databases don't care. Uppercase keywords are the most common convention because they stand out from table and column names.

Is my SQL uploaded?

No. Formatting runs in your browser, so queries that contain real data stay on your device.

Often used together with the SQL Formatter.

  • SQL Query Generator

    Generates CREATE, INSERT, SELECT, UPDATE, and DELETE statements for four databases.

  • PHP Formatter

    Re-indents PHP code safely without touching strings, comments, or inline HTML.

  • JavaScript Formatter

    Beautifies minified or messy JavaScript with your chosen indent and brace style.