0% found this document useful (0 votes)
80 views4 pages

SQL Solutions - 8 - 13

The document contains several SQL queries and their results applied to a COMPANY database. The queries retrieve information such as: (1) Names of employees in department 5 working over 10 hours on project 'ProductX'; (2) Employees with dependents sharing their first name; (3) Employees directly supervised by 'Franklin Wong'; (4) Project names and total hours worked on each project; (5) Average salary by department and for female employees; (6) Names of employees working on projects in Houston but whose department is not in Houston; (7) Last names of managers without dependents.

Uploaded by

Devdutt Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views4 pages

SQL Solutions - 8 - 13

The document contains several SQL queries and their results applied to a COMPANY database. The queries retrieve information such as: (1) Names of employees in department 5 working over 10 hours on project 'ProductX'; (2) Employees with dependents sharing their first name; (3) Employees directly supervised by 'Franklin Wong'; (4) Project names and total hours worked on each project; (5) Average salary by department and for female employees; (6) Names of employees working on projects in Houston but whose department is not in Houston; (7) Last names of managers without dependents.

Uploaded by

Devdutt Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

8.13 Specify the queries of Exercise 7.18 in SQL.

Show the result of each query if applied to the


COMPANY database of Figure 7.6.

Note: More than one SQL solution may be provided for the same query.

(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.

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 )

Result (empty):
LNAME FNAME
- ---------

(c) Find the names of employees who are directly supervised by 'Franklin Wong'.

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' )

Result:
LNAME FNAME
Smith John
Narayan Ramesh
English Joyce

(d) For each project, list the project name and the total hours per week (by all employees) spent
on that project.

SELECT PNAME, SUM (HOURS)


FROM PROJECT, WORKS_ON
WHERE PNUMBER=PNO
GROUP BY PNAME

Result:
PNAME SUM(HOURS)
ProductX 52.5
ProductY 37.5
ProductZ 50.0
Computerization 55.0
Reorganization 25.0
Newbenefits 55.0

(e) Retrieve the names of employees who work on every project.

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 ) )

Result (empty):
LNAME FNAME
- ---------
(f) Retrieve the names of employees who do not work on any project.

SELECT LNAME, FNAME


FROM EMPLOYEE
WHERE NOT EXISTS ( SELECT *
FROM WORKS_ON
WHERE ESSN=SSN )

Result (empty):
LNAME FNAME
- ---------

(g) For each department, retrieve the department name, and the average salary of employees
working in that department.

SELECT DNAME, AVG (SALARY)


FROM DEPARTMENT, EMPLOYEE
WHERE DNUMBER=DNO
GROUP BY DNAME

Result:
DNAME AVG(SALARY)
Research 33250
Administration 31000
Headquarters 55000

(h) Retrieve the average salary of all female employees.

SELECT AVG (SALARY)


FROM EMPLOYEE
WHERE SEX='F'

Result:
AVG(SALARY)
31000

(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.

SELECT LNAME, FNAME, ADDRESS


FROM EMPLOYEE
WHERE EXISTS ( SELECT *
FROM WORKS_ON, PROJECT
WHERE SSN=ESSN AND PNO=PNUMBER AND PLOCATION='Houston' )
AND
NOT EXISTS ( SELECT *
FROM DEPT_LOCATIONS
WHERE DNO=DNUMBER AND DLOCATION='Houston' )

Result:
LNAME FNAME ADDRESS
Wallace Jennifer 291 Berry, Bellaire, TX

(j) List the last names of department managers who have no dependents.

SELECT LNAME, FNAME


FROM EMPLOYEE
WHERE EXISTS ( SELECT *
FROM DEPARTMENT
WHERE SSN=MGRSSN )
AND
NOT EXISTS ( SELECT *
FROM DEPENDENT
WHERE SSN=ESSN )

Result:
LNAME FNAME
Borg James

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy