Student (Sid, Sname, age)
Course Info (Cid, Cname, Instructor SSN)
Enroll (Sid, Cid, grade)
The relational algebra expression for “find the Cid’s of courses enrolled by two different students” :
1. “find the Cid’s of courses enrolled by at least two (different) students” :
$E_1(S,C,G) \leftarrow Enroll$ // Renaming of enroll relation into E_1 with attributes names as $S,C,G$ in that order.
$At\_least2 \leftarrow \prod_{cid} (E_1 \Join_{(C = Cid \, \wedge \,S \neq Sid)} Enroll)$ //Finding courses which have at least two students.
2. “find the Cid’s of courses enrolled by at least three (different) students” :
Similar to what we did in the previous query.
$E_1(S1,C1,G1) \leftarrow Enroll$ // Renaming of enroll relation into E_1 with attributes names as $S1,C1,G1$ in that order.
$E_2(S2,C2,G2) \leftarrow Enroll$ // Renaming of enroll relation into E_2 with attributes names as $S2,C2,G2$ in that order.
$At\_least3 \leftarrow \prod_{cid} ( (E_1 \Join_{(C = Cid \, \wedge \,S \neq Sid)} Enroll) \,\, \Join_{(C2 = C1 \, \wedge \, S2 \neq S1 \, \wedge \, S2 \neq Sid)} E_2 )$ //Finding courses which have at least three students.
3. “find the Cid’s of courses enrolled by exactly two (different) students” :
$Exactly2 \leftarrow At\_least2 - At\_least3$