Listing tables in the FROM
clause and putting all filters (including join conditions) in the WHERE
clause is the old way to do it. There was a time when this approach (with a bit of extended syntax) was even used for outer joins (on Oracle at least).
But the standard approach now is to use explicit joins. So why?
When you have multiple joins, and also possibly filter conditions that are not part of any join, it is more readable to use the explicit join syntax.
Consider:
SELECT emp.ID, mgr.ID, dept.NAME, asset.TAG_NO
FROM employee emp, employee mgr, department dept, asset
WHERE emp.HIRE_DT > :grandfather_date
AND emp.MGR_ID = mgr.ID
AND dept.NAME != 'IT'
AND asset.EMP_ID = mgr.ID
AND asset.TYPE_CD == 'D'
Now, in trouble-shooting this query, you may need to quickly assess things like: Did I join all tables correctly? If a row shows employee A and manager B, to whom is the asset on that row assigned? Who has to be in IT to be excluded from the report?
Carefully organizing the code in the query can help you answer these questions more quickly and reliably. (And this is a pretty modest-sized query when we get down to it; the bigger the query, the more this matters.)
Well, the current standard join syntax imposes some of that sane structure you should want:
SELECT emp.ID, mgr.ID, dept.NAME, asset.TAG_NO
FROM employee emp
INNER JOIN employee mgr
ON emp.MGR_ID = mgr.ID
INNER JOIN department dept
-- OOPS! This was missing above!
ON emp.DEPT_ID = dept.ID
INNER JOIN asset
ON asset.EMP_ID = mgr.ID
WHERE emp.HIRE_DT > :grandfather_date
AND dept.NAME != 'IT'
AND asset.TYPE_CD == 'D'
The fact that a predicate is, or is not, a join condition is now explicit. You know which join is constrained by a given predicate, so you know if you have constraints on all joins (and in fact, unless you specifically use CROSS JOIN
syntax you're required to... because that's almost always correct). If you need to know "whose asset (employee or manager) are we talking about", you know to go straight to the join condition for the asset
table to check it out.
You still might have to think about organizing your where clause if you have many non-join conditions; and you still have to format your code. But if you do these things, there will be much less reason for the on-call support guy to curse your name.