How to Format SQL Queries
Messy SQL is one of the fastest ways to introduce bugs. When a query is a single long line with no indentation, it is hard to see which conditions apply to which joins, where subqueries begin and end, or whether the logic is correct.
Why formatting matters
- Debugging — a well-formatted query makes logic errors visible. You can trace the flow from SELECT to WHERE to JOIN without guessing.
- Code review — reviewers can read formatted SQL in seconds. A single-line query forces them to mentally parse it first.
- Maintenance — when you revisit a query months later, formatting tells you what it does at a glance.
- Collaboration — consistent formatting across a team means everyone reads SQL the same way.
How to format SQL
- Paste your SQL — enter a minified or messy query into the formatter. It handles SELECT, INSERT, UPDATE, DELETE, CREATE TABLE, and complex queries with subqueries and joins.
- Configure options — choose indentation size and whether to uppercase keywords. These settings match your project's style guide.
- Copy the result — the formatted SQL is ready to paste back into your editor, database client, or documentation.
What good formatting looks like
A query like select u.name, o.total from users u join orders o on u.id = o.user_id where o.total > 100 and u.active = 1 order by o.total desc becomes:
SELECT
u.name,
o.total
FROM users u
JOIN orders o
ON u.id = o.user_id
WHERE o.total > 100
AND u.active = 1
ORDER BY o.total DESC
Each clause starts on its own line. Conditions are indented under their parent clause. Joins and their ON conditions are clearly paired.
Tips
- Format before committing — run your SQL through a formatter before adding it to version control. This keeps diffs clean and reviews focused on logic, not style.
- Use consistent keyword casing — pick uppercase or lowercase keywords and stick with it across your project. Mixing styles makes queries harder to read.
- Break up complex queries — if a query is still hard to read after formatting, consider breaking it into CTEs (Common Table Expressions) or views. Formatting cannot fix fundamentally complex logic.
- Check syntax highlighting — a good formatter provides color-coded highlighting that makes keywords, strings, and numbers visually distinct, which helps catch typos.
Frequently Asked Questions
Should SQL keywords be uppercase?
It is a widely followed convention to write SQL keywords in uppercase (SELECT, FROM, WHERE) and table or column names in lowercase. This makes queries easier to scan visually. Most style guides recommend it, but it is not required by any database engine.
Does formatting change how the query runs?
No. Whitespace and indentation have no effect on SQL execution. Formatting is purely for human readability. A minified query and a beautifully indented one produce the same result.
What indentation size should I use?
Two or four spaces are both common. Pick whichever your team uses and stay consistent. Most SQL formatters let you configure this.
Is my SQL sent to a server?
No. The formatting happens entirely in your browser. Your queries never leave your device.