The opening question is sending us down the wrong thinking. It should be:
Is there a relational algebra equivalent of the SQL expression R WHERE ... [NOT] IN S
?
(That is, the answer is some operation between two relations, not some sort of filter.)
The answer is Yes, it is (Natural) JOIN
aka the bowtie operator ⋈
.
To see why, let's first tidy up the SQL solution given. As shown, it's looking for attribute A1 NOT IN
a relation with single attribute A2
. That's really a mis-match in attribute names. SQL also allows NOT
inside the where condition. This SQL makes the logical structure clearer:
SELECT * FROM R
WHERE NOT (A1 IN (SELECT A2 AS A1 FROM R) )
Now we can see a projection and a rename. (The surrounding NOT
we can implement as set MINUS, as per the first answer.) So the equivalent RA is:
R - (R ⋈ ρ
A1⁄A2(π
A2(R)))
For interest, the Tutorial D is:
R MINUS (R JOIN (R {A2} RENAME A2 AS A1))
In the way the question is put, there's a hangover from SQL thinking. SQL's WHERE
forces you into row-level 'mode'. This is contra Codd's rule 7 requiring set-at-a-time operators.
In general, SQL's WHERE
and RA's σ
with their row-level filters can be more succinctly implemented as (Natural) JOIN
with set-at-a-time logic. (For example, this is what Date & Darwen do in their A algebra.)