Reputation: 1975
This has really been bugging me for a while. I'm trying to write an SQL query to return all courses (names), their instructors (names),and their prerequisite course (if they exist). But my query just isn't working and I don't know what to do anymore. I keep getting an empty set returned.
SELECT course, instructors.inst,
something
FROM courses a inner join instructors ON a.course_id = instructors.inst_id
INNER JOIN courses b ON a.prereq = b.course_id where a.prereq IS NOT NULL;
Am I doing something wrong with the multiple inner joins? Any help would be greatly appreciated.
Upvotes: 0
Views: 2461
Reputation: 80
Try this
SELECT a.course_name AS "Course", instructors.inst_name AS "Instructor",
b.course_name AS "Prereq"
FROM courses a
LEFT OUTER JOIN instructors ON a.course_id = instructors.inst_id
LEFT OUTER JOIN courses b ON a.prereq = b.course_id
Upvotes: 1
Reputation: 208
LEFT OUTER JOIN should give You the result.Inner join will return results only in case you have data in all of the joined tables.
Upvotes: 1