How to Format an SQL Query for Readability
Last updated: · 3 min read
A long SELECT statement written on a single line is hard to read, debug and share with a team. The SQL Formatter splits the query at its main clauses such as SELECT, FROM, WHERE, GROUP BY and ORDER BY, and indents the conditions beneath them. Standard SQL, MySQL, PostgreSQL and SQL Server (T-SQL) dialects are supported. You can also convert keywords to uppercase so they stand out from table and column names. Formatting runs in your browser, so your query is never sent to a server.
Formula
1. Split the query into tokens (keyword, identifier, literal, operator) according to the rules of the chosen dialect. 2. Start each major clause keyword (SELECT, FROM, WHERE, GROUP BY, ORDER BY, JOIN) on a new line. 3. Write the column list and conditions inside a clause one level deeper, indented by two spaces. 4. Place every column that follows a comma on its own line. 5. With uppercase keywords selected, only keywords are converted; identifiers and string literals stay as they are. 6. Queries separated by semicolons are formatted one after another.
This is not a mathematical formula but a tokenising and layout algorithm. Parsing uses the sql-formatter library, and the lexical rules vary between Standard SQL and each dialect. Formatting only changes whitespace and keyword case, so the meaning of the query is unchanged.
What the inputs mean
- SQL
- The SQL query to format. If you have several queries, separate them with semicolons. It can be on one line or irregularly indented.
- SQL dialect
- The database the query runs on: Standard SQL, MySQL, PostgreSQL or SQL Server (T-SQL). The dialect is needed to read some dialect-specific syntax correctly.
- Keywords
- 'Uppercase' converts keywords such as SELECT, FROM and WHERE to capitals; 'Keep as is' leaves the original spelling.
Worked example: Formatting an order summary query
- Input: SELECT name, COUNT(*) AS n FROM orders WHERE total > 100 GROUP BY name ORDER BY n DESC; dialect Standard SQL, keywords uppercase.
- Each main clause starts on its own line: SELECT, FROM, WHERE, GROUP BY and ORDER BY.
- The column list name and COUNT(*) AS n is written on two lines, indented by two spaces.
- The table name orders and the condition total > 100 are unchanged; only the keywords are uppercased.
Result: The query now spans eleven lines, and its structure can be read at a glance.
How to read the result
Formatting does not change the logic of a query, but still test the result on a non-production database before running it on live data. The dialect matters, because the same text can behave differently on different systems. The uppercase option changes only spelling and never touches table or column names. This is general information.
Common mistakes
- Choosing the wrong dialect; a PostgreSQL-specific syntax read as Standard SQL can produce an unexpected layout.
- Forgetting the semicolon between queries; the formatter separates statements with it, so missing semicolons merge them.
- Assuming the uppercase option also changes table and column names; only keywords are converted.
- Running a formatted query on production data without testing it; the layout is correct, but the result still needs checking.
- Sharing a query that contains a password or connection string without removing the secret first.
Frequently asked questions
Does formatting change the result of a query?
No. Only whitespace, line layout and keyword case change; the structure and the names stay the same.
Which dialect should I choose?
Choose the database where you will run the query: PostgreSQL, MySQL, SQL Server (T-SQL) or Standard SQL.
Why uppercase keywords?
Uppercase keywords make the structure stand out from table and column names. It is a convention rather than a requirement, because SQL keywords are case-insensitive.
Is my query sent to a server?
No. Formatting runs in your browser and the query is not transmitted anywhere.
The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.