Queries

The following should be taken into account when writing queries:

  • Primary query clauses should all be at the same indentation level.
  • Columns in any SELECT, INSERT, FETCH or VALUES statement should be listed vertically and indented.
  • Any value assignments should also be indented together.
  • Multiple conditions in a WHERE clause should be listed vertically and indented with the condition leading the line.
  • Joins should be explicitly referenced using the JOIN clause as opposed to querying from multiple tables and “joining” in the WHERE clause.
  • Joins should be indented and multiple join conditions should be listed vertically and indented with the condition leading the line in the same manner as a WHERE clause.

For example,


SELECT @Value1	= Column1,
       @Value2	= Column2,
       @Value3	= Column3,
       @Value4	= Column4 
FROM dbo.Table t
INNER JOIN dbo.Table2 t2 ON  t.Column1 = t2.Column1
                             AND t.Column2 = t2.Column2

WHERE   Column1 = 5 
        AND Column2 IS NOT NULL
        OR Column3 LIKE ‘Bob%
ORDER BY Column 1 ASC