When merging tables with selected rows in SQL, we often think of
using the JOIN
statement with the WHERE
and
the ON
clause. The resulting table would depend on the
inclusion of filters in the WHERE
or the ON
clause. To illustrate this difference, I employ the SQL examples from a MODE
tutorial page.
Filtering in The ON
Clause
1 | SELECT companies.permalink AS companies_permalink, |
ORDER BY 1
is equivalent toORDER BY companies.permalink
The conditional statement AND...
is evaluated
before the join occurs. Particularly, the
AND
statement is evaluated on the joining table (i.e.,
acquisitions
) rather than the target table (i.e.,
companies
). Hence, the 1000memories permalink is still
displayed in the column that pulls from the target table (i.e.,
companies
).
Filtering in The
WHERE
Clause
1 | SELECT companies.permalink AS companies_permalink, |
If you move the same filter to the WHERE
clause, you
will notice that the filter happens after the tables
are joined. The result is that the 1000 memories row is joined onto the
original table, but then it is filtered out entirely (in both tables) in
the WHERE
clause before displaying results (it would have
been between the two highlighted lines in the resulting table
below).
Note that filtering in the WHERE
clause can
also filter null values, so we added an extra line to make sure to
include the nulls.
The null values will not be evaluated using comparison operators.
View / Make Comments