Defensive coding in SQL

Mark Sta Ana - Aug 25 '18 - - Dev Community

Always wrap ON clauses in parens to avoid predicates being deleted accidentally. The following code will scream if you delete the AND clause.

SELECT *
  FROM foo
  LEFT JOIN bar
    ON ((foo.id = bar.id)
        AND (foo.fizz = bar.buzz))
Enter fullscreen mode Exit fullscreen mode

Where as the following won't.

SELECT *
  FROM foo
  LEFT JOIN bar
    ON foo.id = bar.id
        AND foo.fizz = bar.buzz
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player