SQL Formatter is a tool for automatically formatting SQL queries: adding indentation, moving clauses to new lines, and converting keywords to UPPERCASE. Readable SQL is easier to debug, review in code reviews, and share with colleagues.
For quick formatting, use SQL Formatter on reChecker.
Why format SQL
An unformatted query:
select u.id,u.name,o.total from users u inner join orders o on u.id=o.user_id where o.status='completed' and o.total>100 order by o.total desc limit 10The same query after formatting:
SELECT
u.id,
u.name,
o.total
FROM users u
INNER JOIN orders o ON u.id = o.user_id
WHERE o.status = 'completed'
AND o.total > 100
ORDER BY o.total DESC
LIMIT 10;The difference is obvious: the structure is visible at a glance, WHERE conditions read like a list, and JOIN is easy to find in a long query.
SQL formatting rules
1. Use UPPERCASE for keywords
The SQL standard does not require uppercase, but it is a widely accepted convention. Keywords SELECT, FROM, WHERE, JOIN, AND, OR should always be uppercase. Table names, column names, and aliases should be lowercase or camelCase, according to the project convention.
-- Плохо
select id, name from users where active = 1
-- Хорошо
SELECT id, name FROM users WHERE active = 12. Put each clause on a separate line
Start SELECT, FROM, WHERE, JOIN, GROUP BY, ORDER BY, HAVING, LIMIT on a new line:
SELECT
id,
name,
email
FROM users
WHERE active = 1
ORDER BY created_at DESC
LIMIT 20;3. Put each SELECT column on its own line
When selecting multiple columns, put each on a separate, indented line. Place the comma at the end or beginning of the line (at the end is more convenient):
SELECT
user_id,
product_id,
quantity,
price,
created_at
FROM order_items;4. Indent WHERE conditions
Put each AND / OR on a new, indented line:
WHERE
status = 'active'
AND created_at > '2026-01-01'
AND (category = 'SEO' OR category = 'Dev')5. Put JOIN on a separate line
Put each JOIN and its ON condition on separate lines:
FROM orders o
INNER JOIN users u ON o.user_id = u.id
LEFT JOIN products p ON o.product_id = p.id6. Indent subqueries further
SELECT
id,
name,
(
SELECT COUNT(*)
FROM orders
WHERE orders.user_id = users.id
) AS order_count
FROM users;7. Use AS for aliases
Explicitly using AS makes a query more readable, although it is not required by the syntax:
-- Хорошо
SELECT u.name AS user_name, COUNT(o.id) AS order_count
FROM users AS u
LEFT JOIN orders AS o ON u.id = o.user_id8. End with a semicolon
End queries with ;. This is required to execute multiple queries in succession and is a good habit in general.
Indentation size
There is no standard, but 2 or 4 spaces are the most common options. Tabs are used less often. The main thing is to be consistent within the project.
SQL minification
Minified SQL is a query without extra spaces or line breaks. It is used:
- in logs where a compact format is important
- when passing data through an API or URL
- in configuration files
-- Оригинал (4 строки)
SELECT id, name
FROM users
WHERE active = 1
LIMIT 10;
-- Минифицированный (1 строка)
SELECT id,name FROM users WHERE active=1 LIMIT 10;Formatting tools
Online
SQL Formatter on reChecker formats and minifies SQL in your browser. Data is not sent to the server.
VS Code
The SQL Formatter extensions (find them by searching in Extensions) format SQL when you save a file or use the keyboard shortcut Shift+Alt+F.
DBeaver and DataGrip
Built-in SQL formatting: Ctrl+Shift+F (DBeaver) or Ctrl+Alt+L (DataGrip).
sql-formatter library (Node.js)
import { format } from 'sql-formatter';
const formatted = format(
`select id,name from users where active=1`,
{ language: 'postgresql', tabWidth: 2 }
);
console.log(formatted);SQL dialects
Syntax differs between:
| Dialect | DBMS | Features |
|---|---|---|
| MySQL | MySQL, MariaDB | Backticks for names, LIMIT/OFFSET |
| PostgreSQL | PostgreSQL | ILIKE, window functions, :: for casting |
| T-SQL | SQL Server | TOP n, GETDATE(), NOLOCK |
| PL/SQL | Oracle | ROWNUM, CONNECT BY, NVL |
| SQLite | SQLite | Simple dialect, without RIGHT JOIN before 2021 |
A standard formatter works with generic SQL. Dialect-specific extensions are formatted as plain text.
Examples of complex queries
GROUP BY with HAVING
SELECT
category,
COUNT(*) AS product_count,
AVG(price) AS avg_price
FROM products
WHERE active = 1
GROUP BY category
HAVING COUNT(*) > 10
ORDER BY avg_price DESC;CTE (Common Table Expression)
WITH active_users AS (
SELECT id, name, email
FROM users
WHERE status = 'active'
AND last_login > NOW() - INTERVAL '30 days'
),
user_orders AS (
SELECT
user_id,
COUNT(*) AS order_count,
SUM(total) AS total_spent
FROM orders
GROUP BY user_id
)
SELECT
u.name,
u.email,
COALESCE(o.order_count, 0) AS orders,
COALESCE(o.total_spent, 0) AS spent
FROM active_users u
LEFT JOIN user_orders o ON u.id = o.user_id
ORDER BY spent DESC
LIMIT 100;CASE WHEN
SELECT
id,
name,
CASE
WHEN score >= 90 THEN 'Отлично'
WHEN score >= 70 THEN 'Хорошо'
WHEN score >= 50 THEN 'Удовлетворительно'
ELSE 'Неудовлетворительно'
END AS grade
FROM students
ORDER BY score DESC;Recommendations
- Use one style throughout the project — agree on conventions (UPPER/lowercase, comma position, indentation size) and document them in the README or
.editorconfig. - Format before code review — unreadable SQL is harder to review.
- Comment on complex queries —
-- Запрос получает активных пользователей с заказами за последние 30 дней. - Use aliases — shorten long table names to
AS u,AS o. - Break up complex queries — use CTEs instead of deeply nested subqueries.
Conclusion
Formatted SQL is a sign of professionalism. It makes debugging, code review, and collaboration easier. Use SQL Formatter to format SQL quickly without installing tools.