The following is based on practical experience with disparate SQL formatting practices used by hundreds of different development shops within North America and elsewhere. These are intended as guidelines, for use as a starting point in developing a consistent personal style of SQL formatting. Discretion must of course be used for individual cases. Recommended deviations from the presented 'canonical' style have been mentioned where appropriate, but these are by no means exclusive. Adopting these or similar guidelines should greatly improve readability of code and clarity of thought, with a corresponding improvement in productivity. Some small effort has been made to use examples which could conceivably make 'practical sense', but this is by no means guaranteed, as contrived examples often do a better job of illustrating the formatting. This remains a work in progress so constructive criticism is welcomed.
How to Format SQL
SQL keywords should be entered in upper case to visually differentiate user-defined identifiers such as table and column names, which should be in lower case or mixed case.
Each clause starts a new line.
When there is only one 'argument' to a clause, it goes on the same line as the clause keyword.
GROUP BY single_column
ORDER BY single_column
when there are multiple comma-separated arguments, the arguments are listed one per line beneath the clause keyword, and indented. (two spaces is necessary and sufficient for fixed-width fonts; more spaces may be necessary for variable-width fonts. DO NOT use tabs.) When refering to multiple tables, it is always advisable to use a short form correlation name, usually derived from the initial or initials of the full table name.
Multiple elements within parentheses should also be indented.
GROUPING SETS (
Some sets of elements lend themselves to conceptual grouping; you may choose to place the elements from such a group together on one line.
name_first, name_middle, name_last,
address_street, address_city, address_state, address_pcode, address_country,
GROUP BY GROUPING SETS (
(a, b, c),
JOIN clauses should be laid out each on a new line, with the JOIN keyword beginning the line.
LEFT JOIN table_two t2 ON t2.column_A = t1.column_B
RIGHT JOIN table_three t3 ON t3.column_C = t1.column_D
Subselects should be further indented, with their enclosing parentheses on distinct lines.
Where the subselect is small, you may choose to reduce it to a single line.
(SELECT c1 FROM t1 WHERE c1='xyz'),
Multiple predicates in a WHERE clause should ideally be placed one per line where possible, with the logical conjunction (AND, OR) starting the line, and expressions within parentheses further indented.
Sometimes you may also choose to group some predicates on a single line.
<predicate_1> AND <predicate_2>
AND (<predicate_3> OR <predicate_4> OR <predicate_5>)
Set operations (UNION, EXCEPT, INTERSECT) between two subselects should be formatted with the set operator keyword on its own line, unindented, between the subselects, as follows:
SELECT c1 FROM t1
SELECT c2 FROM t2
SELECT c3 FROM t3
Finally, these and similar ideas can be applied to other standard (and non-standard) SQL statements.
INSERT INTO my_table (
DELETE FROM my_table