All mock tests are FREE till 31 December — register to save your progress
Unit 4 · Database Management Systems Paper 2 CS · December 2022 Shift 1

Directions (Q96–100): Use the following schema of an academic institution's relational database:

student (rollno, name, degree, year, sex, deptno, advisor)
department (deptid, name, hod, phone)
professor (empid, name, sex, startyear, deptno, phone)
course (courseid, cname, credits, deptno)
enrollment (rollno, courseid, sem, year, grade)
teaching (empid, courseid, sem, year, classroom)
prerequisite (precourseid, courseid)

deptno in student, professor and course refers to department.deptid; advisor (student) and hod (department) refer to professor.empid; rollno in enrollment refers to student; courseid in enrollment and teaching refers to course; empid in teaching refers to professor; precourseid and courseid in prerequisite refer to course.

Which of the following queries would retrieve the roll numbers and names of students who have enrolled in ALL the prerequisite courses of course number 324?

Pick an option to check yourself, or open the answer below.

Show answer & explanation

Correct answer: C.

SELECT s.rollno, s.name FROM student s
WHERE NOT EXISTS (SELECT * FROM prerequisite p
  WHERE p.courseid = '324' AND NOT EXISTS
    (SELECT * FROM enrollment e
     WHERE e.courseid = p.precourseid AND e.rollno = s.rollno))

Explanation

'Enrolled in all prerequisites' is relational division, expressed with a double NOT EXISTS: there is no prerequisite of 324 that the student has not enrolled in. A single enrollment row can equal ALL the prerequisites only if there is just one, and the ANY/EXISTS versions find students with at least one. (The printed paper garbles option C's brackets, and some published keys print B, but B is not a correct division query.)

More Database Management Systems questions