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)
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 , the query checks if the value exists in the set of values of .
- If it exists, the tuple is selected.
- The number of times a tuple appears in the output is exactly equal to its original frequency in . The multiplicity of in 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 appears times in , the tuple will be duplicated times in the output. This changes the original multiplicity. - Option B:
select distinct R.* from R,S where R.a=S.a
Thedistinctkeyword removes all duplicate tuples. If relation 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 selectingdistinct afrom first, we ensure that each value of appears at most once in the temporary relation . Joining with ensures that each matching tuple in is joined with exactly one tuple from . Thus, the original multiplicity of tuples in is perfectly preserved, making it equivalent to theINsubquery. - 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.
