You are on page 1of 6

CSE2031 – Principle of Database Management Systems Lab

Fall Semester 2021 – 2022


Lab Assignment – 3
NAME: BVS PRIYANKA FACULTY: Dr .NATARAJAN P
REG.NO:20BDS0237 SLOT: L55+L56

Exercise: V
Sub Query and View
Aim: to understand the concept of Sub queries and logical tables in oracle
1. Find the employee who is getting highest salary in the department head
quarters
SQL> SELECT FIRST_NAME,MID_NAME,LAST_NAME FROM EMPLOYEES
WHERE SALARY=(SELECT MAX(SALARY) FROM EMPLOYEE
WHERE DEPARTMENT_NUMBER=1);
OUTPUT:

2. Find the employees who earn the same salary as the minimum salary for
each Department.
SQL> SELECT FIRST_NAME,MID_NAME,LAST_NAME FROM EMPLOYEES WHERE
SALARY IN (SELECT MIN(SALARY) FROM EMPLOYEES GROUP BY
DEPARTMENT_NUMBER);
OUTPUT:

This study source was downloaded by 100000808246774 from CourseHero.com on 01-19-2022 02:20:08 GMT -06:00

https://www.coursehero.com/file/118742878/PDBMS-lab-da-3pdf/
3. Find the employee whose salary is greater than average salary of
department 2
SQL> SELECT FIRST_NAME,MID_NAME,LAST_NAME FROM EMPLOYEES WHERE
SALARY > (SELECT AVG(SALARY) FROM EMPLOYEES WHERE
DEPARTMENT_NUMBER=2);
OUTPUT:

4. Find out the department having highest employee strength


SQL> SELECT DEPARTMENT_NAME FROM DEPARTMENTS WHERE
DEPARTMENT_NUMBER=(SELECT DEPARTMENT_NUMBER FROM(SELECT
DEPARTMENT_NUMBER,COUNT(DEPARTMENT_NUMBER) FROM EMPLOYEES
GROUP BY DEPARTMENT_NUMBER ORDER BY COUNT(DEPARTMENT_NUMBER)
DESC) WHERE ROWNUM = 1);

This study source was downloaded by 100000808246774 from CourseHero.com on 01-19-2022 02:20:08 GMT -06:00

https://www.coursehero.com/file/118742878/PDBMS-lab-da-3pdf/
OUTPUT:

5. List out all the departments and average salary drawn by their employees
SQL> CREATE OR REPLACE VIEW AVG_SAL AS SELECT AVG(SALARY) AS
AVERAGE,DEPARTMENT_NUMBER FROM EMPLOYEE GROUP BY
DEPARTMENT_NUMBER;

View created.

SQL> SELECT * FROM AVG_SAL;


OUTPUT:

SQL> SELECT DEPARTMENT_NAME,AVERAGE FROM DEPARTMENTS JOIN


AVG_SAL ON
AVG_SAL.DEPARTMENT_NUMBER=DEPARTMENTS.DEPARTMENT_NUMBER;
OUTPUT:

This study source was downloaded by 100000808246774 from CourseHero.com on 01-19-2022 02:20:08 GMT -06:00

https://www.coursehero.com/file/118742878/PDBMS-lab-da-3pdf/
6. Create a view to display the employee details who is working in
Administration Department.
SQL> CREATE OR REPLACE VIEW ADMIN_DEPT AS
SELECT FIRST_NAME,MID_NAME,LAST_NAME,SSN_NUMBER,BIRTHDAY,SEX
FROM EMPLOYEES WHERE DEPARTMENT_NUMBER = (SELECT
DEPARTMENT_NUMBER FROM DEPARTMENTS WHERE DEPARTMENT_NAME =
'ADMINISTRATION');
View created.
SQL> SELECT * FROM ADMIN_DEPT;
OUTPUT:

7. Create a logical table to store employee details who is getting salary more
than 10000.
SQL> CREATE OR REPLACE VIEW EMP_DETAILS AS SELECT
FIRST_NAME,MID_NAME,LAST_NAME,SSN_NUMBER,SEX,DEPARTMENT_NUM
BER,SALARY FROM EMPLOYEE WHERE SALARY>10000;

View created.

This study source was downloaded by 100000808246774 from CourseHero.com on 01-19-2022 02:20:08 GMT -06:00

https://www.coursehero.com/file/118742878/PDBMS-lab-da-3pdf/
SQL> SELECT * FROM EMP_DETAILS;

OUTPUT:

Exercise: VI
Joins
Aim: To understand how to relate and access data from multiple tables.
Consider the schema given in exercise 2, and execute the following queries
1. Find the names of all the employees who are directly supervised by
‘Joyce’.
SQL> SELECT FIRST_NAME FROM EMPLOYEES WHERE DEPARTMENT_NUMBER
IN (SELECT DEPARTMENT_NUMBER FROM EMPLOYEES WHERE
FIRST_NAME='JOYCE');
OUTPUT:

This study source was downloaded by 100000808246774 from CourseHero.com on 01-19-2022 02:20:08 GMT -06:00

https://www.coursehero.com/file/118742878/PDBMS-lab-da-3pdf/
2. Find the names of all the employees who are working in department
‘Headquarter’ .
SQL> SELECT FIRST_NAME,LAST_NAME FROM EMPLOYEES WHERE
DEPARTMENT_NUMBER IN (SELECT DEPARTMENT_NUMBER FROM
EMPLOYEES WHERE DEPARTMENT_NUMBER=1);
OUTPUT:

3. List the department names and if has a manager then display the manager
name too.
SQL> SELECT DEPARTMENT_NAME FROM DEPARTMENTS;
OUTPUT:

4. Retrieve the names of the departments which have more than 2 employees
SQL> SELECT DEPARTMENT_NAME FROM DEPARTMENTS WHERE
DEPARTMENT_NUMBER IN (SELECT DEPARTMENT_NUMBER FROM EMPLOYEES
GROUP BY DEPARTMENT_NUMBER HAVING COUNT(*) >2);
OUTPUT:

This study source was downloaded by 100000808246774 from CourseHero.com on 01-19-2022 02:20:08 GMT -06:00

https://www.coursehero.com/file/118742878/PDBMS-lab-da-3pdf/
Powered by TCPDF (www.tcpdf.org)

You might also like