Question

Consider the following relations P(X,Y,Z)P(X, Y, Z), Q(X,Y,T)Q(X, Y, T) and R(Y,V)R(Y, V):

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

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))\Pi_X(\sigma_{P.Y=R.Y \land R.V=V2}(P \times R)) - \Pi_X(\sigma_{Q.Y=R.Y \land Q.T>2}(Q \times R))

Answer: ________.

Official answer and solution
Official answer
1
From
Official GATE 2019 answer key

Solution

Evaluate the two projection terms separately and take their set difference.

We are given the relational algebra expression:

ΠX(σP.Y=R.Y∧R.V=V2(P×R))−ΠX(σQ.Y=R.Y∧Q.T>2(Q×R))\Pi_X(\sigma_{P.Y=R.Y \land R.V=V2}(P \times R)) - \Pi_X(\sigma_{Q.Y=R.Y \land Q.T>2}(Q \times R))

Step 1: Evaluate the first term:

T1=ΠX(σP.Y=R.Y∧R.V=V2(P×R))T_1 = \Pi_X(\sigma_{P.Y=R.Y \land R.V=V2}(P \times R))
  • In relation R(Y,V)R(Y, V), tuples with V=V2V = V2 are (Y3,V2)(Y3, V2) and (Y2,V2)(Y2, V2). So the qualifying YY values from RR are Y3Y3 and Y2Y2.
  • Joining with P(X,Y,Z)P(X, Y, Z) on P.Y=R.YP.Y = R.Y, we find tuples in PP having Y∈{Y3,Y2}Y \in \{Y3, Y2\}:
    • (X1,Y1,Z1)(X1, Y1, Z1) has Y=Y1Y = Y1 (no match)
    • (X1,Y1,Z2)(X1, Y1, Z2) has Y=Y1Y = Y1 (no match)
    • (X2,Y2,Z2)(X2, Y2, Z2) has Y=Y2Y = Y2 (matches (Y2,V2)(Y2, V2))
    • (X2,Y4,Z4)(X2, Y4, Z4) has Y=Y4Y = Y4 (no match)
  • Projecting attribute XX yields:
T1={X2}T_1 = \{X2\}

Step 2: Evaluate the second term:

T2=ΠX(σQ.Y=R.Y∧Q.T>2(Q×R))T_2 = \Pi_X(\sigma_{Q.Y=R.Y \land Q.T>2}(Q \times R))
  • In relation Q(X,Y,T)Q(X, Y, T), tuples satisfying Q.T>2Q.T > 2 are:
    • (X1,Y2,5)(X1, Y2, 5) with Y=Y2Y = Y2
    • (X1,Y1,6)(X1, Y1, 6) with Y=Y1Y = Y1
      (Tuple (X2,Y1,2)(X2, Y1, 2) has T=2≯2T = 2 \ngtr 2, and (X3,Y3,1)(X3, Y3, 1) has T=1≯2T = 1 \ngtr 2)
  • Now check if these YY values (Y2,Y1Y2, Y1) exist in relation RR:
    • RR contains Y1Y1 (in (Y1,V1)(Y1, V1)) and Y2Y2 (in (Y2,V3)(Y2, V3) and (Y2,V2)(Y2, V2)). Both exist in RR.
  • Projecting attribute XX from the matching tuples:
    Both (X1,Y2,5)(X1, Y2, 5) and (X1,Y1,6)(X1, Y1, 6) have X=X1X = X1.
    Thus:
T2={X1}T_2 = \{X1\}

Step 3: Set difference:

T1−T2={X2}−{X1}={X2}T_1 - T_2 = \{X2\} - \{X1\} = \{X2\}

The resulting relation contains exactly 1 tuple, namely (X2)(X2).

Therefore, the number of tuples returned is 1.

Final answer

1

Common trap

Treating Q.T>2Q.T > 2 as ≥2\ge 2 which would include (X2,Y1,2)(X2, Y1, 2) with X=X2X = X2, making the set difference empty (00 tuples).

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2019 Computer Science and Information Technology (CS)
Question
65 of 65, printed as CS Q.55
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.