Every developer knows the moment. You pull a query out of application code or a log file, and it arrives as one unbroken line: selects, joins, filters and ordering all crushed together. The query works, probably, but reading it is archaeology. Which tables does it touch? Where does the filtering happen? Why is it slow, or why did it return the wrong rows?
Formatting is how you make that structure visible. This guide shows how to format a query with the free SQL Formatter on Ovatly, what the formatting changes and what it never touches, and why the formatted version is the one you want in front of you when you are debugging a join or unpicking a nested subquery.
Contents
- Quick answer
- What formatting changes, and what it never touches
- Why layout matters when you debug
- Dialects: pick the right one
- How to format SQL, step by step
- Worked example: one line in, structure out
- Limitations and honest expectations
- Common mistakes
- FAQs
- Conclusion
Quick answer
Open the SQL Formatter, paste your SQL statement into the input box, choose the dialect your database uses, and press Format SQL. The output has keywords in upper case and clauses on their own lines, ready to copy or download. Formatting changes layout and keyword case only: your identifiers, values and string literals are left exactly as written, and the statement your database receives is equivalent to the one you pasted.
What formatting changes, and what it never touches
The formatter makes two kinds of change. It rearranges layout, breaking the statement so each clause sits on its own line with the details indented under it. And it normalises keyword case, so select, from and where become SELECT, FROM and WHERE. That is the whole job.
Everything that carries meaning is left alone. Your identifiers, meaning table and column names, stay exactly as written. Your values and string literals stay exactly as written. The formatter is not editing your query; it is typesetting it. A formatted query is faster to review, easier to diff in code review, and much easier to debug than a one-line string copied out of application code, precisely because the words themselves are unchanged and only their arrangement is new.
Why layout matters when you debug
A join bug is usually a structure bug: a join condition that references the wrong table, a filter that landed in the wrong clause, a subquery whose scope is wider than you thought. In a one-line query those faults hide inside the stream of words. With each clause on its own line, the skeleton of the query stands out, and you can check clause by clause what the statement actually does before you run it again.
The same clarity helps in nested queries. Indentation shows which SELECT belongs to which level, so you can read the innermost query first and work outward, instead of counting parentheses in a paragraph. And when a colleague reviews your change, a formatted query diffs cleanly: the altered line is visible, rather than the entire statement showing as changed because it is a single line.
There is a smaller daily benefit too. Queries pasted into tickets, documentation and chat threads stay readable months later, when nobody remembers writing them. A one-line string forces the next reader to format it before they can even start thinking about it. The formatted version is the one worth saving, because it is the one the next person can actually use.
Dialects: pick the right one
SQL is a family, not a single language, and the formatter covers eight dialects, including PostgreSQL, MySQL and SQL Server. Choosing the one your database uses is not a cosmetic step. Some syntax only parses correctly in its own dialect: MySQL backticks around identifiers and T-SQL brackets are the examples the tool itself calls out. Feed a statement to the wrong dialect and it may fail to format, or format in a way that does not reflect how your database reads it. Match the selector to the database the query will run against.
How to format SQL, step by step
- Copy the query from your code, log or database client, exactly as it stands.
- Open the SQL Formatter and paste the statement into the input box. You can paste several statements together; they are formatted in sequence, separated by blank lines.
- Choose the dialect your database uses.
- Press Format SQL.
- Read the formatted version through once, checking the clause structure against what you intended, then copy or download it.
Worked example: one line in, structure out
Here is the example the tool itself uses. The input is a single line:
select id, name from users where id = 1 order by name
The formatted output is:
SELECT
id,
name
FROM
users
WHERE
id = 1
ORDER BY
name
Nothing about the query changed. The same columns, the same table, the same filter, the same ordering. What changed is that the four clauses now sit on their own lines, the keywords stand out in upper case, and the column list is indented under its clause. On a query this small the gain is modest; on a real query with three joins and a subquery, this is the difference between reading the statement and decoding it.
Limitations and honest expectations
The formatter does not connect to any database. It cannot tell you whether the query will run, whether the tables and columns exist, or whether it will return rows. A beautifully formatted query can still be wrong, slow or pointed at the wrong table; formatting is a reading aid, not a check on correctness or performance.
It also depends on the input being a complete, parseable statement in the dialect you selected. An unfinished statement, or syntax borrowed from a different dialect, is the usual reason a query fails to format.
Common mistakes
- Choosing the wrong dialect. Backtick-quoted MySQL identifiers or T-SQL bracket syntax under the wrong selector is the most common cause of failure. Match the dialect to the database.
- Expecting formatting to fix the query. Layout changes cannot repair a wrong join or a bad filter. They only make the fault easier to see.
- Assuming a format failure means the query is broken. Usually it means the statement is unfinished or in another dialect. Check both before rewriting anything.
- Reformatting by hand afterwards. If you adjust the output manually, your copy drifts from what the tool produces. Paste and format again instead, so the style stays consistent.
- Pasting a fragment and judging the tool. A lone WHERE clause is not a statement. Paste the whole query.
FAQs
Does formatting change what the query does? No. It changes layout and keyword case only. The statement your database receives is equivalent.
Can it format multiple statements at once? Yes, paste them together and they are formatted in sequence, separated by blank lines.
Why did my query fail to format? Usually an unfinished statement or syntax from a different dialect. Check the dialect selector matches your database.
Can the formatter tell me whether my query will run? No. It does not connect to any database, so it cannot tell you whether the query will run or return rows. It only lays the statement out.
Is my SQL uploaded anywhere? No. The formatter runs locally in your browser, so your queries never leave your device.
Conclusion
A formatted query is the same query, made readable: keywords in upper case, clauses on their own lines, identifiers and values untouched. Keep the SQL Formatter next to your debugging sessions and code reviews, pick the dialect that matches your database, and let the structure show you where the problem lives. The same change-the-layout, never-the-meaning approach works on the stylesheet side too, as our guide on how to minify CSS without breaking your styles explains, and the Developer Tools archive collects the guides for the rest of the category.