How to Format SQL Queries So They Are Easy to Read (Free Tool)

Illustration: a messy one-line SQL query on the left and the same query neatly formatted with uppercase keywords and indented conditions on the right
Original illustration by ViralScopeHub

Short answer

To format a SQL query, put each main clause (SELECT, FROM, WHERE, GROUP BY, ORDER BY) on its own line, list one column per line, indent the AND and OR conditions, and write keywords in capitals. You can do it by hand, or paste the query into our free SQL Formatter and press Format. It runs in your browser and only changes spacing and keyword case, never what the query does.

SQL has a habit of ending up on one very long line. Apps build queries in code and log them that way, reporting tools export them that way, and when you write one in a hurry it grows sideways until you have to scroll to see the end. The database doesn't care: to it, spaces and line breaks are just separators. People do care. A query you can't read is a query where a missing join condition or a wrong filter hides in plain sight.

Formatting fixes that. It doesn't change the result of the query at all. It just lays the query out so its shape is obvious: what you select, where it comes from, how tables are joined, which rows are filtered and how the result is sorted.

How to format a SQL query in three steps

  1. Open the SQL Formatter and paste your query into the box. You can paste several statements at once if they end with semicolons.
  2. Pick your style: an indent of 2 spaces, 4 spaces or a tab, and whether keywords should be changed to UPPERCASE or left as you typed them.
  3. Press Format SQL and copy the result. Changing the indent or the uppercase option updates the output straight away.

Everything happens on your own device, so table names, customer data or anything else inside the query isn't sent to our server. That matters if you copied the query from a work system.

Before and after

Here is a typical one-liner:

select name,city,count(*) as orders from customers c join orders o on o.cid=c.id where o.total>50 and c.active=1 group by name,city order by orders desc limit 10

And here is what the formatter gives back:

SELECT
  name,
  city,
  COUNT(*) AS orders
FROM customers c
JOIN orders o
  ON o.cid = c.id
WHERE o.total > 50
  AND c.active = 1
GROUP BY name, city
ORDER BY orders DESC
LIMIT 10

Same query, same result. But now you can see in one glance that there are three output columns, one join, two filters and a top-10 limit. If someone asks "why are inactive customers missing?", the answer is right there on its own line.

The formatting rules that matter most

Style guides differ in the details, but almost all of them agree on a few basics. These are also the rules our formatter follows:

What the formatter never touches

A good formatter only moves whitespace and changes the case of SQL keywords. Text inside single quotes, such as 'Pune' or '2026-01-01', stays exactly as written, because changing it would change your data. Quoted names like "Order Details" or [Order Details] are left alone too, and so are comments. That is also why one formatter works for MySQL, PostgreSQL, SQLite, SQL Server and most other databases: it isn't rewriting the query in any dialect, just laying it out.

When to minify instead

Sometimes you want the opposite: the whole query on one line, for example inside a config file, a spreadsheet cell or a string in code. The Minify button does that. It removes comments and line breaks and squeezes the query into a single line. Keep the formatted copy for reading and reviewing, and use the minified one only where a single line is required. If the query lives in a JSON settings file, our JSON formatter helps you check the file around it, and our code beautifier does the same job for HTML, CSS and JavaScript.

Small habits that make SQL easier to read

If you work with exported data, our JSON to CSV converter can turn API results into a spreadsheet you can check against your query. And if you are studying SQL for a course or a job test, our study hub and exam guides have more free help, while the full tools list has everything else we offer.

VSH take: A lot of people think formatting is just about looking neat. In our experience it is really about catching mistakes. Most SQL bugs we have seen were not clever: a forgotten join condition, an OR that needed brackets, a filter on the wrong table. All of them are much easier to spot when each part of the query sits on its own line. Formatting takes one click, so there is no reason to read a query any other way.

ViralScopeHub is independent and not affiliated with Oracle, Microsoft, the PostgreSQL project, SQLite or any database maker mentioned here.

Frequently asked questions

How do I format a SQL query online?

Paste the query into a free SQL formatter such as ours, choose an indent and whether keywords should be uppercase, and press Format SQL. Main clauses move to new lines and conditions are indented.

Does formatting change what my query does?

No. Formatting only changes spaces, line breaks and the case of SQL keywords. Text in quotes, names and comments stay the same, so the query returns exactly the same result.

Should SQL keywords be uppercase?

SQL keywords are not case-sensitive, so it is a style choice. Many teams write them in capitals because it separates SQL words from table and column names. Our formatter can do either.

Does the formatter work with MySQL, PostgreSQL and SQL Server?

Yes. Because it only rearranges whitespace and keyword case, it works with MySQL, PostgreSQL, SQLite, SQL Server and most other databases.

What does minify SQL mean?

Minifying removes comments and line breaks so the whole query fits on one line. It is useful inside config files or code strings. Keep a formatted copy for reading.

Is my SQL uploaded to your server?

No. The SQL Formatter runs in your browser, so the queries you paste stay on your device.

Sources

More games

All games