Select elname efname from employee e employee s where

Info icon This preview shows pages 7–10. Sign up to view the full content.

View Full Document Right Arrow Icon
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.
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
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.
Image of page 8
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 8.14 Specify the following additional queries on the database of Figure 5.5 in SQL. Show the query results if applied to the database of Figure 5.6. (a) For each department whose average employee salary is more than $30000, retrieve the department name and the number of employees working for that department. (b) Suppose we want the number of male employees in each department rather than all employees as in Exercise 8.14(a). Can we specify this query in SQL? Why or why not? Answers: (a) SELECT DNAME, COUNT (*) FROM DEPARTMENT, EMPLOYEE WHERE DNUMBER=DNO GROUP BY DNAME HAVING AVG (SALARY) > 30000 Result: DNAME DNUMBER COUNT(*) Research 5 4 Administration 4 3 Headquarters 1 1 (b) 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
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern