Quick answer
A SQL formatter rewrites the whitespace and letter case of a query so its structure is obvious: one clause per line, indented subqueries and CTEs, consistent keyword case. Unless you change identifier case, it never changes what the query does. Paste SQL into Playxoft's free SQL Formatter, pick your database dialect, then choose keyword case and indent: the result updates as you type, Minify compresses it instead, and errors show the exact line and column.
- 12 SQL dialects
- Keyword and identifier case
- 2, 4 or tab indent
- Safe minify
- Line:column errors
- Open and download .sql files
How to use the SQL Formatter
- 1Paste your SQL, drop a .sql file onto the editor, or click Load example.
- 2Pick your database under Dialect so strings, quoted identifiers and comments are read correctly.
- 3Choose Keywords case and Indent. Open More options for identifier case, tabular alignment and where logical operators break.
- 4Switch Mode to Minify to collapse the query onto one line, with Strip comments if you want it smaller still.
- 5Copy the result, download it as query.sql, or press Ctrl + Enter in the editor to copy without reaching for the mouse.
What does a SQL formatter do?
A SQL formatter reads your query as a stream of tokens (keywords, identifiers, strings, operators, comments) and prints it again with consistent line breaks and indentation. Only whitespace and, if you ask for it, the letter case of keywords change. String literals, quoted identifiers and comments come out exactly as they went in, so the formatted query returns the same rows.
-- Before
select u.id,u.email,count(o.id) orders from users u left join orders o on o.user_id=u.id where u.active group by 1,2
-- After
SELECT
u.id,
u.email,
count(o.id) orders
FROM
users u
LEFT JOIN orders o ON o.user_id = u.id
WHERE
u.active
GROUP BY
1,
2Formatting is also a cheap syntax check: if the formatter cannot parse a statement, you get the line and column of the first token it could not place, which is usually an unclosed quote, a stray parenthesis or the wrong dialect.
Should SQL keywords be uppercase or lowercase?
Both are correct: SQL keywords are case-insensitive in every major database. The choice is about readability and team habit.
- Uppercase (
SELECT,LEFT JOIN) is the traditional style and what the widely shared SQL Style Guide recommends. Keywords stand out from table and column names even in plain text, in logs and in code review tools without syntax highlighting. - Lowercase (
select,left join) is popular in analytics and application code, where editors already color keywords and capitals feel like shouting.
Pick one and apply it everywhere, which is exactly what a formatter is for. Be more careful with identifier case: PostgreSQL folds unquoted names to lowercase, but MySQL table names can be case-sensitive on Linux, so changing them can break a query. Leave Identifiers on *Keep as written* unless you know your database ignores case.
Leading or trailing commas?
Trailing commas put the comma at the end of each line; leading commas put it at the start of the next:
-- Trailing (this formatter's output)
SELECT
id,
email,
created_at
FROM users
-- Leading
SELECT
id
, email
, created_at
FROM usersLeading commas make it easy to comment out the last column and keep diffs to one line when you append one. Trailing commas read like ordinary prose and are what most formatters and style guides produce, so this tool uses them. Whichever you choose, never leave a comma before FROM: most databases reject it.
How should CTEs and subqueries be formatted?
Treat each common table expression (WITH name AS (…)) like a small named query: the name and AS ( on one line, the body indented one level, the closing parenthesis back at the margin, and a comma before the next CTE. The final SELECT starts at the left margin so the reader can find the query's output immediately.
WITH
paid AS (
SELECT customer_id, sum(total) AS revenue
FROM orders
WHERE status = 'paid'
GROUP BY customer_id
)
SELECT c.name, p.revenue
FROM customers c
JOIN paid p ON p.customer_id = c.idSubqueries follow the same rule: indent the inner query one level deeper than the clause that contains it. Short expressions such as IN (1, 2, 3) stay on one line; raise or lower Inline width under More options to control when they break.
How do SQL dialects differ?
The core of SQL is shared, but the details a formatter must understand are not. Choosing the right Dialect stops it from misreading a quote or a comment:
| Feature | PostgreSQL | MySQL / MariaDB | SQL Server | BigQuery | SQLite |
|---|---|---|---|---|---|
| Quoted identifier | "name" | backticks | [name] | backticks | "name" or [name] |
| Limit rows | LIMIT 10 | LIMIT 10 | TOP 10 or FETCH NEXT | LIMIT 10 | LIMIT 10 |
| Auto-increment | GENERATED ALWAYS AS IDENTITY (or serial) | AUTO_INCREMENT | IDENTITY(1,1) | none | INTEGER PRIMARY KEY (AUTOINCREMENT optional) |
| Cast | x::int or CAST | CAST(x AS SIGNED) | CAST, CONVERT | CAST, SAFE_CAST | CAST |
| Extra comment style | none | # comment | none | # comment | none |
| Function bodies | $$ … $$ | BEGIN … END | BEGIN … END | BEGIN … END | not supported |
Even string concatenation differs: a || b in PostgreSQL, SQLite and BigQuery, CONCAT(a, b) in MySQL (where || means OR unless PIPES_AS_CONCAT is set) and a + b or CONCAT(a, b) in SQL Server (SQL Server 2025 and Azure SQL also accept ||). Standard SQL works for portable queries. If you see an "Unexpected" error on code that runs fine in your database, switching the dialect is the first thing to try.
Is minifying SQL safe?
Yes, when it is done on tokens rather than with a regular expression. The minifier here collapses whitespace only between tokens, so the inside of every string, quoted identifier and dollar-quoted body is untouched. Line comments are kept with their line break (otherwise -- note would swallow the rest of the query), or removed entirely with Strip comments. Optimizer hints such as /*+ INDEX(users) */ and MySQL conditional comments such as /*!50100 … */ are always kept, because they change how the query runs.
Minified SQL is handy for log lines, for embedding a query in a JSON or YAML config, or for comparing two queries that differ only in layout. For anything a person will read, format it instead.
Frequently asked questions
Last updated by the Playxoft engineering team.