I know this question is already asked here. But it's not answered properly there.
Q) Consider the following relational database schemes:
COURSES(Cno.name)
PRE-REQ(Cno, pre-Cno)
COMPLETED(student_no, Cno)
COURSES gives the number and name of all the available courses.
PRE-REQ gives the information about which courses are pre-requisites for a given course.
COMPLETED indicates what courses have been completed by students
Express the following using relational algebra:
List all the courses for which a student with student_no 2310 has completed all the pre-requisites.
Answer given here:
S ← π Cno (σ student_no=2310 (COMPLETED))
RESULT ← ((ρ (Course,Cno) (PRE−REQ))÷S)
But I found a flaw in it. Suppose
PRE-REQ COMPLETED
Cno Pre-Cno Student_no Cno
C1 C3 2310 C3
C2 C4 2310 C4
The desired result should be C1, C2 but my query will return an empty relation as C1 doesn't have C4 as its pre-requisite course and similarly, C2 doesn't have C3 as its pre-requisite course.
S RESULT
Cno Course Cno
C3
C4
Another solution which is given in one of the answers here but by using SQL is-
SELECT Pre-Req.Cno
FROM Completed, Pre-Req
WHERE student_no = '2310'
GROUP BY Pre-req.Cno
HAVING pre-Cno IN (
SELECT C.cno
FROM Completed AS C
WHERE C.student_no = '2310';
)
Is there any other possible way to write is as a relational algebra expression ?