To answer each query using relational algebra given the relational schema:
Schema:
- Student(Id, Name, Age, Address)
- Teacher(Teacher-Id, Teacher-Name, Dept-Id)
- Course(Dept-Id, Course-Id, Course-Name)
- Department(Dept-Id, Dept-Name)
- Teaching(Teacher-Id, Course-Id, Student-Id)
i. List the names of all teachers who are from the "Computer" department.
Relational Algebra Expression:
[
\pi_{\text{Teacher-Name}} \left( \sigma_{\text{Dept-Name} = 'Computer'} (\text{Department}) \ \underset{\text{Dept-Id}}{\Join} \ \text{Teacher} \right)
]
Explanation:
- Select departments where
Dept-Name is 'Computer':
[
\sigma_{\text{Dept-Name} = 'Computer'} (\text{Department})
]
- Join this result with
Teacher on Dept-Id to find teachers in the "Computer" department:
[
\sigma_{\text{Dept-Name} = 'Computer'} (\text{Department}) \ \underset{\text{Dept-Id}}{\Join} \ \text{Teacher}
]
- Project the
Teacher-Name to get only the names:
[
\pi_{\text{Teacher-Name}} \left( \text{Result from Step 2} \right)
]
ii. Retrieve the names of all students who have registered for the course "Database".
Relational Algebra Expression:
[
\pi_{\text{Student.Name}} \left( \left( \left( \sigma_{\text{Course-Name} = 'Database'} (\text{Course}) \ \underset{\text{Course-Id}}{\Join} \ \text{Teaching} \right) \ \underset{\text{Student-Id}}{\Join} \ \text{Student} \right) \right)
]
Explanation:
- Select courses where
Course-Name is 'Database':
[
\sigma_{\text{Course-Name} = 'Database'} (\text{Course})
]
- Join this with
Teaching on Course-Id to find teaching records for "Database":
[
\sigma_{\text{Course-Name} = 'Database'} (\text{Course}) \ \underset{\text{Course-Id}}{\Join} \ \text{Teaching}
]
- Join the result with
Student on Student-Id to find students registered for "Database":
[
\left( \text{Result from Step 2} \right) \ \underset{\text{Student-Id}}{\Join} \ \text{Student}
]
- Project the
Student.Name to get only the names:
[
\pi_{\text{Student.Name}} \left( \text{Result from Step 3} \right)
]
iii. List the names of all courses and their departments.
Relational Algebra Expression:
[
\pi_{\text{Course-Name}, \text{Dept-Name}} \left( \text{Course} \ \underset{\text{Dept-Id}}{\Join} \ \text{Department} \right)
]
Explanation:
- Join
Course with Department on Dept-Id to associate courses with their departments:
[
\text{Course} \ \underset{\text{Dept-Id}}{\Join} \ \text{Department}
]
- Project the
Course-Name and Dept-Name to get the names of courses and their departments:
[
\pi_{\text{Course-Name}, \text{Dept-Name}} \left( \text{Result from Step 1} \right)
]
iv. Find the names of teachers who are from the "Civil" department and teaching the course "Mechanics".
Relational Algebra Expression:
[
\pi_{\text{Teacher-Name}} \left( \left( \left( \sigma_{\text{Dept-Name} = 'Civil'} (\text{Department}) \ \underset{\text{Dept-Id}}{\Join} \ \text{Teacher} \right) \ \underset{\text{Teacher-Id}}{\Join} \ \left( \left( \sigma_{\text{Course-Name} = 'Mechanics'} (\text{Course}) \ \underset{\text{Course-Id}}{\Join} \ \text{Teaching} \right) \right) \right) \right)
]
Explanation:
- Select the department with
Dept-Name 'Civil':
[
\sigma_{\text{Dept-Name} = 'Civil'} (\text{Department})
]
- Join this with
Teacher on Dept-Id to find teachers in the "Civil" department:
[
\sigma_{\text{Dept-Name} = 'Civil'} (\text{Department}) \ \underset{\text{Dept-Id}}{\Join} \ \text{Teacher}
]
- Select the course where
Course-Name is 'Mechanics':
[
\sigma_{\text{Course-Name} = 'Mechanics'} (\text{Course})
]
- Join this with
Teaching on Course-Id to find teaching records for "Mechanics":
[
\sigma_{\text{Course-Name} = 'Mechanics'} (\text{Course}) \ \underset{\text{Course-Id}}{\Join} \ \text{Teaching}
]
- Join the results from Steps 2 and 4 on
Teacher-Id to find teachers from "Civil" teaching "Mechanics":
[
\left( \text{Result from Step 2} \right) \ \underset{\text{Teacher-Id}}{\Join} \ \left( \text{Result from Step 4} \right)
]
- Project the
Teacher-Name to get the names:
[
\pi_{\text{Teacher-Name}} \left( \text{Result from Step 5} \right)
]
Each relational algebra expression follows the logical steps needed to process the query based on the given schema. The expressions use selection (σ), projection (π), and natural join (⋈) operations to filter and combine data from different relations.