I have the following tables:
COURSE
+----------+-------------------------+
| course_id | course_name |
+-----------+------------------------+
| 1 | s001 |
| 2 | s002 |
| 3 | s003 |
| 4 | s004 |
+-----------+------------------------+
COURSE_PREREQUISITES
+----------+-------------------------+
| course_id | prerequisite_course_id |
+-----------+------------------------+
| 3 | 2 |
+-----------+------------------------+
| 4 | 1 |
+-----------+------------------------+
| 4 | 2 |
+-----------+------------------------+
| 4 | 3 |
+-----------+------------------------+
My question is: Given a list of Course IDs a student has completed, how can I obtain a list of courses the student is eligible to participate in?
Example
If a student has completed course_id
2, the query should return courses: 1, (since it has no prerequisites) and 3 but not 4 since 4 has 1,3 as prerequisites as well.
Attempt at a solution
I have tried using the IN
statement like so for a student who has completed the course 2:
SELECT DISTINCT course_id FROM COURSE_PREREQUISITES
WHERE prerequisite_course_id IN (2)
but it obviously fails since it returns all courses that satisfy at least one prerequisite which is not what I need.
I came across this question which is similar: Select rows that match all items in a list. But the provided solution does not work in my case, since the number of prerequisites for a course is not fixed.
Finally, I would also like to know if NOSQL databases (couchDB, mongoDB) are better suited for problems like these.