You are on page 1of 9

Database Systems lab

Assessment -3

Name- Prianshu Goutam


Reg no-21BCE0400
Slot- L29 + L30
Faculty-Shashank Mouli Satapathy
EXERCISE -1
1) Find the employee who is getting highest salary in the department research.

Query:

SELECT * FROM Employee WHERE Salary = ( SELECT MAX(Salary) FROM Employee WHERE
Department_Number = ( SELECT Department_Number FROM Dept WHERE Department_Name =
'Research' ));

Output:

2) Find the employees who earn the same salary as the minimum salary for each Department.

Query:

SELECT E.FIRST_NAME, E.LAST_NAME, E.SALARY, D.DEPARTMENT_NAME FROM Employee E

JOIN ( SELECT D.DEPARTMENT_NUMBER, MIN(E.SALARY) AS MIN_SALARY FROM Employee E

JOIN dept D ON E.DEPARTMENT_NUMBER = D.DEPARTMENT_NUMBER

GROUP BY D.DEPARTMENT_NUMBER) M

ON E.DEPARTMENT_NUMBER = M.DEPARTMENT_NUMBER

JOIN dept D ON E.DEPARTMENT_NUMBER = D.DEPARTMENT_NUMBER

WHERE E.SALARY = M.MIN_SALARY;

Output:

3) Find the employee whose salary is greater than average salary of department 2.

Query:

SELECT *

FROM Employee
WHERE SALARY > (SELECT AVG(SALARY) FROM Employee WHERE DEPARTMENT_NUMBER = 2);

Output:

Reason:

Because there was only one employee whose department number was 2 but in assessment 2 the
department number of that employee was modified to 2 . Hence , no data found.

4) List out all the department names with their individual employees strength.

Query:

SELECT D.DEPARTMENT_NAME, COUNT(E.SSN_NUMBER) AS EMPLOYEE_COUNT

FROM dept D

LEFT JOIN EMPLOYEE E ON D.DEPARTMENT_NUMBER = E.DEPARTMENT_NUMBER

GROUP BY D.DEPARTMENT_NAME;

Output:
5) Find out the department name having highest employee strength

Query:

SELECT D.DEPARTMENT_NAME, COUNT(E.SSN_NUMBER) AS EMPLOYEE_COUNT

FROM dept D

LEFT JOIN EMPLOYEE E ON D.DEPARTMENT_NUMBER = E.DEPARTMENT_NUMBER

GROUP BY D.DEPARTMENT_NAME

HAVING COUNT(E.SSN_NUMBER) = (

SELECT MAX(COUNT(E2.SSN_NUMBER))

FROM dept D2

LEFT JOIN EMPLOYEE E2 ON D2.DEPARTMENT_NUMBER = E2.DEPARTMENT_NUMBER

GROUP BY D2.DEPARTMENT_NAME

);

Output:

6) List out all the departments and average salary drawn by their employees.

Query:

SELECT D.DEPARTMENT_NAME, AVG(E.SALARY) AS AVERAGE_SALARY FROM dept D

LEFT JOIN EMPLOYEE E ON D.DEPARTMENT_NUMBER = E.DEPARTMENT_NUMBER

GROUP BY D.DEPARTMENT_NAME;

Output:
EXERCISE 2

1) Find the names of all the employees who are directly supervised by ‘Franklin Wong’.

Query:

SELECT e.first_name, e.last_name

FROM employee e

JOIN employee s ON e.supervisor_ssn = s.ssn_number

WHERE s.last_name = 'Wong' AND s.first_name = 'Frankin'

Output:

4) Retrieve the names of all who do not work on any project.

Query:

SELECT e.first_name, e.last_name

FROM employee e

LEFT JOIN works_on w ON e.ssn_number = w.essn


WHERE w.pno IS NULL;

Output:

5) List the names of all managers who have no dependents.

Query:

SELECT e.first_name, e.last_name FROM employee e

JOIN dept d ON e.ssn_number = d.managerssn

WHERE e.ssn_number NOT IN ( SELECT DISTINCT employee FROM dependent)

Output:

6) List the employee’s names and the department names if they happen to manage a department.

Query:

SELECT e.first_name, e.last_name, d.department_name

FROM employee e
LEFT JOIN dept d ON e.ssn_number = d.managerssn

WHERE d.managerssn IS NOT NULL

Output:

7) For each project retrieve the project number, project name and the number of employees who
work on that project.

Query:

SELECT p.project_number, p.project_name, COUNT(w.essn) AS employee_count

FROM project p

LEFT JOIN works_on w ON p.project_number = w.pno

GROUP BY p.project_number, p.project_name

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

Query:

SELECT p.project_number, p.project_name, COALESCE(SUM(w.hours), 0) AS total_hours_per_week

FROM project p

LEFT JOIN works_on w ON p.project_number = w.pno

GROUP BY p.project_number, p.project_name

Output:

9) Retrieve the names of the employees who have 2 or more dependents.

Query:

SELECT e.first_name, e.last_name FROM employee e INNER JOIN dependent d ON e.ssn_number =


d.employee GROUP BY e.first_name, e.last_name HAVING COUNT(d.dependent_name) >= 2;

Output:

You might also like