Question
SQL allows duplicate tuples in relations, and correspondingly defines the multiplicity of tuples in the result of joins. Which one of the following queries always gives the same answer as the nested query shown below:
select * from R where a in (select S.a from S)
Official answer and solution
- Official answer
- C
- From
- Official GATE 2014 answer key
Solution
Analyze SQL join semantics under duplicate tuples (multiset semantics) to find the query preserving exact tuple multiplicity.
-
Semantics of the Given Query:
select * from R where a in (select S.a from S)- The subquery returns the multiset of values of .
- For each tuple , the
INpredicate evaluates to TRUE if matches at least one value in . - Crucially, even if appears multiple times in , the tuple is output only once per instance of in .
- If contains duplicate tuples, each duplicate tuple in is evaluated independently and preserved.
-
Evaluating Options:
- (A)
select R.* from R, S where R.a=S.a: If a value of appears times in , the tuple will be duplicated times in the cross join, changing multiplicity. - (B)
select distinct R.* from R,S where R.a=S.a: TheDISTINCTkeyword eliminates pre-existing duplicate tuples from , changing multiplicity. - (C)
select R.* from R,(select distinct a from S) as S1 where R.a=S1.a:
The subquery(select distinct a from S)produces a relation where each matching attribute value appears exactly once.
Therefore, each tuple in matches at most one tuple in , preserving the exact multiplicity of without generating spurious duplicate rows or discarding legitimate duplicates in .
- (A)
-
Conclusion:
Option (C) produces the identical answer under SQL multiset semantics.
C
Overlooking that a plain join duplicates R rows if S has duplicate join keys, while distinct on the outer query wrongly deduplicates R.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2014 Computer Science and Information Technology (CS) Session 2
- Question
- 64 of 65, printed as CS Q.54
- 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.