Question
Consider the relational database with the following four schemas and their respective instances.
Student(sNo, sName, dNo), Dept(dNo, dName), Course(cNo, cName, dNo), Register(sNo, cNo)
SQL Query:
SELECT * FROM Student AS S WHERE NOT EXISTS
(SELECT cNo FROM Course WHERE dNo = "D01"
EXCEPT
SELECT cNo FROM Register WHERE sNo = S.sNo)
The number of rows returned by the above SQL query is___________.
Official answer and solution
- Official answer
- 2
- From
- Official GATE 2022 answer key
Solution
Evaluate the correlated NOT EXISTS / EXCEPT query for each student tuple.
The inner query computes:
(SELECT cNo FROM Course WHERE dNo = "D01"
EXCEPT
SELECT cNo FROM Register WHERE sNo = S.sNo)
-
From the
Coursetable, the courses offered by departmentD01are: -
For each student in the
Studenttable, the query finds whether the student has registered for all courses in . TheNOT EXISTScondition is TRUE if and only if theEXCEPTresult is empty, meaning the student has registered for bothC11andC12:
S01(James): Registered forC11, C12. .NOT EXISTSis TRUE. (Selected)S02(Rocky): Registered forC11. .NOT EXISTSis FALSE.S03(Jackson): Registered forC21, C22, C23. .NOT EXISTSis FALSE.S04(Jane): Registered forC11, C12. .NOT EXISTSis TRUE. (Selected)S05(Milli): Registered forC11, C21. .NOT EXISTSis FALSE.
Thus, exactly 2 students (S01 and S04) satisfy the query condition, and 2 rows are returned.
2
Assuming the student must belong to department D01; the query filters on courses in D01, not the student's own department.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2022 Computer Science and Information Technology (CS)
- Question
- 56 of 65
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.