SQL Formatting Standards Compared: Which Style Guide Should You Follow?

May 28, 20267 min read

Why SQL Formatting Standards Matter

Unformatted SQL is hard to read, hard to review, and hard to debug. A consistent formatting standard solves all three problems. But which standard should your team adopt?

Several organizations and communities have published SQL style guides. They agree on the basics — keywords uppercase, clauses on new lines — but diverge on specifics like comma placement, alias naming, and join indentation.

This guide compares the most widely referenced standards so you can make an informed choice.

The Major Style Guides

SQL Style Guide by Simon Holywell

The most popular open-source SQL style guide. It emphasizes readability and consistency with clear, opinionated rules.

Key rules:

  • Keywords in uppercase
  • Each clause (SELECT, FROM, WHERE) on a new line
  • Commas at the end of lines (trailing commas)
  • Indentation: 2 spaces
  • Table aliases: short, meaningful (customersc, not t1)

Mozilla SQL Style Guide

Mozilla's internal guide prioritizes consistency across large codebases with many contributors.

Key rules:

  • Keywords in uppercase
  • Right-align keywords to the longest keyword in the statement
  • Commas at the start of lines (leading commas)
  • Indentation: 4 spaces
  • Explicit AS for column aliases

###triSquared SQL Style

A pragmatic guide focused on reducing merge conflicts and diff noise.

Key rules:

  • Leading commas preferred (easier to comment out lines)
  • One column per line
  • JOIN conditions on their own line
  • Subqueries indented by one level

Key Differences Compared

RuleHolywellMozillatriSquared
Comma positionTrailingLeadingLeading
Keyword alignmentLeftRightLeft
Indent size2 spaces4 spaces4 spaces
AS for aliasesRequiredRequiredOptional
Join indentationSame level as FROMIndentedIndented
Subquery styleIndented blockIndented blockCTEs preferred

The Leading vs Trailing Comma Debate

This is the most controversial formatting choice in SQL.

Trailing commas (Holywell style):

SELECT
  first_name,
  last_name,
  email
FROM users

Leading commas (Mozilla / triSquared style):

SELECT
  first_name
  , last_name
  , email
FROM users

Arguments for leading commas:

  • Easier to comment out individual lines without fixing commas
  • Diff-friendly — adding a column only changes one line
  • Missing comma errors are caught visually at the start of the line

Arguments for trailing commas:

  • More natural reading flow (matches how lists work in English)
  • Consistent with most programming languages
  • Most SQL formatters default to trailing

Neither is objectively better. Pick one and enforce it consistently.

Keyword Alignment

Left-aligned (common):

SELECT first_name,
FROM   users
WHERE  status = 'active'

Right-aligned (Mozilla):

SELECT first_name,
  FROM users
 WHERE status = 'active'

Right alignment makes clauses visually distinctive, especially in long queries. Left alignment is simpler and works better with auto-formatters.

JOIN Formatting

Joined at FROM level (Holywell):

SELECT u.first_name
FROM users u
JOIN orders o ON u.id = o.user_id

Indented joins (Mozilla):

SELECT u.first_name
FROM users u
  JOIN orders o ON u.id = o.user_id

Indentation becomes valuable with multiple joins, making the join chain visually distinct from the base FROM clause.

Choosing a Standard for Your Team

Consider these factors:

  1. Team size: Larger teams benefit from more prescriptive guides (Mozilla or Holywell)
  2. Existing codebase: Adopt whatever is closest to your current convention to minimize churn
  3. Tooling: Choose a guide that matches your auto-formatter's defaults — manual enforcement always fails
  4. Query complexity: Teams writing complex analytics queries benefit from indentation-heavy styles (triSquared)

The best style guide is the one your team actually follows. A mediocre standard applied consistently beats a perfect standard applied inconsistently.

Automating Formatting

Manual formatting does not scale. Use an auto-formatter and configure it to match your chosen standard.

Popular options:

  • sqlfmt — opinionated formatter inspired by Black (Python)
  • SQLFormatter (VS Code extension) — configurable formatting rules
  • pg_format — PostgreSQL-specific formatter

Run formatters in pre-commit hooks or CI pipelines to catch unformatted SQL before it reaches the main branch.

Key Takeaways

  • Three major SQL style guides dominate: Holywell (most popular), Mozilla (large-team focus), triSquared (diff-friendly)
  • The biggest分歧 is leading vs trailing commas — both have valid arguments
  • Right-aligned keywords improve readability for complex queries; left-aligned is simpler
  • Indented joins help distinguish join chains in multi-table queries
  • Auto-formatting is essential — manual enforcement breaks down at scale
  • Consistency matters more than which standard you pick

Try It Yourself

Format any SQL query instantly with our free SQL Formatter. Paste your code, pick a style, and copy the cleaned-up result — no installation required.