Skip to content
Official PYQ · 2014

GATE CS 2014 Set 2 Q54 — Tuple Calculus and SQL

Explain with AI

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
View worked solution

Answer: C

To find the equivalent query, let us analyze the behavior of the original nested query:

select * from R where a in (select S.a from S)
  • For each tuple r∈Rr \in R, the query checks if the value r.ar.a exists in the set of values of S.aS.a.
  • If it exists, the tuple rr is selected.
  • The number of times a tuple rr appears in the output is exactly equal to its original frequency in RR. The multiplicity of r.ar.a in SS has no effect on the output multiplicity.

Now let us evaluate the options:

  • Option A: select R.* from R, S where R.a=S.a
    This performs a regular inner join. If a value r.ar.a appears kk times in SS, the tuple rr will be duplicated kk times in the output. This changes the original multiplicity.
  • Option B: select distinct R.* from R,S where R.a=S.a
    The distinct keyword removes all duplicate tuples. If relation RR originally contained duplicate tuples, they would be collapsed into a single tuple, which does not match the original query's behavior.
  • Option C: select R.* from R,(select distinct a from S) as S1 where R.a=S1.a
    By selecting distinct a from SS first, we ensure that each value of aa appears at most once in the temporary relation S1S1. Joining RR with S1S1 ensures that each matching tuple in RR is joined with exactly one tuple from S1S1. Thus, the original multiplicity of tuples in RR is perfectly preserved, making it equivalent to the IN subquery.
  • Option D: select R.* from R,S where R.a=S.a and is unique R
    This is syntactically invalid in standard SQL and does not preserve the correct semantics.

Therefore, Option C is the correct query.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Tuple Calculus and SQL questions