How to Format SQL Queries So They Are Easy to Read and Review

Learn a practical SQL style for SELECT, JOIN and WHERE clauses, why the dialect matters when formatting, and how to beautify long queries from logs or ORMs.

Updated September 29, 2026 6 min read

Use the free SQL FormatterFormat SQL queries for MySQL, PostgreSQL and more

A slow-query log hands you a 600-character statement on one line. An ORM prints the SQL it generated, and you need to understand why it returns duplicate rows. A colleague sends a report query in a chat message with all the line breaks stripped. SQL is forgiving about layout, which means it is very often written, logged and shared with no layout at all.

This guide covers a simple, widely used style for laying out SQL, why the database dialect matters even for "just formatting", how to beautify a query in the browser and in the tools you already use, and a few habits that make queries easier to review and debug.

Why formatting SQL matters

The database does not care about whitespace or keyword case. People do. A well-formatted query lets you answer the questions that matter in a review at a glance:

  • Which tables are involved, and how are they joined?
  • Which conditions belong to the join, and which filter the whole result?
  • What is grouped, and what is aggregated?

Compare the one-line version of a report with its formatted equivalent:

select c.id, c.name, count(o.id) as orders, sum(o.amount) as total_spent from customers c left join orders o on o.customer_id = c.id and o.status in ('paid','shipped') where c.city = 'Leeds' group by c.id, c.name having sum(o.amount) > 5000 order by total_spent desc limit 20
SELECT
  c.id,
  c.name,
  count(o.id) AS orders,
  sum(o.amount) AS total_spent
FROM
  customers c
  LEFT JOIN orders o ON o.customer_id = c.id
  AND o.status IN ('paid', 'shipped')
WHERE
  c.city = 'Leeds'
GROUP BY
  c.id,
  c.name
HAVING
  sum(o.amount) > 5000
ORDER BY
  total_spent DESC
LIMIT
  20

In the formatted version, it is immediately visible that the status filter is part of the LEFT JOIN condition, not the WHERE clause. That placement changes the result: customers with no paid orders still appear. On one line, that detail is easy to miss.

A practical SQL style

There is no single official SQL style, but most guides agree on a few points:

  • One clause per line. SELECT, FROM, JOIN, WHERE, GROUP BY, HAVING, ORDER BY and LIMIT each start a new line.
  • One column per line in long select lists. Diffs then show exactly which column was added or removed.
  • Uppercase keywords, lowercase identifiers. SELECT name FROM customers separates the language from your data. Some teams prefer all lowercase; consistency matters more than the choice.
  • Meaningful aliases. customers c is fine in a short query; customers AS cust or no alias is clearer in a long one.
  • Explicit joins. Write JOIN ... ON rather than listing tables separated by commas with join conditions in WHERE.
  • Consistent indentation, usually 2 or 4 spaces.

Format SQL with our tool

The SQL Formatter uses the open-source sql-formatter library in your browser and updates as you type.

  1. Open the SQL Formatter. Paste the query into SQL input, or click Open file to load a .sql file. Load sample shows a report query and an update statement.
  2. Choose the Dialect: Standard SQL, MySQL, PostgreSQL, SQL Server (T-SQL), SQLite, BigQuery or Oracle PL/SQL.
  3. Choose the Keyword case: UPPERCASE, lowercase, or Keep as typed.
  4. Choose the Indent: 2 spaces, 4 spaces or Tab.
  5. Read the Formatted SQL, then Copy it or Download it as a .sql file. Several statements separated by semicolons are formatted one after another with a blank line between them.

The formatter changes layout and keyword case only. It does not check that tables or columns exist, and it never runs the query.

Why the dialect setting matters

Every database adds its own syntax to standard SQL, and a formatter has to recognise it to lay the query out correctly.

Dialect Example syntax
PostgreSQL created_at::date casts, $1 parameters
MySQL `backtick quoted` identifiers
SQL Server [bracket quoted] identifiers, TOP 10
BigQuery `project.dataset.table` paths
Oracle PL/SQL blocks, :name bind variables

If the dialect is wrong, you may see a parse error. For instance, created_at::date fails under Standard SQL with an "Unexpected" message and a line and column, but formats cleanly with PostgreSQL selected. When you get an error on valid SQL, switch to your database's dialect first.

The dialect also decides which words count as keywords. A column called day, date or name can be uppercased as if it were a keyword in some dialects. If that bothers you, choose Keep as typed for keyword case, or quote the identifier.

Other ways to format SQL

Database IDEs

DBeaver, DataGrip, SQL Server Management Studio (with extensions), pgAdmin and Azure Data Studio all include formatters, usually under a "Format" or "Beautify" command. Use them when you are already working in the database.

Code editors and the command line

VS Code has SQL formatting extensions, several built on the same sql-formatter library. For consistent formatting across a repository of .sql files, a dedicated linter such as SQLFluff can both format and enforce rules in CI, which is the better option for teams.

Your ORM

Many ORMs and query builders can log formatted SQL in development. Check your framework's logging options before copying raw queries out of logs.

Tips for readable, reviewable SQL

  • Format before you debug. Most "wrong results" bugs, such as a join condition in the wrong place, an OR without parentheses, or a missing GROUP BY column, are much easier to spot in formatted SQL.
  • Parenthesise mixed AND/OR. a = 1 AND b = 2 OR c = 3 does not mean what many people expect. Parentheses make intent explicit and survive formatting.
  • Keep generated SQL generated. If a query comes from an ORM, format it for reading, but fix the ORM code, not the formatted text.
  • Never paste real data into shared tools. Queries in logs often contain customer emails or IDs as literals. Our formatter runs locally, but replace sensitive values before sharing the query with anyone.

If you are building tables from spreadsheet data, the CSV to SQL tool generates CREATE TABLE and INSERT statements you can then tidy here. For moving query results into other formats, the CSV to JSON Converter handles exported result sets, and the JSON Formatter tidies JSON columns you pull out of PostgreSQL or MySQL.

Frequently asked questions

Should SQL keywords be uppercase?

It is a convention, not a requirement. Uppercase keywords make queries easier to scan because they stand out from table and column names, which is why most style guides recommend them.

Does formatting SQL change what the query does?

No. The formatter only changes whitespace, line breaks and keyword case. String literals and quoted identifiers are left exactly as they are.

Why does the formatter show a parse error for valid SQL?

The query probably uses syntax specific to one database. Select the matching dialect, such as PostgreSQL for :: casts or SQL Server for bracketed names.

Can it format stored procedures?

It handles many procedural statements, especially with the matching dialect selected, but complex procedural code may format less neatly than plain queries. Database IDEs are usually better for long procedures.

Can I format several queries at once?

Yes. Paste them separated by semicolons and each is formatted in turn, with a blank line between statements.

Try it now: SQL Formatter

Loading tool…