SQL formatting conventions that survive code review

FreePanda Team

Formatting does not change what a query does. It changes how quickly a reviewer finds the bug in it.

One thing per line

A column, a join or a condition per line means adding one produces a one-line diff. Cramming a SELECT list onto a single line means every change rewrites that line and the reviewer has to diff it by eye.

Leading commas

SELECT
    id
  , email
  , created_at

Ugly at first glance, genuinely useful in practice: commenting out the last column no longer breaks the syntax, and a missing comma is visible in the left margin rather than hidden at the end of a line.

Uppercase keywords

SELECT against select costs nothing and lets the eye separate structure from identifiers instantly. Pick one convention and enforce it in the formatter rather than in review comments.

Indent subqueries and CTEs

Named CTEs beat nested subqueries for anything non-trivial. They read top to bottom, each step gets a name, and the query plan is usually identical.