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)
Choose one option
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.

  1. Semantics of the Given Query:

    select * from R where a in (select S.a from S)
    
    • The subquery returns the multiset of values of S.aS.a.
    • For each tuple r∈Rr \in R, the IN predicate evaluates to TRUE if r.ar.a matches at least one value in S.aS.a.
    • Crucially, even if r.ar.a appears multiple times in SS, the tuple rr is output only once per instance of rr in RR.
    • If RR contains duplicate tuples, each duplicate tuple in RR is evaluated independently and preserved.
  2. Evaluating Options:

    • (A) select R.* from R, S where R.a=S.a: If a value of aa appears kk times in SS, the tuple r∈Rr \in R will be duplicated kk times in the cross join, changing multiplicity.
    • (B) select distinct R.* from R,S where R.a=S.a: The DISTINCT keyword eliminates pre-existing duplicate tuples from RR, 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 RR matches at most one tuple in S1S_1, preserving the exact multiplicity of RR without generating spurious duplicate rows or discarding legitimate duplicates in RR.
  3. Conclusion:
    Option (C) produces the identical answer under SQL multiset semantics.

Final answer

C

Common trap

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.