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?
View worked solution
Answer: B
To determine which statement is correct, let us analyze the schema and the behavior of each query with respect to duplicate rows.
1. Schema Analysis
enrolled(student, course): The primary key is(student, course). This means a single student can be enrolled in multiple courses, so a student can appear multiple times in this relation.paid(student, amount): The primary key isstudent. This means each student can appear at most once in this relation.
2. Query Analysis
-
Query 1:
This selects fromselect student from enrolled where student in (select student from paid)enrolled. If a student is enrolled in courses and has paid, this query will return that student's ID times. -
Query 2:
This selects fromselect student from paid where student in (select student from enrolled)paid. Sincestudentis the primary key ofpaid, each student who has paid and is enrolled in at least one course will be returned exactly once. -
Query 3:
This performs an inner join. For a student enrolled in courses who has paid, the join produces rows. Thus, this query returns that student's ID times, matching the output of Query 1.select E.student from enrolled E, paid P where E.student = P.student -
Query 4:
This selects fromselect student from paid where exists (select * from enrolled where enrolled.student = paid.student)paid. It checks for the existence of a matching record inenrolled. Since it selects frompaid, each matching student is returned exactly once, matching the output of Query 2.
3. Evaluating the Options
- Option A is incorrect: If a student is enrolled in multiple courses, Query 1 and Query 2 will return different numbers of duplicate rows.
- Option B is correct: Query 2 and Query 4 both select from
paidand return each matching student exactly once, making them identical for all databases. However, if a student is enrolled in multiple courses, Query 1 (which returns duplicates) and Query 2 (which does not) will produce different row sets. - Option C is incorrect: Since for matching students, Query 3 (which returns rows per matching student) will always return at least as many rows as Query 2 (which returns row per matching student). It can never return strictly fewer rows.
- Option D is incorrect: Standard
SELECTqueries do not modify data and cannot cause integrity violations.
Free practice · Your answers on this page are not saved.
