The GATE Grind

GATE 2019 CS – Question 65

Databases · Relational Model: Relational Algebra, Tuple Calculus, SQL · 2 marks · Numerical answer

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

XYZ
X1Y1Z1
X1Y1Z2
X2Y2Z2
X2Y4Z4

(relation P)

XYT
X2Y12
X1Y25
X1Y16
X3Y31

(relation Q)

YV
Y1V1
Y3V2
Y2V3
Y2V2

(relation R)

How many tuples will be returned by the following relational algebra query?

$$\Pi_X\left(\sigma_{(P.Y=R.Y\wedge R.V=V2)}(P\times R)\right)-\Pi_X\left(\sigma_{(Q.Y=R.Y\wedge Q.T>2)}(Q\times R)\right)$$

Answer: ________.

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: 1

Explanation

The first part: tuples of $P$ joined with $R$ on $Y$ where $V=V2$, i.e. $Y\in\{Y3,Y2\}$ with $V2$: $R$ has $(Y3,V2)$ and $(Y2,V2)$. $P$ has $Y2$ with $X2$, so the first set is $\{X2\}$. The second part: $Q$ tuples with $T>2$ are $(X1,Y2,5)$ and $(X1,Y1,6)$, which join with $R$ on $Y$, so the second set is $\{X1\}$. The difference is $\{X2\}$, which has 1 tuple.