Problem 1a: Find the sid of Students who have been enrolled in all Courses of department “CMPT”.
To retrieve the sid of students who are enrolled in every course offered by the "CMPT" department, we can use the division operation in relational algebra. The division operator is useful for queries that require matching all entries in one set against another.
Relational Algebra Expression:
[
\pi_{\text{sid}}(\text{Enrollment}) \div \pi_{\text{cid}}(\sigma_{\text{department} = \text{'CMPT'}}(\text{Course}))
]
Explanation:
-
Select CMPT Courses:
- (\sigma_{\text{department} = \text{'CMPT'}}(\text{Course}))
Selects all tuples from the Course relation where the department is "CMPT".
-
Project Course IDs:
- (\pi_{\text{cid}}(\sigma_{\text{department} = \text{'CMPT'}}(\text{Course})))
Projects the cid (Course ID) of these CMPT courses.
-
Project Enrollments:
- (\pi_{\text{sid}}(\text{Enrollment}))
Projects the sid (Student ID) from the Enrollment relation.
-
Division:
- The division operation (\div) ensures that we select those
sid values that are associated with all cid values from the CMPT department.
Problem 1b: Find the cid and title of Courses that have had at least two different Sections.
To identify courses that have multiple sections, we can perform a self-join on the Section relation to find pairs of sections for the same course but different semesters (or any other distinguishing attribute).
Relational Algebra Expression:
[
\pi_{\text{C.cid}, \text{C.title}} \left( \text{Course} \ \text{C} \ \bowtie \ \left( \rho_{\text{S1}}(\text{Section}) \bowtie_{\text{S1.cid} = \text{S2.cid} \land \text{S1.semester} \neq \text{S2.semester}} \rho_{\text{S2}}(\text{Section}) \right) \right)
]
Explanation:
-
Rename Sections for Self-Join:
- (\rho_{\text{S1}}(\text{Section})) and (\rho_{\text{S2}}(\text{Section}))
Renames the Section relation to S1 and S2 to differentiate between two instances of sections.
-
Self-Join on Same Course but Different Semesters:
- (\text{S1} \bowtie_{\text{S1.cid} = \text{S2.cid} \land \text{S1.semester} \neq \text{S2.semester}} \text{S2})
Joins S1 and S2 where they belong to the same course (cid is the same) but are in different semesters.
-
Join with Course Relation:
- (\text{Course} \ \text{C} \bowtie ) (result of the self-join)
Joins the resulting sections with the Course relation to bring in the title attribute.
-
Project Desired Attributes:
- (\pi_{\text{C.cid}, \text{C.title}})
Projects the cid and title of courses that have multiple sections.
Summary
- 1a: Use division to find students enrolled in all CMPT courses.
- 1b: Use a self-join on the
Section relation to identify courses with multiple sections and then project their cid and title.
Final Relational Algebra Expressions:
a)
[
\pi_{\text{sid}}(\text{Enrollment}) \div \pi_{\text{cid}}(\sigma_{\text{department} = \text{'CMPT'}}(\text{Course}))
]
b)
[
\pi_{\text{C.cid}, \text{C.title}} \left( \text{Course} \ \text{C} \bowtie \left( \rho_{\text{S1}}(\text{Section}) \bowtie_{\text{S1.cid} = \text{S2.cid} \land \text{S1.semester} \neq \text{S2.semester}} \rho_{\text{S2}}(\text{Section}) \right) \right)
]