SQL Formatter
Format and indent SQL for any dialect.
About SQL Formatter
An SQL formatter indents and line-breaks a query so it reads clearly. Pick the dialect, keyword case and indent; the result updates as you type.
How to use
- Paste a query or load a
.sqlfile. The formatted SQL appears as you type. - Choose the dialect: Standard SQL, MySQL, PostgreSQL, SQLite, SQL Server (T-SQL), Oracle (PL/SQL), BigQuery, Snowflake or Spark SQL. The dialect decides which keywords, operators and quoting styles are recognised.
- Pick the keyword case: UPPER (default), lower, or Keep to leave keywords as written. Identifiers and string literals are never changed.
- Pick the indent: 2 spaces, 4 spaces or a tab. Copy the result or download it as
.sql, named after the loaded file.
What it does
- Uses the
sql-formatterlibrary: each clause (SELECT, FROM, WHERE, GROUP BY, ORDER BY, JOIN) starts on its own line, selected columns go one per line, and subqueries and parentheses are indented. - Formats several statements at once; each ends with a semicolon on its own line.
- Keeps comments, both
-- lineand/* block */, in place. - Reports a parse error, with the offending token and position, when the query does not fit the chosen dialect. Trying another dialect often fixes an error caused by a vendor-specific keyword.
- Does not run or validate the query against a database, and does not rewrite it: nothing is reordered, aliased or optimised.
Example
select id,name from users where active=1 and role in ('admin','owner') order by name
→
SELECT
id,
name
FROM
users
WHERE
active = 1
AND role IN ('admin', 'owner')
ORDER BY
name
Privacy
Formatting runs in the browser. No request is made, and nothing is stored.
Frequently asked questions
- Should SQL keywords be uppercase?
- It is convention, not a requirement: SQL keywords are case-insensitive in every major database. Uppercase keywords make the structure of a query stand out from table and column names, which is why most style guides and this formatter default to it.
- Why does the formatter report an error on a query that runs fine?
- The parser follows one dialect at a time. A query that uses vendor syntax, for example MySQL backticks, T-SQL square brackets or PostgreSQL casts, needs that dialect selected. Switch dialects and the error usually goes away.
- Does formatting change what the query does?
- No. Only whitespace, line breaks and keyword case change. Identifiers, string literals, numbers and comments are left as written, so the formatted query is equivalent to the original.
- Can I format a file with many statements?
- Yes. Statements separated by semicolons are formatted one after another, each ending with its semicolon on its own line. Files up to 5 MB can be loaded.