GATE / CS ARCHIVE

SIMILAR QUESTIONS / DATABASE MANAGEMENT SYSTEMS

Join enrollments to courses for a grade and instructor

2 questions solved the same way, newest first. Back to Database Management Systems

How these are solved

Connect enrollment course identifiers to course/instructor rows, then require grade A and the named instructor. Omitted join predicates produce unrelated Cartesian witnesses.

2 marks · MCQ
Report issue

Existential selection in a three-relation join

SQL & Queries

Question 86, GATE 2003: Relations are Students(Roll_number,Name,Date_of_birth), Courses(Course_number,Course_name,Instructor), and Grades(Roll_number,Course_number,Grade). Interpret this query with the printed string constants rendered in SQL quotes:
SELECT DISTINCT Name
FROM Students,Courses,Grades
WHERE Students.Roll_number=Grades.Roll_number
  AND Courses.Instructor='Korth'
  AND Courses.Course_number=Grades.Course_number
  AND Grades.Grade='A';
What set is computed?
A. Students' names with A in all courses taught by Korth
B. Students' names with A in all courses
C. Students' names with A in at least one course taught by Korth
D. None of the above
Question 86, GATE 2003: Relations are Students(Roll_number,Name,Date_of_birth), Courses(Course_number,Course_name,Instructor), and Grades(Roll_number,Course_number,Grade). Interpret this query with the printed string constants rendered in SQL quotes:
SELECT DISTINCT Name
FROM Students,Courses,Grades
WHERE Students.Roll_number=Grades.Roll_number
  AND Courses.Instructor='Korth'
  AND Courses.Course_number=Grades.Course_number
  AND Grades.Grade='A';
What set is computed?
A. Students' names with A in all courses taught by Korth
B. Students' names with A in all courses
C. Students' names with A in at least one course taught by Korth
D. None of the above
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

1. The Roll_number equality pairs each student with that student's grade records. The Course_number equality pairs each grade with its course.
2. The two filters discard any joined row whose instructor is not Korth or whose grade is not A. Every surviving row therefore witnesses one qualifying course for that student.
3. Projection selects the name and DISTINCT removes repeated names when several qualifying rows exist. It does not test unselected rows or require every course to qualify.
4. Thus a student appears if at least one qualifying Korth-course A exists, C. Requiring all such courses would need a different universal/anti-existence/division construction.

Solution: The joins connect each student to their grade records and the corresponding courses. The filters retain records having grade A in a course taught by Korth. A student's name is produced whenever at least one such joined record exists, and DISTINCT removes duplicate names. There is no universal quantifier, division, or count comparison requiring all courses to qualify. The intended relational query therefore computes option C, names of students with an A in at least one course taught by Korth.

Original paper, page 12

5 marks · Descriptive
Report issue

Students taught by Ramesh

SQL & Queries

Question 23, GATE 1993: STUDENTS(rollno key,sname,saddr), COURSES(cno key,cname), ENROLL(rollno,cno composite key,grade), TEACH(tno,cno composite key,tname). FDs: rollno→sname,saddr; cno→cname; tno→tname; rollno,cno→grade. The reproduced cao/sdaddr spellings are interpreted as cno/saddr. Retrieve roll number and name of students with grade A in at least one course taught by a teacher named Ramesh.
Question 23, GATE 1993: STUDENTS(rollno key,sname,saddr), COURSES(cno key,cname), ENROLL(rollno,cno composite key,grade), TEACH(tno,cno composite key,tname). FDs: rollno→sname,saddr; cno→cname; tno→tname; rollno,cno→grade. The reproduced cao/sdaddr spellings are interpreted as cno/saddr. Retrieve roll number and name of students with grade A in at least one course taught by a teacher named Ramesh.
Question 23, GATE 1993: STUDENTS(rollno key,sname,saddr), COURSES(cno key,cname), ENROLL(rollno,cno composite key,grade), TEACH(tno,cno composite key,tname). FDs: rollno→sname,saddr; cno→cname; tno→tname; rollno,cno→grade. The reproduced cao/sdaddr spellings are interpreted as cno/saddr. Retrieve roll number and name of students with grade A in at least one course taught by a teacher named Ramesh.
Tap to enlarge
Answer & solution

Answer: Use EXISTS to correlate enrollment and teaching to each student. (no official key was published)

A student qualifies if some enrollment has their roll number and grade A, and some teaching row for that enrollment's course has tname='Ramesh'. The course number is the join between ENROLL and TEACH; names need not be unique teacher identifiers.

SELECT s.rollno, s.sname
FROM STUDENTS AS s
WHERE EXISTS (
  SELECT 1 FROM ENROLL AS e
  JOIN TEACH AS t ON t.cno=e.cno
  WHERE e.rollno=s.rollno
    AND e.grade='A' AND t.tname='Ramesh'
);

EXISTS yields one truth value for each outer student, so several qualifying courses or several teachers named Ramesh do not duplicate the output. A plain join version would need DISTINCT for that purpose. The COURSES relation is unnecessary because the query does not request a course name or another condition on it. No requirement says the student must have an A in every Ramesh-taught course; replacing EXISTS with universal division would answer a stronger question.

The source TEACH attribute is printed cao but described as course number cno, and its FD list prints sdaddr for saddr. These obvious naming inconsistencies are explicitly normalized in the query rather than treated as extra attributes.

Original paper, page 14