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

**First part:** $\Pi_X\big(\sigma_{P.Y=R.Y\ \wedge\ R.V=V2}(P\times R)\big)$

$R$ rows with $V=V2$: $(Y3,V2)$ and $(Y2,V2)$. $P$ rows whose $Y$ matches: only $(X2,Y2,Z2)$. So the first set is $\{X2\}$.

**Second part:** $\Pi_X\big(\sigma_{Q.Y=R.Y\ \wedge\ Q.T>2}(Q\times R)\big)$

$Q$ rows with $T>2$: $(X1,Y2,5)$ and $(X1,Y1,6)$. Both $Y2$ and $Y1$ appear in $R$. So the second set is $\{X1\}$.

**Difference:** $\{X2\}-\{X1\}=\{X2\}$, which has **1** tuple.

More Relational Model: Relational Algebra, Tuple Calculus, SQL questions

All Relational Model: Relational Algebra, Tuple Calculus, SQL questions →