0% found this document useful (0 votes)
12 views

1233789_ SQL PROBLEMS SET

The document contains a set of SQL problems and their corresponding solutions related to employee data management. It includes various queries to retrieve employee details, department information, project participation, and salary averages. The results of these queries are also presented, showcasing specific employee names and departmental statistics.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

1233789_ SQL PROBLEMS SET

The document contains a set of SQL problems and their corresponding solutions related to employee data management. It includes various queries to retrieve employee details, department information, project participation, and salary averages. The results of these queries are also presented, showcasing specific employee names and departmental statistics.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Nguyễn Duy Thái- 2163789

SQL problems set:

SQL statements:
a. Retrieve the name, birth date and address of the employee(s)
whose name is “John B.

Smith

SELECT Name, BirthDate, Address

FROM Employee

WHERE Name = 'John B. Smith';

b. Retrieve the names of all employees in the “Administration”


department.

sql
SELECT Employee.Name FROM Employee JOIN Department ON
Employee.DeptID = Department.DeptID WHERE Department.DeptName =
'Administration';

c. Retrieve the names of all employees in department 5 who work


more than 10 hours per week on the ProductX project.

SELECT Employee.Name

FROM Employee

JOIN WorksOn ON Employee.EID = WorksOn.EID

JOIN Project ON WorksOn.ProjectID = Project.ProjectID

WHERE Employee.DeptID = 5

AND Project.ProjectName = 'ProductX'

AND WorksOn.Hours > 10;

e. Retrieve the names of all employees in the departments which


are located in Houston.

SELECT Employee.Name

FROM Employee

JOIN Department ON Employee.DeptID = Department.DeptID

WHERE Department.Location = 'Houston';

f. List the names of all employees who have a dependent with the
same first name as themselves.

SELECT DISTINCT Employee.Name

FROM Employee
JOIN Dependent ON Employee.EID = Dependent.EID

WHERE Employee.FirstName = Dependent.DependentName;

g. For each project, calculate the total number of employees who


work for it, and the total number of hours that these employees
work for the project.

SELECT Project.ProjectName, COUNT(WorksOn.EID) AS TotalEmployees,


SUM(WorksOn.Hours) AS TotalHours

FROM WorksOn

JOIN Project ON WorksOn.ProjectID = Project.ProjectID

GROUP BY Project.ProjectName;

h. Retrieve the average salary of all female employees.

SELECT AVG(Salary) AS AverageSalary

FROM Employee

WHERE Gender = 'Female';

i. For each department whose average employee salary is more than


$30,000, retrieve the department name and the number of
employees who work for that department.

SELECT Department.DeptName, COUNT(Employee.EID) AS NumEmployees

FROM Employee

JOIN Department ON Employee.DeptID = Department.DeptID

GROUP BY Department.DeptName

HAVING AVG(Employee.Salary) > 30000;


*IMPLEMENTATION OF THOSE ABOVE SQL STATEMENTS ONTO THIS
DATABASE:

. Retrieve the name, birth date, and address of the employee(s)


whose name is "John B. Smith."

Using the Employee table:

 Result:
Name: John B. Smith
Birth Date: 1965-01-09
Address: 731 Fondren, Houston, TX

2. Retrieve the names of all employees in the “Administration”


department.

From the Employee and Department tables:

 The Dno for "Administration" is 4.

 Employees in Department 4:
o Alicia J. Zelaya

o Jennifer S. Wallace

3. Retrieve the names of all employees in department 5 who work


more than 10 hours per week on the ProductX project.

From the Works_On, Employee, and Project tables:

 ProductX is Pnumber 1, and Dno 5 corresponds to Department 5.

 Employees who work more than 10 hours:

o John B. Smith (32.5 hours)

Result: John B. Smith

5. Retrieve the names of all employees in the departments which


are located in Houston.

From the Dept_Locations and Employee tables:

 Departments in Houston: 1 (Headquarters) and 5 (Research).

 Employees:

o John B. Smith

o Franklin T. Wong

o Joyce A. English

o Ramesh K. Narayan

o James E. Borg

6. List the names of all employees who have a dependent with the
same first name as themselves.

From the Employee and Dependent tables:

 Employees:

o John B. Smith (Dependent: Alice)


o Franklin T. Wong (Dependent: Alice)

7. For each project, calculate the total number of employees who work for it
and the total number of hours that these employees work for the project.

From the Works_On table:

Project Total Employees Total Hours


ProductX 2 52.5
ProductY 2 55.0
ProductZ 2 70.0
Computerization 3 75.0
Reorganization 2 30.0

8. Retrieve the average salary of all female employees.

From the Employee table:

 Female employees:

o Alicia J. Zelaya: 25000

o Jennifer S. Wallace: 43000

o Joyce A. English: 25000

Average Salary: (25000 + 43000 + 25000) / 3 = 31000

9. For each department whose average employee salary is


more than $30,000, retrieve the department name and the
number of employees who work for that department.

From the Employee table:

 Calculate average salaries per department:

Research (5): (30000 + 40000 + 38000 + 25000) / 4 = 33250

Administration (4): (25000 + 43000 + 25000) / 3 = 31000

Headquarters (1): 55000 / 1 = 55000

Departments meeting the condition:

Research (5): 4 employees

Headquarters (1): 1 employee

You might also like