DB2007 HW03 Answer
DB2007 HW03 Answer
1. Specify the following queries on the COMPANY relational database schema shown in
Figure 5.5, using the relational operators discussed in this chapter. Also show the result
of each query as it would apply to the database state of Figure 5.6.
(a) Retrieve the names of employees in department 5 who work more than 10 hours per
week on the 'ProductX' project.
(b) List the names of employees who have a dependent with the same first name as
themselves.
(c) Find the names of employees that are directly supervised by 'Franklin Wong'.
(d) For each project, list the project name and the total hours per week (by all employees)
spent on that project.
(e) Retrieve the names of employees who work on every project.
(f) Retrieve the names of employees who do not work on any project.
(g) For each department, retrieve the department name, and the average salary of employees
working in that department.
(h) Retrieve the average salary of all female employees.
(i) Find the names and addresses of employees who work on at least one project located in
Houston but whose department has no location in Houston.
(j) List the last names of department managers who have no dependents.
Answers:
In the relational algebra, as in other languages, it is possible to specify the same query
in multiple ways. We give one possible solution for each query.
Result:
LNAME FNAME
Smith John
English Joyce
1
(b) E ← (EMPLOYEE) SSN=ESSN AND FNAME=DEPENDENT_NAME (DEPENDENT)
R ← Π LNAME,FNAME (E)
Result (empty):
LNAME FNAME
Result:
LNAME FNAME
Smith John
Narayan Ramesh
English Joyce
Result:
PNAME TOT_HRS
ProductX 52.5
ProductY 37.5
ProductZ 50.0
Computerization 55.0
Reorganization 25.0
Newbenefits 55.0
Result (empty):
LNAME FNAME
2
NON_WORKING_EMPS ← ALL_EMPS - WORKING_EMPS
Result (empty):
LNAME FNAME
Result:
DNAME AVG_SAL
Research 33250
Administration 31000
Headquarters 55000
Result:
AVG_F_SAL
31000
(i) E_P_HOU(SSN) ←
D_NO_HOU ←
Result:
LNAME FNAME ADDRESS
Wallace Jennifer 291 Berry, Bellaire, TX
3
RESULT_EMPS ← DEPT_MANAGERS - EMPS_WITH_DEPENDENTS
Result:
LNAME FNAME
Borg James
2.
Answer: R2
Answer: R5
Answer: R4
4
3.
Answer:
(a) PQRABC
10 a 5 10 b 6
10 a 5 10 b 5
25 a 6 25 c 3
(b) PQRABC
15 b 8 10 b 6
15 b 8 10 b 5
(c) PQRABC
10 a 5 10 b 6
10 a 5 10 b 5
15 b 8 null null null
25 a 6 25 c 3
(d) PQRABC
15 b 8 10 b 6
null null null 25 c 3
15 b 8 10 b 5
(e) PQR
10a 5
15 b 8
25 a 6
10b 6
25 c 3
10b 5
(f) PQRABC
10 a 5 10 b 5
4. Specify queries (a), (b), (c), (e), (f), (i), and (j) of Question 1 in both the tuple relational
calculus and the domain relational calculus.
Answer:
(a) Retrieve the names of employees in department 5 who work more than 10 hours per
week on the 'ProductX' project.
5
{ qs | EMPLOYEE(qrstuvwxyz) AND z=5 AND (∃ a) (∃ b) (∃ e)
(b) List the names of employees who have a dependent with the same first name as
themselves.
(c) Find the names of employees that are directly supervised by 'Franklin Wong'.
6
(f) Retrieve the names of employees who do not work on any project.
(i) Find the names and addresses of employees who work on at least one project located in
Houston but whose department has no location in Houston.
(j) List the last names of department managers who have no dependents.