Aley Aley - 1 year ago 75
SQL Question

SQL JOIN where to place the WHERE condition?

I have two following examples.

1. Example (WHERE)

FROM table1 t1
JOIN table2 t2 ON =
WHERE t2.field = true

2. Example (JOIN AND)

FROM table1 t1
JOIN table2 t2 ON = AND t2.field = true

What is the faster way in terms of performance? What do you prefer?

Answer Source

If a filter enters in a JOIN condition functionnaly, it must appear in the ON clause of that join.

If you place it in the WHERE clause instead, the performances are the same if the join is INNER join, otherwise it differs.

Placing the filter in the WHERE clause when in reality it belongs to an OUTER JOIN would "transform" (in terms of outcome) the OUTER JOIN into an INNER JOIN, because the WHERE conditions must be realised (mandatory). The results are obviously different, as well as the explain plans.

LEFT and RIGHT joins are implicitely OUTER joins.

Hope it helped.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download