Consider the relations P(X,Y,Z) = {(X1,Y1,Z1),(X1,Y1,Z2),(X2,Y2,Z2),(X2,Y4,Z4)}, Q(X,Y,T) = {(X2,Y1,2),(X1,Y2,5),(X1,Y1,6),(X3,Y3,1)}, and R(Y,V) = {(Y1,V1),(Y3,V2),(Y2,V3),(Y2,V2)}. How many tuples will be returned by the following relational algebra query?
Π_X(σ_{P.Y=R.Y ∧ R.V=V2}(P × R)) − Π_X(σ_{Q.Y=R.Y ∧ Q.T>2}(Q × R))
Explanation
First term: R rows with V='V2' are (Y3,V2) and (Y2,V2). Matching P.Y=R.Y: no P row has Y=Y3; the P row (X2,Y2,Z2) has Y=Y2, matching (Y2,V2). So the filtered join has one row with X=X2, giving Π_X = {X2}. Second term: Q rows with T>2 are (X1,Y2,5) and (X1,Y1,6). Matching Q.Y=R.Y: (X1,Y2,5) matches R rows (Y2,V3) and (Y2,V2) — two matches, both with X=X1; (X1,Y1,6) matches R row (Y1,V1) — also X=X1. Projecting onto X (a SET operation, deduplicating) gives {X1}. Set difference: {X2} − {X1} = {X2}, which has exactly 1 tuple.
Source: GATE CS 2019 (Set-2) (2019)
This question is part of a 15-day revision cycle — sign in and Project Forge will bring it back the day before you'd forget it.