Toolstia / SQL formatter
SQL formatter
Queries copied from logs, ORMs and old scripts tend to arrive as one long line. This formatter rebuilds them into a readable layout, with each clause on its own line, columns listed one per row, joins and conditions indented, and keywords in the case you prefer. That makes a query far easier to review, debug, and compare in a code review.
Pick the dialect that matches your database from Standard SQL, MySQL, MariaDB, PostgreSQL, SQLite, SQL Server T-SQL, Oracle PL/SQL or BigQuery, then choose upper-case, lower-case or as-typed keywords and an indent of 2 or 4 spaces. The formatter changes whitespace and keyword case only, and does not connect to a database or run anything.
How it works
- Paste your SQL or press Use sample.
- Pick the dialect that matches your database.
- Copy the formatted query.
Pick the right dialect
Databases write some things differently, such as quoting names and special functions. Choosing MySQL, PostgreSQL, SQL Server or another dialect helps the formatter understand your query correctly.
Keyword case
Many teams write keywords such as SELECT and WHERE in capitals so they stand out from table and column names. You can also choose lowercase or leave them as typed.
Formatting only
The tool changes layout, never the meaning of your query. It does not run the SQL, connect to any database or check that tables exist.
Errors
If the SQL contains something the formatter cannot read, it shows a short message with the position. Try a different dialect if your query uses features special to one database.
Before and after on a real query
The sample is 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=1 group by u.id,u.name having count(o.id)>2 order by orders desc limit 10; on a single line. With upper-case keywords and a 2-space indent it becomes SELECT on its own line followed by u.id, u.name and count(o.id) AS orders each on a separate row, then FROM, a LEFT JOIN with its ON condition, WHERE, GROUP BY, HAVING, ORDER BY and LIMIT, each starting a new line.
Notice that count stays in lower case while AS, LEFT JOIN and the clause words are capitalized: function names are treated as names, not keywords. Operators gain spaces around them, so o.user_id=u.id becomes o.user_id = u.id.
Why the dialect setting changes the result
Dialects differ in how they quote names and what symbols they allow. MySQL uses backticks around identifiers, SQL Server uses square brackets such as [dbo].[t], and PostgreSQL uses double quotes and operators like ->> for JSON and $1 style placeholders. When the selected dialect does not know a construct, the formatter can fail or produce odd spacing.
If a query formats badly or reports an error, switch the dialect before assuming the SQL is wrong. Standard SQL is a reasonable first guess for simple SELECT statements.
Comments, strings and several statements
Line comments written with two dashes are kept, and a comment placed after a condition stays on that line. String contents are never modified, so a literal such as 'Active' keeps its case even when you switch keywords to upper or lower.
When you paste several statements separated by semicolons, each one is formatted separately and a blank line is placed between them, so select 1; select 2; turns into two short blocks.
Reading the error message
An unclosed quote, for example SELECT 'unterminated FROM t, produces a message similar to Parse error: Unexpected token at line 1 column 8, giving the position where the formatter gave up. The usual causes are a missing closing quote or bracket, a dialect-specific keyword, or text that is not SQL at all.
Fix the position named in the message first, since later errors often disappear once the earlier one is corrected.
What formatting will not do
It will not optimize the query, check column names, or reveal logic mistakes, and it cannot minify SQL to one line. Teams that format SQL consistently find that diffs get smaller, since a changed condition touches one line instead of rewriting a paragraph. If your project has a house style, check the keyword case and indent settings match it before pasting the output into version control.
Questions
Is my query sent anywhere?
No. Formatting happens in your browser, which matters when queries contain business data.
Can it format several statements?
Yes, separated by semicolons.
Does it support stored procedures?
Oracle PL/SQL and SQL Server T-SQL are supported for common cases.
Will it make my query faster?
No. It only changes how the query looks.
Does formatting ever change what a query returns?
It changes only whitespace and the case of keywords. Quoted text and names are left alone, so the meaning of the query stays the same.
Why are function names like COUNT not capitalized?
Function names are treated as identifiers, so they keep the case you typed. Keywords such as SELECT and WHERE follow your keyword setting.
Which option should I pick for the keyword case?
Upper case is the most common convention in shared code. Choose as typed if you want to keep the case of an existing codebase untouched.
Can I format a stored procedure or a block of statements?
Simple cases in T-SQL and PL/SQL work. Complex procedural code may produce errors or uneven layout.
Why does a trailing semicolon end up on its own line or after the last item?
The semicolon is attached to the end of the last clause of each statement, such as after the LIMIT value, and then a blank line separates statements.