edited by
20,522 views
61 votes
61 votes

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. All queries return identical row sets for any database
  2. Query$2$ and Query$4$ return identical row sets for all databases but there exist databases for which Query$1$ and Query$2$ return different row sets
  3. There exist databases for which Query$3$ returns strictly fewer rows than Query$2$
  4. There exist databases for which Query$4$ will encounter an integrity violation at runtime
edited by

7 Answers

1 votes
1 votes

ENROLLED

Student           Course

SEHWAG.       ODI

SACHIN          ODI

SEHWAG        TEST

VIRAT             ODI

PAID 

Student          Amount

SEHWAG       219

SACHIN         200

ROHIT           264

OUTPUT OF QUERY 1

SEHWAG

SEHWAG

SACHIN

OUTPUT OF QUERY 2

SEHWAG

SACHIN

OUTPUT OF QUERY 3

SEHWAG

SACHIN

OUTPUT OF QUERY 4

SEHWAG

SACHIN

So, Query2 and Query4 return identical row sets for all databases but there exist databases for which Query1 and Query2 return different row sets.

Therefore, option (B) is correct. 🏏

0 votes
0 votes
BEFORE CHECKING CONSIDER THESE FACTS THAT THERE MAY BE SOME STUDENTS WHO HAVE ENROLLED BUT NOT PAID AND SOME STUDENTS WHO HAVE PAID BUT NOT ENROLLED
0 votes
0 votes

Adding more perspective

Query 1: wants student names from the enrolled relation but student alone doesn't form primary key so duplicate students are allowed.

Query 2: wants student from paid relation and student alone is a primary key which has to be unique and not null.

Query 3: logic same as option B

Query 4: logic same as option D

so from the observation, we can say query 1 equivalent to query 3 and query 2 equivalent to query 4

Answer:

Related questions