Assignment (Solution): Basic Operators in Relational Algebra
Database Tables
Table 1: Student Table 2: Department
StuID Name Age DeptID DeptID DeptName Location
S1 Ali 21 D1 D1 Computer Science Lahore
S2 Sara 19 D2 D2 Information Tech Karachi
S3 Ahmed 22 D1 D3 Electrical Engg Lahore
S4 Fatima 20 D3
S5 Hassan 23 D2
Q1. (Projection Operator π)
Write relational algebra expressions to:
1. Display only the names of all students.
Expression: π Name (Student)
2. Show the DeptName of all departments without repetition.
Expression: π DeptName (Department)
Q2. (Selection Operator σ)
Write relational algebra expressions to:
1. Find details of students who are older than 20.
Expression: σ Age>20 (Student)
2. List all departments located in “Lahore”.
Expression: σ Location=′Lahore′ (Department)
Q3. (Union Operator ∪)
Suppose we create two separate relations from the Student table:
• CS_Students = Students in Department D1
• IT_Students = Students in Department D2
Write a relational algebra expression to find all students who are either in CS or IT departments.
Expression: σ DeptID=′D1′ (Student) ∪ σ DeptID=′D2′ (Student)
Q4. (Set Difference Operator −)
Using the CS_Students and IT_Students relations above, find students who are in CS but not in IT.
Expression: σ DeptID=′D1′ (Student) – σ DeptID=′D2′ (Student)
Q5. (Rename Operator ρ)
Write relational algebra expressions to:
1. Rename the Student relation to Learner.
Expression: ρ Learner (Student)
2. Rename the attribute Name in Student relation to FullName.
Expression: ρ Student(StuID,FullName,Age,DeptID) (Student)
3. Rename DeptName in Department table to DepartmentTitle.
Expression: ρ Department(DeptID,DepartmentTitle,Location) (Department)
Q6. (Cartesian Product ×)
Write a relational algebra expression to list all possible combinations of students and departments.
Expression:
Student × Department
Evaluate: every student paired with every department → 5×3=15 tuples.
Result (showing S.DeptID and D.DeptID to avoid confusion):
StuID Name Age S.DeptID D.DeptID DeptName Location
S1 Ali 21 D1 D1 Computer Science Lahore
S1 Ali 21 D1 D2 Information Tech Karachi
S1 Ali 21 D1 D3 Electrical Engg Lahore
S2 Sara 19 D2 D1 Computer Science Lahore
S2 Sara 19 D2 D2 Information Tech Karachi
S2 Sara 19 D2 D3 Electrical Engg Lahore
S3 Ahmed 22 D1 D1 Computer Science Lahore
S3 Ahmed 22 D1 D2 Information Tech Karachi
S3 Ahmed 22 D1 D3 Electrical Engg Lahore
S4 Fatima 20 D3 D1 Computer Science Lahore
S4 Fatima 20 D3 D2 Information Tech Karachi
S4 Fatima 20 D3 D3 Electrical Engg Lahore
S5 Hassan 23 D2 D1 Computer Science Lahore
S5 Hassan 23 D2 D2 Information Tech Karachi
S5 Hassan 23 D2 D3 Electrical Engg Lahore