Professional Documents
Culture Documents
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:
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.
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)