In: Computer Science
Q2: Get SSN and the last name of married female employees who work on three or more projects Write Q2 in Relational Algebra:
Solution:
Schema of required tables
Employee ( SSN,LNAME,FNAME,SEX)
Dependent(ESSN,Relationship)
Workson(ESSN,PNO)
Write relation algebra query part by part and then combine all to get the final query.
First find the female employees from employee table
Female_Employees = ∏ SSN ( ( σ SEX = ‘F’ (Employee)))
After finding the female employees find the employee ids whose relationship is spouse . This gives the result of married employees.
So Query is
Married_Employees = ∏ESSN ( ( σRelationship = ‘Spouse’ (Dependent) ))
Now combine the first two queries to get the female married employees i.e
Married_Female_Employees = ∏SSN (Female_Employees ⋈SSN=ESSN Married_Employees )
After that find the employees whose count of project numbers is greater than or equal to 3 from workson table.
So the query is
Employees_more_than_3_projects = ( σCount(pno) >= 3 (ESSN F COUNT(pno) WORKS_ON))
So the above query gives the results of more than 3 projects employees.
And then Join Married_Female_Employees and Employees_more_than_3_projects on SSN Column
output1 = ∏SSN(Married_Female_Employees ⋈ SSN=ESSN Employees_more_than_3_projects )
Now do output1 natural join with employee table on common column ssn to get the last name of employee.
i.e
Final_Output= ∏SSN,LNAME (Employee * output1 )
So the final query is
Final_Query = ∏SSN,LNAME (Employee * (∏SSN((∏SSN ((∏ SSN ( ( σ SEX = ‘F’ (Employee))))⋈SSN=ESSN (∏ESSN ( ( σRelationship = ‘Spouse’ (Dependent) ))))) ⋈ SSN=ESSN (( σCount(pno) >= 3 (ESSN F COUNT(pno) WORKS_ON))) )))
Note: It's very difficult to understand the query if we write directly so derive each part and then combine all parts is better.
Note: if you have any queries please post a comment thanks a lot..always available to help you...