Reputation: 758
I have one table called Exams composed by the columns student and exam and i need to find all the students that took a proper subset of the Exams taken by the student A.
A sample of data could be:
student exam
A 1
A 2
B 1
B 3
C 1
C 2
D 1
And the result should be
student
D
Because only D took a proper subset of the exams taken by A, B is not in the result because he took an exam that A has not taken
What i came up with so far is:
take all the exams that the student A has taken
examsA ← π exam (σ student='A' (Exams))
divide the Exams relation by the exams the student A as taken
studentsNoGood ← Exams ÷ examsA
now i have all the students that took exactly the same exams and those who took more exams, by substracting i find only those who took less exams and those who did not take a subset of the student 'A' exams.
lessExamsOrNotSubset ← Exams - studentNoGood
And then im stuck on how to differentiate those with less exams and those who took unrelated exams
With 'a proper subset' i mean that with 2 sets D and E, D is a proper subset of E iff D is contained in E and D is not equal to E, so there is an element of E that is not in D.
I am using the relational algebra found in the book Fundamentals of Database Systems (Elmasri, Navathe). page 239
Upvotes: 1
Views: 454
Reputation: 27424
To find the students that took exams unrelated to those of A first find those exams:
R1 ← π exam (Exams) - examsA
then find the students with at least one exam in this set:
studentsUnrelated ← π student (Exams ⨝(Exams.exam = R1.exam) R1)
Then you can remove also these students to find those that have taken only a proper subset of the exams of A.
Upvotes: 1