I’m having some trouble finding out the proper way to approach this question.
Database tables:
Student(stdID[pk], stdName, stdSet)
Enrollment(stdID[pk], crsID[pk], year[pk], semester[pk], grade)
Offering(crsID[pk], year[pk], semester[pk], instrID)
Course(crsID[pk], crsTitle, creditHrs)
Instructor(instrID[pk], instrName, dept)
“Find all instructors who had taught, from 2006 to 2008, a given student A01234567. List the student ID, instructorID, and year. Do not duplicate rows.”
What I originally had was this:
SELECT DISTINCT stdID, instrID, year
FROM Student s
JOIN Instructor i ON Offering o (o.instrID = i.instrID)
JOIN Offering o ON Course c (c.crsID = o.crsID)
WHERE stdID = 'A01234567'
AND date BETWEEN (2006 AND 2008);
However, this is not entirely correct. I have notes that it’s something to do with “missing 2 columns from Offering join”, but I don’t know what that means…
Conceptually, I would start with the
Instructortable, since that’s what I want to return.I’d join that to the
Offeringtable, so I have all theOfferingfor each instructor.(But we don’t really need anything from the
Instructortable at all, since we have InstrId in theOfferingtable.)Then I’d join that to
Enrollment, to basically get all the students that were enrolled in each course offering.Then I’d join that to
Student. But again, we don’t really need anything from theStudenttable, we already havestdIdin theEnrollmenttable.The tricky part is that join between
OfferingandEnrollment. The join predicate will need to be onyearandsemester, as well ascrsId.So, to satisfy the stated requirements, only two tables need to be queried:
There’s a peculiarity with the database design. Consider what happens when there are two or more instructors teaching a given course in the same year and semester.
But the student was enrolled in only one of those.
This query will pick up the instructors of both offerings.
It’s a problem with the query, but the given database design doesn’t give us any way to resolve the issue… because there’s no way to tell which instructor the student had.
The query satisfies the stated requirements, but the results will be a bit odd, showing a particular student had six different instructors for ‘Calc I’ in the fall of 2006.
Unless there’s something that’s eluding me here.
EDIT:
As Emily Litella would have intoned, “Never mind…”. There’s a unique constraint on the
Offeringtable. There can be only one offering of a givencrsIdin a semester. So there’s no problem there. (Except that it’s peculiar that there would be only one offering of a given course in a given semester.)TheGROUP BYis not necessary. The constraints already guarantee that there will be no duplicate rows returned.Actually, I think the GROUP BY is needed, because it’s possible that the student was enrolled in two separate courses led by the same instructor. The purpose of the
GROUP BYis to eliminate any duplicate rows from the result set (as indicated in the specification.)