362 views
0 votes
Retrieve the names of employees who work on exactly one project that is controlled by their department. (It does not matter how many non-departmental projects they work on.) Also show the department name, and the project name and location. Show results in ascending alpha order (by last name and then first name).

Column Headings: FNAME LNAME DNAME PNAME PLOCATION

I currently have a list of names who work on projects controlled by their departments. But I am unsure on how to trim down the table so that it shows the employees who work on exactly one project controlled by there department. (ie, james borg and jennifer wallace). Values are subject to change, so i can not just do a direct comparison to their last names or something.

SQL Query I produced:

select distinct fname, lname, dname, pname, plocation
from works_on, project, employee, department
where works_on.pno = project.pnumber
and works_on.essn = employee.ssn
and project.dnum = employee.dno
and department.dnumber = employee.dno
order by lname, fname;

1 Answer

2 votes

Answer:

select distinct fname, lname, dname, pname, plocation

from works_on, project, employee, department

where works_on.pno = project.pnumber

and works_on.essn = employee.ssn

and project.dnum = employee.dno

and department.dnumber = employee.dno AND employee.ssn IN(

select essn

FROM works_on

GROUP BY essn

HAVING COUNT(essn)=1

)

order by lname, fname;

Step-by-step explanation:

User Hemamalini
by
3.9k points