Skip to main content

GATE CSE 2006Databases › SQLVerified

GATE CSE 2006 PYQ: Consider the relation enrolled (student, course) in which (student, course) is the primary key, and the…

Consider the relation enrolled (student, course) in which (student, course) is the primary key, and the relation paid (student, amount) where student is the primary key. Assume no null values and no foreign keys or integrity constraints.
Given the following four queries:

Query1: 

select student from enrolled where student in (select student from paid)

Query2: 

select student from paid where student in (select student from enrolled)

Query3: 

select E.student from enrolled E, paid P where E.student = P.student

Query4: 

select student from paid where exists 
    (select * from enrolled where enrolled.student = paid.student)

Which one of the following statements is correct?

  1. A
    All queries return identical row sets for any database
  2. B
    Query2 and Query4 return identical row sets for all databases but there exist databases for which Query1 and Query2 return different row sets
    Correct answer
  3. C
    There exist databases for which Query3 returns strictly fewer rows than Query2
  4. D
    There exist databases for which Query4 will encounter an integrity violation at runtime

Answer

(B) Query 2 2 2 and Query 4 4 4 return identical row sets for all databases but there exist databases for which Query 1 1 1 and Query 2 2 2 return different row sets

Approach

Analyze each query's SQL semantics under bag/set behavior, using the key constraints: enrolled(student, course) allows duplicate student values because a student can enroll in multiple courses; paid(student, amount) makes student unique. Compare the resulting row bags for Q1, Q2, Q3, Q4.

Because enrolled permits multiple rows per student while paid permits only one, Query1 can produce duplicate student rows that Query2 cannot, so Q1 and Q2 may differ. Likewise, Query3 uses UNION (set semantics, duplicates removed) while Query4 uses UNION ALL (bag semantics, duplicates retained), so Q3 and Q4 may differ. This matches option B.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.