0% found this document useful (0 votes)
31 views14 pages

21bce0400 Ass3

This document contains details of exercises completed as part of a database systems assessment. It includes 10 SQL queries with each query targeting a specific task such as finding the highest paid employee in the research department, listing departments and their employee counts, and retrieving names of employees with 2 or more dependents. The output of each query is blank, suggesting the queries were executed but no results were returned likely because the database is empty or contains insufficient data.

Uploaded by

Priyanshu Goutam
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
0% found this document useful (0 votes)
31 views14 pages

21bce0400 Ass3

This document contains details of exercises completed as part of a database systems assessment. It includes 10 SQL queries with each query targeting a specific task such as finding the highest paid employee in the research department, listing departments and their employee counts, and retrieving names of employees with 2 or more dependents. The output of each query is blank, suggesting the queries were executed but no results were returned likely because the database is empty or contains insufficient data.

Uploaded by

Priyanshu Goutam
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/ 14

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:
7) Find maximum average salary for each department.

Query:

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

JOIN dept D ON E.DEPARTMENT_NUMBER = D.DEPARTMENT_NUMBER GROUP BY


D.DEPARTMENT_NAME

HAVING AVG(E.SALARY) = (

SELECT MAX(AVG_SALARY)

FROM (SELECT AVG(SALARY) AS AVG_SALARY FROM EMPLOYEE

GROUP BY DEPARTMENT_NUMBER ));

Output:

8) Create a view to display the employee details who is working in IT department.

Query:

CREATE VIEW IT_Employee_Details AS

SELECT * FROM EMPLOYEE

WHERE DEPARTMENT_NUMBER = ( SELECT DEPARTMENT_NUMBER FROM dept

WHERE DEPARTMENT_NAME = 'IT');

Output:
To view employee details:

Query:

SELECT * FROM IT_Employee_Details;

Output:

Reason:

Because there is no employee who is in the IT department till now .

9) Create a logical table to store employee details who is getting salary more than 10000.

Query:

CREATE VIEW HighSalary_Employees AS

SELECT * FROM Employee

WHERE SALARY > 10000;

Output:

To view Details:
Query:

select *from HighSalary_Employees;


Output:

10) Create a table to store the employees details based on the department no.

Query:

CREATE VIEW DepartmentEmployees AS

SELECT e.ssn_number, e.First_Name, e.Last_Name, e.Department_Number

FROM Employee e

JOIN dept d ON e.Department_Number = d.Department_Number;

Output:
To display:

Query:

select * from DepartmentEmployees

Output:
EXERCISE 2
1) Retrieve the names of all employees in department 5 who work more than 10 hours per week
on ProductX project.

Query:

SELECT e.first_name, e.last_name FROM employee e

JOIN works_on w ON e.ssn_number = w.essn

JOIN project p ON w.pno = p.project_number

WHERE e.department_number = 5 AND w.hours > 10 AND p.project_name = 'ProductX';

Output:

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

Query:

SELECT e.first_name, e.last_name

FROM employee e

JOIN dependent d ON e.ssn_number = d.employee AND e.first_name = d.dependent_name

Output:

3) 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) 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.

Query:

SELECT e.first_name, e.last_name, e.address FROM employee e


JOIN works_on w ON e.ssn_number = w.essn

JOIN project p ON w.pno = p.project_number

WHERE p.project_location = 'Houston' AND e.department_number NOT IN ( SELECT d.depno FROM


dept_locations d WHERE d.d_location = 'Houston' );

Output:

6) 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:

7) 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:

8) 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:
9) 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:

10) 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy