Write queries in SQL using the COMPANY database of Figures 7.5 and 7.6 given below.
The results of the queries are shown for your convenience.
(a) Retrieve the names of employees in department 5 who work more than 10 hours per
week on the 'ProductX' project.
SELECT LNAME, FNAME
FROM EMPLOYEE, WORKS_ON, PROJECT
WHERE DNO=5 AND SSN=ESSN AND PNO=PNUMBER AND PNAME='ProductX' AND HOURS>10;
Another possible SQL query uses nesting as follows:
SELECT LNAME, FNAME
FROM EMPLOYEE
WHERE DNO=5 AND SSN IN ( SELECT ESSN
FROM WORKS_ON
WHERE HOURS>10 AND PNO IN ( SELECT PNUMBER
FROM PROJECT
WHERE PNAME='ProductX' ) );
Result:
LNAME FNAME
Smith John
English Joyce
(b) List the names of employees who have a dependent with the same first name as
themselves.
Result (empty):
LNAME FNAME
SELECT LNAME, FNAME
FROM EMPLOYEE, DEPENDENT
WHERE SSN=ESSN AND FNAME=DEPENDENT_NAME;
Another possible SQL query uses nesting as follows:
SELECT LNAME, FNAME
FROM EMPLOYEE
WHERE EXISTS ( SELECT *
FROM DEPENDENT
WHERE FNAME=DEPENDENT_NAME AND SSN=ESSN );
(c) Find the names of employees that are directly supervised by 'Franklin Wong'.
Result:
LNAME FNAME
Smith John
Narayan Ramesh
English Joyce
SELECT E.LNAME, E.FNAME
FROM EMPLOYEE E, EMPLOYEE S
WHERE S.FNAME='Franklin' AND S.LNAME='Wong' AND E.SUPERSSN=S.SSN;
Another possible SQL query uses nesting as follows:
SELECT LNAME, FNAME
FROM EMPLOYEE
WHERE SUPERSSN IN ( SELECT SSN
FROM EMPLOYEE
WHERE FNAME='Franklin' AND LNAME='Wong' );
(d) For each project, list the project name and the total hours per week (by all
employees) spent on that project.
Result:
PNAME SUM(HOURS)
ProductX 52.5
ProductY 37.5
ProductZ 50.0
Computerization 55.0
Reorganization 25.0
Newbenefits 55.0
SELECT PNAME, SUM (HOURS)
FROM PROJECT, WORKS_ON
WHERE PNUMBER=PNO
GROUP BY PNAME;
(e) Retrieve the names of employees who work on every project.
Result (empty):
LNAME FNAME
SELECT LNAME, FNAME
FROM EMPLOYEE
WHERE NOT EXISTS ( SELECT PNUMBER
FROM PROJECT
WHERE NOT EXISTS ( SELECT *
FROM WORKS_ON
WHERE PNUMBER=PNO AND ESSN=SSN ) );
(f) For each department, retrieve the department name, and the average salary of
employees working in that department.
Result:
DNAME AVG(SALARY)
Research 33250
Administration 31000
Headquarters 55000
SELECT DNAME, AVG (SALARY)
FROM DEPARTMENT, EMPLOYEE
WHERE DNUMBER=DNO
GROUP BY DNAME;
(g) Retrieve the average salary of all female employees.
Result:
AVG(SALARY)
31000
SELECT AVG (SALARY)
FROM EMPLOYEE
WHERE SEX='F';
(h) List the last names of department managers who have no dependents.
Result:
LNAME FNAME
Borg James
SELECT LNAME, FNAME
FROM EMPLOYEE
WHERE EXISTS ( SELECT *
FROM DEPARTMENT
WHERE SSN=MGRSSN )
AND
NOT EXISTS ( SELECT *
FROM DEPENDENT
WHERE SSN=ESSN );
(i) For each department whose average employee salary is more than $30000, retrieve
the department name and the number of employees working for that department.
Answers:
SELECT DNAME, COUNT (*)
FROM DEPARTMENT, EMPLOYEE
WHERE DNUMBER=DNO
GROUP BY DNAME
HAVING AVG (SALARY) > 30000
Result:
DNAME COUNT(*)
Research 4
Administration 3
Headquarters 1
(j) Suppose we want the number of male employees in each department rather than all
employees as in
The query may still be specified in SQL by using a nested query as follows (not all
implementations may support this type of query):
SELECT DNAME, COUNT (*)
FROM DEPARTMENT, EMPLOYEE
WHERE DNUMBER=DNO AND SEX='M' AND DNO IN
( SELECT DNO
FROM EMPLOYEE
GROUP BY DNO
HAVING AVG (SALARY) > 30000 )
GROUP BY DNAME
Result:
DNAME COUNT(*)
Research 3
Administration 1
Headquarters 1
Company Database(Figures 7.5 and 7.6 of the COMPANY database from
Elmasri and Navathe) :