- SELECT, FROM, WHERE, ORDER BY, GROUP BY, LIMIT, OFFSET, UNION on its own line
- Columns to select, from, order by, group by on its own line, if AS is present, line them up at the end
- All SQL operators capitalized
- The all comparison operators are indented 8 spaces
- Usage of AND or OR operators replace spaces. Example:
WHERE
foo.id = bar.foo_id
AND bar.id = baz.bar_id
OR bar.id = quux.bar_id
- Open parenthesis on the same line
- Close parenthesis on a new line
- Spaces, not tabs
- Operators are lined up within its section
WHERE
foo.id = bar.foo_id
AND (
bar.id = baz.bar_id
OR (
-- Note that the `=`s are lined up here, but not to the parent section
bar.id = quux.bar_id
AND quux.enabled = 1
)
)
- All column or table listings are on their new line, indented by 4 spaces
- A single blank line between sections (SELECT block, FROM block, JOIN blocks)
- JOIN's ON is on the same line as JOIN, and the table name.
- In a JOIN, put the joined table's columns first in expressions
SELECT
-- Note these are 4 spaces, not 8
foo.bar,
bar.baz,
baz.quux,
FROM
-- Note that it is separated by the SELECT by a single line, and the tables begin on a new line
-- Also note that the AS's line up, even though one of them is very long.
foo AS quux,
supercalifragilisticexpialidocious AS super,
baz AS zanzibar
LEFT JOIN another_table ON (
-- Note that another_table comes first.
another_table.foozle = super.bazzle
AND another_table.baz = baz.id
)