tags:

views:

24

answers:

1

Here is the schema:

Student(Snum, Sname)
Course(Cnum, Cname)
Prerequisite(Cnum, Prereq)
Professor(Pnum,Pname, Dept, Office)
Enrollment(Snum, Cnum, Term, Section, Mark)
Schedule(Cnum, Term, Section, Days, Time, Room)
Class(Cnum, Term, Section, Instructor)

The full question is:

Find professors (Pnum, Pname, Dept) who whenever taught a course in the Class relation, either the course does not have any prerequisite or the professor has also taught each of the prerequisites of the course. A professor taught a course exactly when the professor taught a class (a section of a course) in some term.

My starting point so far...

SELECT * FROM Class c
LEFT JOIN Prerequisite p ON p.Cnum = c.Cnum
WHERE p.Cnum IS NULL

I know how to find the courses that the professors taught that have no prerequisites, and therefore they satisfy part of the condition. The next step is finding if they have taught the prerequisites if there are any. Also, my current query only selects a single prerequisite for every class, when there can really be multiple prerequisites for a class.

Note: Cnum and Prereq are both foreign keys to Cnum

Thanks for any help! This is a toughie.

EDIT: I came up with the final answer!

SELECT prof.Pnum, prof.Pname, prof.Dept FROM Professor prof
WHERE prof.Pnum NOT IN
(
SELECT DISTINCT c.Instructor FROM Class c
RIGHT JOIN Prerequisite p ON p.Cnum = c.Cnum
LEFT JOIN Class cpre ON cpre.Cnum = p.Prereq AND cpre.Instructor = c.Instructor
WHERE cpre.Instructor IS NULL AND c.Instructor IS NOT NULL
)
+1  A: 

Since this is homework, here's a hint...

If Professor taught all prerequisites then Pnum does not EXISTS in subselect of Prerequisites for a Class' Course left joined to actual Classes that Professor taught in given Term where actual class is NULL (in other words where there are no prerequisites for a class taught that was not actually thought).

Unreason
ok, so if i understand correctly, i am going to try making the subquery first, which selects the class taught by the professor and its prerequisites, and left joins that instructor's class to every prerequisite. my question now is how can i get all of the prerequisites?
Garrett
@Garrett, good progress; it might be correct, but it might not - the question says 'A professor taught a course exactly when the professor taught a class (a section of a course) in some term.' - this suggests that if professor though some prerequisites in one term and some in another that such case would not qualify as teaching all prerequisites for a course. If such interpretation makes sense you might need to go a bit further; other comments - distinct should not matter when doing NOT IN (and would in practice slow things down).
Unreason