Database Reference
In-Depth Information
Figure 7.8
An equi-join with restricted rows returned.
You will notice that really there are two different meanings to the three different clauses
used. The first two,
webpage.id = log.webpageid
AND cookies.cookieid=log.cookieid
are used by the two joins to match primary and foreign keys in different tables, whereas the
last,
log.webpageid=1
is just a standard restriction that would work on a normal SELECT statement that did not
contain a join. Sometimes this can get confusing so it is useful to know of another format
of the inner join that works as follows:
SELECT log.*, webpage.title
FROM
webpage
INNER JOIN log ON
webpage.id = log.webpageid
This query will produce the same results as displayed in Figure 7.3. If we wanted to
restrict the rows that we were getting back from the query, in this instance we should not
append another clause on the end of the ON condition, we would need to add a WHERE
clause as follows:
SELECT log.*, webpage.title
FROM
webpage
INNER JOIN log ON
webpage.id = log.webpageid
WHERE
log.webpageid = 1
Using this format clearly sets a distinction between a restriction WHERE condition and
a JOIN condition and so can lead to queries that are easier to read.
On some database systems it is advisable to use the INNER JOIN format rather than the
one shown previously, as the DBMS has special code for performing the INNER JOIN that
Search WWH ::




Custom Search