What this SQL formatter does
A query copied from a log, an ORM or a chat often arrives as one long line. This SQL formatter, also called an SQL beautifier, breaks it into clauses, puts each selected column on its own line and indents joins, conditions and subqueries, so you can read the logic at a glance and spot a missing condition. It handles one query or a whole file with many statements, separated by semicolons.
The formatting is done by sql-formatter, an open source JavaScript library under the MIT license, version 15.9.0. It runs in a background thread of your browser, so the page stays responsive while a large dump is formatted, and Cancel stops a long run.
Pick your SQL dialect
Every database speaks its own flavor of SQL. MySQL quotes names with backticks, SQL Server with square brackets or double quotes, and PostgreSQL has dollar-quoted strings and the :: cast. The formatter needs to know which one it is reading. These are the dialects the library supports, named as its language docs name them:
| Group | Dialects |
|---|---|
| Common | Standard SQL, MySQL, PostgreSQL, SQL Server Transact-SQL, SQLite, MariaDB, Oracle PL/SQL |
| More | Amazon Redshift, Apache Hive, ClickHouse, Couchbase N1QL, DuckDB, GCP BigQuery, IBM DB2, IBM DB2i (marked experimental), SingleStoreDB, Snowflake, Spark, TiDB, Trino (the docs note it should also work for Presto) |
Standard SQL is the default of the library. Its docs are clear that this is not auto-detection: it supports features common to many databases and may or may not work for yours, so pick a specific dialect whenever you can. If you get a parse error, switching the dialect is the first thing to try.
The options
- Keywords: SELECT, FROM, WHERE and the other reserved words in UPPERCASE, lowercase, or kept as you wrote them. Function names and data types have their own settings under More options, so you can write SELECT with count() and VARCHAR.
- Indent: 2 or 4 spaces, or a tab per level.
- Between statements: how many blank lines separate statements in a file with several queries.
- AND, OR, XOR: at the start of the next line, which the library uses by default, or at the end of the line.
- Bracket width: an expression in parentheses that is at most this many characters long stays on one line; a longer one is split over several lines. The library default is 50.
- Dense operators: writes price*tax instead of price * tax. The docs note that AND, OR and XOR keep their spaces.
- Semicolon on its own line: puts the closing ; of each statement on a separate line.
Copy link saves these settings in the address, so a team can share one style. Your SQL is never part of the link.
Compact: one line per statement
Sometimes you want the opposite of pretty: a query on a single line for a log message, a config value or a string in code. Compact turns every run of spaces, tabs and line breaks into one space, removes spaces after an opening bracket or a comma and before a closing bracket, a comma or a semicolon, and starts each statement on a new line. Keywords keep the case you wrote them in.
Strings, quoted names and comments are copied exactly, with the quoting rules of the dialect you picked, such as doubled quotes, backslash escapes in MySQL or dollar quotes in PostgreSQL. A line comment that starts with -- still ends at a line break, because otherwise it would swallow the rest of the query. As a safety check, the tool formats both your input and the compact version with the library and compares the two results. They must match exactly, or you get a message instead of output, and nothing is changed.
When the formatter cannot read a query
If the library cannot parse your SQL, you see its message with the line and column where it got stuck, the line itself with a marker under that spot, and a Jump to error button that selects it in the input. The library README names the most common cause, a wrong or missing dialect, and lists what it does not support: stored procedures, and changing the statement delimiter to something other than a semicolon. If one part of a file trips it up, wrap that part in /* sql-formatter-disable */ and /* sql-formatter-enable */ comments, and the library copies it as it is.
A formatter checks only the layout, not whether a table exists or a query is correct for your database. For other formats, the JSON formatter pretty-prints API responses, the CSS and JS minifier shrinks style sheets and scripts, and the regex tester helps with the patterns in your LIKE and REGEXP conditions.
How to use the SQL formatter
- 1Paste your SQL query, drop a .sql file on the box or press Open .sql file.
- 2Pick the dialect of your database, then the keyword case and the indent you like.
- 3Press Format SQL, or Compact for one line per statement. Changing an option updates the formatted result; Compact uses only the Dialect.
- 4Copy the result or download it as a .sql file. If there is an error, press Jump to error and fix the spot shown.
Frequently asked questions
Is my SQL sent to a server?
No. The formatter runs in your browser, and your queries never leave your device, so you can format queries with table names or data from work. Copy link only stores your options, never the SQL.
Which dialect should I pick?
The one of your database: MySQL, PostgreSQL, SQL Server (Transact-SQL), SQLite, Oracle PL/SQL and 15 more are in the list. The sql-formatter docs say Standard SQL is not auto-detection; it covers what many databases share, so syntax like [brackets] in SQL Server or backticks in MySQL can fail there.
Why do I get a parse error for a query that runs fine?
Most often the dialect is wrong: pick your database and try again. The error shows the line and column where the formatter got stuck, and Jump to error selects that spot in your query. The library docs also say it does not support stored procedures or changing the statement delimiter, as in DELIMITER $$ blocks.
Does formatting change what my query does?
The formatter changes line breaks, indentation and spaces, and the letter case of keywords, function names and data types when you ask for it. Strings, quoted names and comments keep their text. Still, check the result before you run it on a production database.
What does Compact do?
Compact puts each statement on one line and removes extra spaces, but keeps every string, quoted name and comment exactly as it was. After compacting, the tool formats both versions and compares them; if they differ, it shows an error instead of a result.
Can I keep part of my SQL unformatted?
Yes. Put the comment /* sql-formatter-disable */ before the part and /* sql-formatter-enable */ after it. The library copies everything between them as it is, without even parsing it.