We add such an anti-join to E for every NOT EXISTS subquery. Note,
however, that in this case we cannot remove context relations from the
cartesian product obtained by translating the From-list. (Why not?)
5.4 Translating the select list
Finally, we must apply to E the projection π
Select-list
. If the query that we
are translating is used itself as a subquery in an outer lying query, then we
must of course take care to add all parameters to Select-list.
6 Operations on relations
SQL-expressions do not simply consist of select-statements, but can also be
formed using the operations UNION, INTERSECT, and EXCEPT (possibly in con-
junction with the modifier ALL); and with the join-operations CROSS JOIN,
NATURAL JOIN, JOIN ON, and OUTER JOIN. Notice that such expressions may
also occur as subqueries. In this section, we discuss how to translate these
operations. We begin with the join operations.
6.1 Join operations
Consider first the SQL-expression
Q
1
CROSS JOIN Q
2
(†)
where Q
1
and Q
2
are themselves also SQL-expressions. We first translate
Q
1
and Q
2
into the relational algebra; which yields relational algebra ex-
pressions E
1
and E
2
. At first sight we may think that we can then simply
translate the expression (†) above as E
1
× E
2
.
This translation is incorrect, however, when Q
1
CROSS JOIN Q
2
is used
as a subquery. Indeed, in that case, Q
1
and Q
2
can be correlated subqueries
that may have parameters in common. In order to be able to continue
using the translation strategy from Section 5, we need to synchronize the
correlated subqueries on the common parameters. This can be done using
a natural join over the common parameters, simply taking:
E
1
on E
2
instead of E
1
× E
2
. (Since, according to the SQL specification, the operands
of a CROSS JOIN must have disjoint sets of attributes, any attributes common
to both E
1
and E
2
must be parameters. Hence, it is not necessary to specify
that only the parameters should be equal using a theta-join. A natural join
suffices.)
Example 12. To illustrate this discussion, consider the relations R(A, B)
and S(C), as well as the following query.
10