|
Find employees who work on
all projects
controlled by the 'Research' department"
See: click here |
I will now explain how to answer a division query using the CONTAINS (superset) test in a number of examples
|
|
You can see that:
|
The SQL query can be formulated using the following pseudo code:
SELECT fname, lname
FROM employee
WHERE "set of projects worked on by emplyee.ssn"
CONTAINS
"set of projects controlled by department 4"
|
SELECT fname, lname
FROM employee
WHERE "set of projects controlled by department 4"
-
"set of projects worked on by emplyee.ssn"
= empty set
|
"set of projects controlled by department 4":
(SELECT pnumber
FROM project
WHERE dnum = 4)
|
SELECT ssn, fname, lname
FROM employee e
WHERE NOT EXISTS
(SELECT pnumber
FROM project /* Universe set used in the subtract operation */
WHERE
pnumber IN (SELECT pnumber
FROM project
WHERE dnum = 4)
and
pnumber NOT IN (SELECT pno
FROM works_on
WHERE essn = e.ssn)
)
(I aliased Employee as e for convenience)
|
ssn fname lname --------- ------ -------- 999887777 Alicia Zelaya 987987987 Ahmad Jabbar |
because:
Projects control by department 4: pname pnumber plocation dnum --------------- ----------- ---------- ----------- Computerization 10 Stafford 4 Newbenefits 30 Stafford 4 |
|
SELECT pname
FROM project P
WHERE "set of employees working on project P (id. by P.pnumber)"
contains
"set of employees in the Administration department"
|
Re-write:
SELECT pname
FROM project P
WHERE "set of employees in the Administration department"
−
"set of employees working on project P (id. by P.pnumber)"
= empty set
|
Use Set Difference technique:
SELECT pname
FROM project P
WHERE NOT EXISTS
(SELECT ssn
FROM employee /* Universe set of employees */
WHERE
ssn IN "set of employees
in the Administration department"
and
ssn NOT IN "set of employees
working on project P (id. by P.pnumber)"
)
|
SELECT pname
FROM project P
WHERE NOT EXISTS
(SELECT ssn
FROM employee /* Universe set of employees */
WHERE
ssn IN (SELECT ssn
FROM employee, department
WHERE dno=dnumber
AND dname='Administration')
and
ssn NOT IN (SELECT essn
FROM works_on
WHERE works_on.pno = P.pnumber)
)
|
pnumber pname ----------- --------------- 30 Newbenefits |
becasue:
The Administration department is:
dname dnumber mgrssn mgrstartdate
--------------- ----------- --------- ------------
Research 5 333445555 22-MAY-78
Administration 4 987654321 01-JAN-85
Headquarters 1 888665555 19-JUN-71
|
Project 30 (Newbenefits) is worked on by all employees in the Administration department !