Skip to main content

← Back to blog

Why Formatting SQL Matters (and What a Formatter Does)

An ORM logs a query and it lands in your console as one 300-character line. A formatter turns that wall of text into an indented, scannable query where every clause has its place. It’s the same query — just legible. Here’s what’s actually going on.

What formatting changes (and what it doesn’t)

Formatting only touches whitespace, line breaks and indentation — plus, optionally, the case of keywords. It puts SELECT, FROM, WHERE, JOIN and GROUP BY on their own lines, indents the columns and conditions, and lines up the structure so nesting is obvious.

What it never changes: your tables, columns, values or logic. A formatted query returns exactly the same result as the cramped one. That also means formatting is not validation — it won’t tell you a column is misspelled or that the query will actually run.

Dialects: why the tool asks which database

SQL has a standard, but every database adds its own flavour, and the formatter needs to know which one to parse correctly:

  • Identifier quoting — MySQL uses backticks (`col`); PostgreSQL and standard SQL use double quotes ("col").
  • Row limitingLIMIT in MySQL/SQLite/PostgreSQL, TOP in SQL Server.
  • Casts — PostgreSQL’s value::int has no equivalent elsewhere.

Pick the wrong dialect and these get mis-parsed, so the indentation comes out wrong. Choose your actual database in the SQL formatter before you paste.

Uppercase keywords: style, not rule

Many teams write keywords in UPPERCASE (SELECT, FROM) so they stand out from table and column names. SQL itself is case-insensitive for keywords, so this is purely a readability convention — a formatter can apply it consistently across a whole query in one click.

Why do it locally?

A client-side formatter parses and re-prints the SQL in your browser. Your queries — which often reveal schema and business logic — never leave your machine.

Before and after

The same query, minified by an ORM and then formatted:

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)>0 order by orders desc;

Formatted, the structure reads top to bottom:

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) > 0
ORDER BY orders DESC;

Same result, but now the joins, filters and grouping are obvious at a glance.

Leading vs trailing commas

Two common column styles: trailing commas (u.id, at the end of each line) or leading commas (, u.name at the start). Leading commas make version-control diffs cleaner — adding a column touches one line instead of two — but both are valid and most formatters let you pick. Consistency within a codebase matters more than which side you choose.

Common questions

  • Does formatting affect performance? No. The parser ignores insignificant whitespace and keyword case, so a formatted query is equivalent to the engine. Only the contents of string literals are ever whitespace-sensitive.
  • Should I commit formatted SQL? For views, migrations and stored procedures, yes — a consistent style keeps diffs small and reviews fast.
  • Formatting won’t fix a broken query, though. It only rearranges text; use a real database or linter to catch errors. Exporting the results afterwards? See how to convert JSON to CSV and Excel.