How to format an SQL query
- 1.
Paste the query
Paste SQL into the left box, open a .sql file or insert the sample.
- 2.
Choose the dialect
Pick your database (PostgreSQL, MySQL, SQL Server…) so that its specific syntax is recognised.
- 3.
Set the style
Choose keyword case (UPPER, lower, preserve) and indentation (2 spaces, 4 spaces or tab).
- 4.
Copy the result
Copy the formatted query or download it as a .sql file.
What well-formatted SQL looks like
A readable query puts each main clause on its own line (SELECT, FROM, JOIN, WHERE, GROUP BY, ORDER BY), indents columns and conditions by a fixed amount and writes keywords consistently - usually in upper case. This one-liner:
select id,name from users where active=1 and city='Boston' order by namebecomes:
SELECT
id,
name
FROM
users
WHERE
active = 1
AND city = 'Boston'
ORDER BY
nameFormatting does not change what the query does: only whitespace and the case of keywords, functions and data types change. String literals and quoted identifiers stay exactly as they were.
Supported SQL dialects
| Database | Syntax recognised only in that dialect (examples) |
|---|---|
| PostgreSQL | $$…$$, ::int, RETURNING, ILIKE |
| MySQL / MariaDB | `name`, # comments, LIMIT 10, 20 |
| SQL Server (T-SQL) | [Order Details], TOP 10, GO |
| Oracle (PL/SQL) | CONNECT BY, :variable, BEGIN … END; blocks |
| SQLite, BigQuery, Snowflake, Redshift, Db2, DuckDB, ClickHouse, Trino, Spark, Hive | their own functions and data types |
Standard SQL mode handles ANSI syntax. If the query uses database-specific constructs and formatting fails, select the exact dialect.
Minify without breaking strings
Minify writes the query on a single line - handy when embedding SQL as a string in code, config files, logs or CI parameters. It collapses whitespace into single spaces and removes spaces around , ( ) ;, while keeping string literals, quoted identifiers, $$…$$ blocks and T-SQL [names] untouched. Comments are removed, or - if you keep them - -- comments become /* … */ so they do not comment out the rest of a one-line query. Optimizer hints /*+ … */ (Oracle, MySQL) and /*! … */ (MySQL) are always kept. Minify does not validate syntax.
Common causes of formatting errors
- Unclosed quote - e.g.
WHERE name = 'O'Brien'. Escape an apostrophe by doubling it:'O''Brien'. - Another database's syntax -
[column]is SQL Server,`column`is MySQL; Standard SQL mode may reject both. - Template placeholders -
{{ var }}or${id}are not SQL; replace them with a value such as1while formatting. - Incomplete fragment - a lone
AND x = 1without the start of the query is often unrecognisable.
On error you get the line, column and a code excerpt with a caret. For API responses, use the JSON formatter; to compare two versions of a query, try the diff checker.
Frequently asked questions
Are my queries sent to a server?
+
Does formatting change what the query does?
+
Should SQL keywords be upper or lower case?
+
Which dialect should I use for SQL Server?
+
Why do I get an error when the query runs fine in my database?
+
Can I format several statements at once?
+
Updated: