SQL Formatter

Formatted lines—
Keywords found—
Output length (chars)—

How SQL Formatter is calculated: Keywords upper-cased and major clauses (SELECT, FROM, WHERE, GROUP BY, ORDER BY) put on their own lines, for SELECT, INSERT, UPDATE and DELETE. A basic formatter — it does not parse your query or check it against a database.

What is a SQL formatter?

A SQL formatter takes a query written on one long line — or with random spacing — and prints it in the layout people actually read: keywords in upper case, each major clause on its own line, and the selected columns listed one per line, indented. The query means exactly the same thing after formatting; only its layout changes, and the database runs it identically either way.

The need is constant. Queries copied from an application log arrive as a single line. A query written in a hurry in a database console has no line breaks at all. A query pasted from a chat message has lost its indentation. Any of those is hard to read, and a query you cannot read is a query you cannot debug, review, or trust.

The default example is the classic case: select id, name from users where age > 18 order by name; — one line, 54 characters, lower-case keywords. Formatted, it becomes 6 lines: SELECT on its own line, id and name each on an indented line, then FROM users, an indented WHERE age > 18, and ORDER BY name;. Same query, same result, far easier to follow.

This formatter covers the four statements people write most: SELECT, INSERT, UPDATE, and DELETE, with the common clauses that go with them. It is a basic formatter, and the formula line says so — it works on the text of the query and does not parse it or check it against a database. For everyday queries, that is enough; for a query with nested subqueries and window functions, it still helps, but it will not lay out every level perfectly.

How this SQL formatter works

The formatter works in three passes over the text. The first pass normalises the whitespace: runs of spaces, tabs, and newlines become single spaces, so the input is one clean line no matter how it arrived. The second pass finds SQL keywords — SELECT, FROM, WHERE, and the rest — and sets them to upper case, or lower case if you picked that style. Keywords inside quoted strings are protected, so a name like 'whereabouts' in a text value is never touched.

The third pass breaks the query into lines. Each major clause — SELECT, FROM, WHERE, GROUP BY, ORDER BY, HAVING, LIMIT, INSERT INTO, VALUES, UPDATE, SET, DELETE FROM — starts a new line. JOINs get their own lines too, so a query that joins three tables shows each join separately instead of hiding them in the middle of a line. The WHERE line is indented, which makes the filter stand out from the tables it filters.

The SELECT list gets special treatment. Instead of leaving the columns on one long line, the formatter splits them at commas — but only at commas that are not inside parentheses, so a function call like concat(first_name, ' ', last_name) stays whole on one line. Each column then goes on its own indented line. A query selecting ten columns becomes ten short lines instead of one line you have to scroll sideways to read.

The keyword case selector changes only the case, not the layout. Upper-case keywords are the long-standing convention in SQL — they make the structure words stand out from table and column names at a glance. Lower-case suits codebases that use lower case everywhere. Switch the option and the same query reformats immediately, so you can match the style your team already uses.

The numbers: lines, keywords, and length

Three numbers describe the formatted query. Lines is the count of lines in the output — 6 for the default example. Keywords is the count of recognised SQL keywords in the formatted text — 4 in the default, one each for SELECT, FROM, WHERE, and ORDER BY. Output length is the character count of the formatted query.

The keyword count is a quick check on what the query actually does. A SELECT with a JOIN, a WHERE, a GROUP BY, and an ORDER BY should show 8 keywords, and the worked example below does exactly that. If the count is lower than you expected, a clause may be written in a way the formatter does not recognise — a good reason to read the formatted output carefully rather than trusting the count alone.

Formatted SQL is almost always longer than the input, because it adds newlines and indentation. The default query is 54 characters on one line and a little longer formatted, spread over 6 lines. That is the trade formatting always makes: more characters on screen, far less effort to read. A query is read many more times than it is written, so the trade pays off quickly.

The line count also tells you how complex a query is at a glance. A query that formats to 6 lines is simple. One that formats to 40 lines has several joins, a subquery, or a long column list, and deserves a slower read. When a query formats to far more lines than you expected, it is usually because a comma inside a column list split something you thought was one item — worth a look before you run it.

A worked example: three queries, formatted

Start with the default: select id, name from users where age > 18 order by name;. It formats to 6 lines and 4 keywords. Read it top to bottom and it tells a small story: take the id and name columns, from the users table, where age is over 18, ordered by name. That story is invisible when the same words sit on one line.

Next, an insert: INSERT INTO orders (id, total) VALUES (1, 99.50);. It formats to 2 lines and 2 keywords — the INSERT INTO line with the table and columns, then the VALUES line with the data. Short statements stay short, which is exactly right: formatting should not pad a two-line query into ten lines.

Then a join, the case where formatting earns its keep: select u.name, count(o.id) from users u join orders o on u.id = o.user_id where u.age >= 21 group by u.name order by count(o.id) desc;. Formatted, it is 8 lines and 8 keywords — SELECT with two columns, FROM users u, JOIN orders o on its own line, an indented WHERE, GROUP BY u.name, and ORDER BY count(o.id) DESC. Each clause is visible, each join is separate, and the filter is impossible to miss.

One more, to show the comma rule: select concat(first_name, ' ', last_name) as full_name from users;. The formatter splits the column list at top-level commas only, so the commas inside concat() do not split the function call. The result is a clean two-column-style listing with the function call whole on one line — exactly what a reader wants.

Who formats SQL, and why

Developers format SQL every time they debug a slow or wrong query. An application log shows the query as one line, and the bug — a missing condition, a wrong join, a filter applied to the wrong table — hides in the middle of it. Formatted, each clause sits on its own line, and the eye goes straight to the clause that looks wrong. Most query bugs are found this way, in the first minute of reading.

Analysts who write reports format queries so their colleagues can review them. A query that pulls a monthly sales report will be read by someone else in six months, and that person needs to see what it does without reverse-engineering a single line. Formatted SQL with upper-case keywords is the closest thing the database world has to a shared reading style.

Database administrators format queries from slow-query logs before deciding what to fix. A log line is a compressed record of what ran; formatted, it shows which tables were joined, which filters were applied, and whether an ORDER BY is sorting a large result for no reason. The fix is usually obvious once the query is readable.

Students learning SQL use a formatter to check their own work. Writing a query by hand in a plain editor is easy to get wrong — a clause in the wrong order, a comma in the wrong place — and a formatted view makes the structure visible. If the clauses do not appear in the order you expected, the query is not doing what you thought it was.

SQL formatter vs other text tools

A SQL formatter is a specialist: it knows the clause structure of a query, and nothing else. A general text formatter that only fixes indentation would not know that WHERE belongs on a new line, or that the columns of a SELECT list belong one per line. This page knows the shape of a query, so it lays the query out the way people who read SQL expect to see it.

The closest relatives are the code formatters in this same group. The JS formatter does the same job for JavaScript, using braces and semicolons the way this tool uses clauses and commas. Both tools share one idea — structure made visible through layout — but each knows the grammar of its own language, so neither replaces the other.

For data rather than code, the JSON formatter does the equivalent job: it takes compressed JSON and prints it with indentation so the nesting is visible. And for plain text, a text formatter tidies spacing without understanding any grammar at all. Each tool matches the structure of what it formats, and SQL's structure is clauses. If you need to measure a query, a word counter counts its words and a character counter counts its characters.

What a formatter never does is run the query. It cannot tell you whether the query returns the right rows, whether it will be slow, or whether a table name is misspelled. For that you need a database. The formatter's job ends where the database's begins: it makes the query readable, so that you can judge it before you run it.

Common SQL formatting mistakes

The first mistake is formatting a query and assuming it is correct. Formatting only changes layout. A query that selects the wrong column, joins on the wrong key, or filters out the rows you wanted formats perfectly and still returns the wrong answer. Read the formatted query, then run it on a small test, and check the rows before you trust it.

Second: pasting a query with placeholders and expecting the formatter to understand them. Query builders often use question marks or named parameters like :user_id, and the formatter treats them as ordinary text. That is fine — they pass through untouched — but do not expect the formatter to fill them in or check them. It formats the query as written, parameters included.

Third: relying on the keyword count as a correctness check. The count tells you how many recognised keywords appear, not whether they are in the right order or the right place. A query with SELECT and FROM in the wrong order would still count two keywords. Use the formatted layout to check the order, not the number.

Fourth: formatting vendor-specific SQL and expecting every dialect feature to lay out perfectly. This formatter covers the standard clauses and the four common statements. A query using window functions, common table expressions, or a database-specific extension will still format, but the exotic parts may land on lines you would not have chosen. Read the output and adjust by hand where it matters.

How to use this SQL formatter

Paste your SQL into the box at the top. The default example is already there: a one-line SELECT that formats into 6 lines with 4 keywords. Replace it with your own query and the formatted result appears instantly — there is no button to press.

Choose upper-case or lower-case keywords from the dropdown. Upper case is the default and the long-standing convention: the structure words stand out from table and column names. Lower case matches codebases that write everything in lower case. Switching reformats the same query immediately.

Copy the formatted SQL from the output box and paste it into your editor, your report, or your database console. Check the line and keyword counts first: if the line count looks far too high, or a clause lands somewhere unexpected, read the output — the place where the layout looks wrong is usually where the input query has a problem.

Everything runs in your browser. Your queries never leave your device, are never uploaded, and are never stored. You can format queries against proprietary tables, with real table and column names, without worrying about where the text goes — it goes nowhere, and closing the page removes it.

Frequently Asked Questions

No. Formatting only changes whitespace and keyword case, which do not affect how a database runs a query. The default SELECT returns the same rows formatted or on one line. What changes is readability — each clause sits on its own line, so the query is easier to review before you run it.

SELECT, INSERT, UPDATE, and DELETE, with the common clauses that go with them — FROM, WHERE, GROUP BY, ORDER BY, HAVING, LIMIT, JOIN, and VALUES. It is a basic formatter for standard SQL, not a parser, so vendor-specific features and deeply nested subqueries may not lay out perfectly.

Upper-case keywords are the long-standing convention in SQL: the structure words stand out from table and column names at a glance, which makes a query faster to scan. If your team writes everything in lower case, switch the style dropdown — the layout stays the same and only the case changes.

No. The column list is split only at commas that are not inside parentheses, so concat(first_name, ' ', last_name) stays whole on one line. A comma inside a function call is part of the function, not a column separator, and the formatter treats it that way.

More free tools

More calculators and utilities from Sortrun — free, no sign-up.