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)
SELECT * FROM Student AS S WHERE NOT EXIST
(SELECT cNo FROM Course WHERE dNo = “D01”
EXCEPT
SELECT cNo FROM Register WHERE sNo = S.sNo)
The number of rows returned by the above query is ____________.
View worked solution
Answer: 2
To find the number of rows returned by the SQL query, let us analyze the query step-by-step.
Step 1: Analyze the Inner Subqueries
For a given student from the outer query:
-
First subquery:
SELECT cNo FROM Course WHERE dNo = 'D01'
Looking at theCoursetable, the courses withdNo = 'D01'are: -
Second subquery:
SELECT cNo FROM Register WHERE sNo = S.sNo
This returns the set of courses registered by the current student .
Step 2: Understand the EXCEPT and NOT EXISTS Logic
The query evaluates:
The outer NOT EXISTS (written as NOT EXIST in the query) condition is satisfied if and only if the result of this set difference is empty. This means:
In plain English, the query selects students who have registered for all courses offered by department D01 (i.e., both C11 and C12).
Step 3: Evaluate for Each Student
Let's check the registered courses for each student from the Register table:
- S01 (James): Registered for . Since , this student is selected.
- S02 (Rocky): Registered for . Since , this student is not selected.
- S03 (Jackson): Registered for . Since , this student is not selected.
- S04 (Jane): Registered for . Since , this student is selected.
- S05 (Milli): Registered for . Since , this student is not selected.
Conclusion
The query returns the rows for students S01 and S04. Therefore, the number of rows returned is 2.
Free practice · Your answers on this page are not saved.
