How to Format SQL: A Readable Style Guide
· 2 min read
A long query on a single line is hard to read. You can't see where the joins end, which conditions apply, or which column a change touches. A consistent layout fixes that and makes queries easier to review.
This guide sets out a readable style for SQL and explains each choice. To apply it, paste a query into the SQL Formatter.
From one line to a readable layout
Here is a typical query as people often paste it. It is valid, but it sits on one line:
select u.id, u.name, count(o.id) as orders from users u left join orders o on o.user_id = u.id where u.active = true and o.created_at > '2026-01-01' group by u.id, u.name order by orders desc limit 10;
This is the formatter's output with the default settings and the PostgreSQL dialect:
SELECT
u.id,
u.name,
count(o.id) AS orders
FROM
users u
LEFT JOIN orders o ON o.user_id = u.id
WHERE
u.active = TRUE
AND o.created_at > '2026-01-01'
GROUP BY
u.id,
u.name
ORDER BY
orders DESC
LIMIT
10;
The style, rule by rule
- Each clause starts a line. SELECT, FROM, WHERE, GROUP BY, ORDER BY and LIMIT each begin their own line.
- Items under a clause are indented, so you can see at a glance which columns, tables and conditions belong where.
- One column per line. Diffs show exactly which column changed, and you can comment one out easily.
- AND and OR start the line, so conditions line up. The formatter can put them at the end of the line instead if your team prefers.
- Keywords are upper case, so SELECT, AS, TRUE and DESC stand out from table and column names.
Keyword case is a style choice
Upper case is the long-standing convention and separates keywords from names. Many teams now prefer lower case because it is easier to type. Either is fine; mixing both is what makes a query hard to scan, so pick one and use it everywhere.
The formatter's options cover this: keyword case (upper, lower or as written), function case, indent (2 spaces, 4 spaces or a tab), and removing comments.
What formatting changes
Formatting changes only whitespace, line breaks and the case of keywords. Table and column names, strings and comments stay exactly as written: the date string '2026-01-01' is the same before and after. The query runs the same either way.
Choose your database's dialect
Dialects differ in quoting, parameters and keywords. SQL Server uses square brackets and TOP; PostgreSQL uses $1 parameters and the ->> operator; MySQL uses backticks. Standard SQL rejects some of this vendor syntax, so choose the dialect your database uses.
The formatter supports Standard SQL, PostgreSQL, MySQL, MariaDB, SQLite, SQL Server (T-SQL), Oracle (PL/SQL), BigQuery, Snowflake, Amazon Redshift, Spark SQL, Trino and Presto, DuckDB, ClickHouse, IBM Db2 and Hive.
If a query that runs fine in your database gives an error, the dialect is usually wrong. Otherwise, look for an unclosed quote or bracket at the position shown.
Several statements, and privacy
Paste several statements separated by semicolons and each is formatted in turn, with a blank line between them.
The formatter runs in your browser, so queries containing table names, customer data or credentials never leave your device.
Try it
Paste your longest one-line query into the SQL Formatter, choose your dialect, and set keyword case and indent to match your team's style.