100% found this document useful (1 vote)
942 views6 pages

Question 1: Make A List of Project Numbers For Projects That Involve An Employee Whose Last Name Is Smith', Either As A Worker or As A Manager of The Department That Controls The Project

This document contains 13 SQL queries related to retrieving employee, department, and project information from various tables in a database. The queries return data such as lists of projects involving employees with a certain last name, names of employees with multiple dependents, names of employees without dependents, names of managers with dependents, and more.

Uploaded by

Yousef Mohammed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
942 views6 pages

Question 1: Make A List of Project Numbers For Projects That Involve An Employee Whose Last Name Is Smith', Either As A Worker or As A Manager of The Department That Controls The Project

This document contains 13 SQL queries related to retrieving employee, department, and project information from various tables in a database. The queries return data such as lists of projects involving employees with a certain last name, names of employees with multiple dependents, names of employees without dependents, names of managers with dependents, and more.

Uploaded by

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

Question 1 : Make a list of project numbers for projects that involve an

employee whose last name is ‘Smith’, either as a worker or as a manager of


the department that controls the project.

(SELECT PNUMBER
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM=DNUMBER
AND MGRSSN=SSN AND
LNAME=‘Smith’)
UNION
FROM PROJECT, WORKS_ON, EMPLOYEE
WHERE PNUMBER=PNO AND ESSN=SSN AND LNAME=‘Smith’)
SELECT DISTINCT PNUMBER
FROM PROJECT
WHERE PNUMBER IN (SELECT PNUMBER
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM=DNUMBER
AND MGRSSN=SSN
AND LNAME=’Smith’)
OR
PNUMBER IN (SELECT PNOFROM WORKS_ON, EMPLOYEE
WHERE ESSN=SSN AND LNAME=‘Smith’)
2. List the names of all employees with two or more dependents.

SELECT Lname, COUNT (*) AS Number_of_dependents


FROM Employee, Dependent
WHERE Ssn = Essn
GROUP BY Lname
HAVING COUNT (*) 2 ;

3. Retrieve the names of employees who have no dependents.


SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE NOT EXISTS (SELECT *FROM DEPENDENTWHERE SSN=ESSN)
List the names of managers who have at least one dependent.

4. List the names of managers who have at least one dependent.


WHERE EXISTS
(SELECT *ANDEXISTS
(SELECT *FROM DEPARTMENT
WHERE SSN=MGRSSN)
5. Retrieve the names of all 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

6. List the names of all 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;

7. Find the names of all 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;

8. 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;
9. Retrieve the names of all employees who do not work on any project.

SELECT LNAME, FNAME

FROM EMPLOYEE

WHERE NOT EXISTS

( SELECT *FROM WORKS_ON

WHERE ESSN=SSN )

10. For each department, retrieve the department name and the average
salary of all employees working in that department.

SELECT DNAME, AVG (SALARY)


FROM DEPARTMENT, EMPLOYEE
WHERE DNUMBER=DNO GROUP BY DNAME;

11. Retrieve the average salary of all female employees.


SELECT AVG (SALARY)
FROM EMPLOYEE
WHERE SEX='F'
12. Find the names and addresses of all 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

LOCATION='Houston' )

AND NOT EXISTS

( SELECT *

FROM DEPT_LOCATIONS

WHERE DNO=DNUMBER AND DLOCATION='Houston' )


13. List the last names of all department managers who have no dependents.

SELECT LNAME, FNAME

FROM EMPLOYEE

WHEREEXISTS

( SELECT *FROM DEPARTMENT

WHERE SSN=MGRSSN )

AND NOT EXISTS

( SELECT *FROM DEPENDENT

WHERE SSN=ESSN )

You might also like