Employee Database An enterprise wishes to maintain a database to automate its operations.

Enterprise divided into to certain departments and each department consists of employees. The following two tables describes the automation schemas. DEPT1 (DEPTNO, DNAME, LOC) EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) Creating DEPT table SQL>Create table DEPT1( DEPTNO number(3)primary key, DNAME varchar(30)not null, LOC varchar(30)); Inserting the values into the DEPT1 table SQL> insert into dept1 values(&DEPTNO,'&DNAME','&LOC'); Enter value for deptno: 10 Enter value for dname: ACCOUNTING Enter value for loc: NEW YORK old 1: insert into dept1 values(&DEPTNO,'&DNAME','&LOC') new 1: insert into dept1 values(10,'ACCOUNTING','NEW YORK') 1 row created. SQL> / Enter value for deptno: 20 Enter value for dname: RESEARCH Enter value for loc: DALLAS old 1: insert into dept1 values(&DEPTNO,'&DNAME','&LOC') new 1: insert into dept1 values(20,'RESEARCH','DALLAS') 1 row created. SQL> / Enter value for deptno: 30 Enter value for dname: SALES Enter value for loc: CHICAGO old 1: insert into dept1 values(&DEPTNO,'&DNAME','&LOC') new 1: insert into dept1 values(30,'SALES','CHICAGO') 1 row created. SQL> / Enter value for deptno: 40 Enter value for dname: OPERATIONS Enter value for loc: BOSTON old 1: insert into dept1 values(&DEPTNO,'&DNAME','&LOC') new 1: insert into dept1 values(40,'OPERATIONS','BOSTON') 1 row created. Cresting EMP1 Table SQL> create table EMP1( 2 EMPNO number(4) primary key, 3 ENAME varchar(30) not null, 4 JOB varchar(30) , 5 MGR number(4), 6 HIREDATE date,

20) 1 row created. Enter value for empno: 7369 Enter value for ename: SMITH Enter value for job: CLERK Enter value for mgr: 7902 Enter value for hiredate: 17-DEC-80 Enter value for sal: 800 Enter value for comm: NULL Enter value for deptno: 20 old 1: insert into emp1 values(&EMPNO.'&HIREDATE'. SQL> / Enter value for empno: 7521 Enter value for ename: WARD Enter value for job: SALESMAN Enter value for mgr: 7698 Enter value for hiredate: 22-FEB-81 Enter value for sal: 1250 Enter value for comm: 500 Enter value for deptno: 30 old 1: insert into emp1 values(&EMPNO.&COMM. Inserting the values into EMP1 Table SQL> insert into emp1 values(&EMPNO.1250.800.&SAL.&MGR.'&ENAME'.&COMM.'SALESMAN'.&SAL.7 SAL number(7.&MGR.&DEPTNO) new 1: insert into emp1 values(7369.500.'22-FEB-81'.'&JOB'. 8 COMM number(5.'&HIREDATE'.7902.'ALLEN'.&SAL.&DEPTNO).'SALESMAN'. Table created.'CLERK'.&MGR.'&JOB'.'&ENAME'.NULL.'&HIREDATE'. SQL> / Enter value for empno: 7566 Enter value for ename: JONES Enter value for job: MANAGER Enter value for mgr: 7839 Enter value for hiredate: 02-APR-81 Enter value for sal: 2975 .'&JOB'. 9 DEPTNO number(3) references DEPT1(DEPTNO)).'&ENAME'.'SMITH'.300.'WARD'. SQL> / Enter value for empno: 7499 Enter value for ename: ALLEN Enter value for job: SALESMAN Enter value for mgr: 7698 Enter value for hiredate: 20-FEB-81 Enter value for sal: 1600 Enter value for comm: 300 Enter value for deptno: 30 old 1: insert into emp1 values(&EMPNO.'17-DEC-80'.&SAL.2).&DEPTNO) new 1: insert into emp1 values(7521.&MGR.'&JOB'.&COMM.7698.7698.&COMM.&DEPTNO) new 1: insert into emp1 values(7499.30) 1 row created.'&HIREDATE'.'&ENAME'.2).'20-FEB-81'.30) 1 row created.1600.

'02-APR81'.&MGR.NULL.'&HIREDATE'.'JONES'.-------------.--------.20) 1 row created.-----------10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON Retrieve all the records form EMP1 table SQL> select * from emp.'MANAGER'.--------CLERK 7902 17-DEC-80 800 SALESMAN 7698 20-FEB-81 1600 300 SALESMAN 7698 22-FEB-81 1250 500 MANAGER 7839 02-APR-81 2975 SALESMAN 7698 28-SEP-81 1250 1400 MANAGER 7839 01-MAY-81 2850 MANAGER 7839 09-JUN-81 2450 ANALYST 7566 19-APR-87 3000 PRESIDENT 17-NOV-81 5000 SALESMAN 7698 08-SEP-81 1500 0 CLERK 7788 23-MAY-87 1100 CLERK 7698 03-DEC-81 950 ANALYST 7566 03-DEC-81 3000 CLERK 7782 23-JAN-82 1300 DEPTNO 20 30 30 20 30 30 10 20 10 30 20 30 20 10 14 rows selected.&SAL.--------.2975. Note : Press forward slash (/) and insert the following rows into the EMP1 table 7654 MARTIN SALESMAN 7698 BLAKE MANAGER 7782 CLARK MANAGER 7788 SCOTT ANALYST 7839 KING PRESIDENT 7844 TURNER SALESMAN 7876 ADAMS CLERK 7900 JAMES CLERK 7902 FORD ANALYST 7934 MILLER CLERK 7698 28-SEP-81 1250 7839 01-MAY-81 2850 7839 09-JUN-81 2450 7566 19-APR-87 3000 17-NOV-81 5000 7698 08-SEP-81 1500 7788 23-MAY-87 1100 7698 03-DEC-81 950 7566 03-DEC-81 3000 7782 23-JAN-82 1300 1400 30 30 10 20 10 30 20 30 20 10 0 Retrieve all the records form DEPT1 table SQL> select * from DEPT!.&COMM.'&ENAME'.---------7369 SMITH 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE 7782 CLARK 7788 SCOTT 7839 KING 7844 TURNER 7876 ADAMS 7900 JAMES 7902 FORD 7934 MILLER JOB MGR HIREDATE SAL COMM --------.&DEPTNO) new 1: insert into emp1 values(7566 . .--------.'&JOB'. EMPNO ENAME --------.Enter value for comm: NULL Enter value for deptno: 20 old 1: insert into emp1 values(&EMPNO.7839. DEPTNO DNAME LOC --------.--------.

ENAME) FROM EMP E1. whose experience is greater than 10 years.--------7566 7566 02-APR-81 7698 7698 01-MAY-81 7782 7782 09-JUN-81 7844 7844 08-SEP-81 7900 7900 03-DEC-81 7902 7902 03-DEC-81 Q4) Display the manager who is having maximum number of employees working under him? Q5. SQL> SELECT ENAME. .'DD')<15.EMPNO.EMP E2 WHERE E1. SAL.HIREDATE FROM EMP WHERE TO_CHAR(HIREDATE.EMPNO AND E2. Find out the details of top 3 earner of company.SAL) 3* ORDER BY SAL DESC ENAME ---------KING SCOTT FORD SAL DEPTNO -------------. EMPNO EMPNO HIREDATE --------.---------5000 10 3000 20 3000 20 Q2 . Print a list of employees displaying ‘less salary’ if less than 1500 if exactly 1500 display as ‘Exact salary’ and if greater than 1500 display ‘more salary’? 6.--------. Display those managers name whose salary is more than average salary of his employees.Display those employees who joined the company before 15th of the Month? SQL> SELECT EMPNO.SAL> (SELECT AVG(SAL) FROM EMP). Update the employee salary by 15%.Q1. ENAME ---------BLAKE CLARK FORD JONES KING SCOTT Q3. 1.MGR = E2. SAL<=Y. DEPTNO FROM EMP X WHERE 2>= 2 (SELECT COUNT (DISTINCT SAL) FROM EMP Y WHERE X. SQL> SELECT DISTINCT(E2.

SQL> SELECT * FROM EMP WHERE SAL IN(SELECT MIN(SAL) FROM EMP GROUP BY HIREDATE HAVING COUNT(HIREDATE)>1) EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO --------.EMPNO 5 AND 6* E1.7.---------ALLEN BLAKE WARD BLAKE MARTIN BLAKE BLAKE KING TURNER BLAKE JAMES BLAKE 6 rows selected.--------7900 JAMES CLERK 7698 03-DEC-81 1187.--------. SQL> DELETE FROM EMP1 WHERE (MONTHS_BETWEEN(SYSDATE. COUNT(*) ---------4 10. SQL> View created.5 30 9. which contain employee names and their manager names working in sales department. Determine the count of employees.EMPNO AND E1. Determine the names of employee.--------.MGR=E2. 11. 1 row deleted.ENAME "MANAGER NAME" 3 FROM EMP1 E1. EMP1 E2 4 WHERE E1.--------.--------. who are taking commission. SQL> SELECT COUNT(*) FROM EMP WHERE COMM IS NOT NULL.---------. who joined on the same date. ENAME . EMPLOYEE MANAGER NA ---------. Create a view.HIREDATE))/12>30.MGR=E2.ENAME FROM EMP1 E1.SAL. Delete the employees. who completed 30 years of service. who earn more than their managers. Retrieve the values from view SQL> SELECT * FROM EMPDEPTMGR_SALESVIEW. SQL> CREATE VIEW EMPDEPTMGR_SALESVIEW 2 AS SELECT E1.E2.--------. SQL> SELECT E1.SAL>E2. Determine the minimum salary of an employee and his details.EMP1 E2 WHERE E1.DEPTNO =(SELECT DEPTNO FROM DEPT1 WHERE DNAME ='SALES').ENAME " EMPLOYEE NAME". 8.

Determine the employees. who take highest salary in their departments. Determine the employees. Determine the names of employees. SQL> SELECT * FROM EMP WHERE SAL+COMM IN(SELECT MIN(SAL) FROM EMP 2 GROUP BY DEPTNO). Determine the department which does not contain any employees. whose total salary is like the minimum Salary of any department. DEPTNO FROM EMP WHERE SAL IN (SELECT MAX(SAL) FROM EMP GROUP BY DEPTNO) ENAME DEPTNO ---------.--------BLAKE 30 SCOTT 20 FORD 20 KING 10 13.---------SCOTT FORD 12. SQL> SELECT DEPTNO FROM DEPT1 WHERE DEPTNO NOT IN (SELECT DEPTNO FROM EMP GROUP BY DEPTNO) DEPTNO --------40 . who are located at the same place. SQL> SELECT ENAME. 14. no rows selected 15.

ENAME " EMPLOYEE NAME". SQL> View created. DEPTNO FROM EMP WHERE SAL IN (SELECT MAX(SAL) FROM EMP GROUP BY DEPTNO) ENAME DEPTNO .ENAME FROM EMP1 E1. DETERMINE THE NAMES OF EMPLOYEE. CREATE A VIEW. WHO TAKE HIGHEST SALARY IN THEIR DEPARTMENTS. EMPLOYEE MANAGER NA ---------.ENAME "MANAGER NAME" 3 FROM EMP1 E1. 2. WHO EARN MORE THAN THEIR MANAGERS.EMPNO 5 AND 6* E1.SAL. SQL> CREATE VIEW EMPDEPTMGR_SALESVIEW 2 AS SELECT E1.MGR=E2. SQL> SELECT E1. WHICH CONTAIN EMPLOYEE NAMES AND THEIR MANAGER NAMES WORKING IN SALES DEPARTMENT.DEPTNO =(SELECT DEPTNO FROM DEPT1 WHERE DNAME ='SALES').EMPNO AND E1. DETERMINE THE NAMES OF EMPLOYEES.Q1. EMP1 E2 4 WHERE E1.E2. ENAME ---------SCOTT FORD 3.EMP1 E2 WHERE E1.---------ALLEN BLAKE WARD BLAKE MARTIN BLAKE BLAKE KING TURNER BLAKE JAMES BLAKE 6 rows selected. Retrieve the values from view SQL> SELECT * FROM EMPDEPTMGR_SALESVIEW.MGR=E2.SAL>E2. SQL> SELECT ENAME.

DETERMINE THE DEPARTMENT DOES NOT CONTAIN ANY EMPLOYEES. SQL> DELETE FROM EMP1 WHERE (MONTHS_BETWEEN(SYSDATE. 6. no rows selected 5.--------7900 JAMES CLERK 7698 03-DEC-81 1187.5 30 8.HIREDATE))/12>10 14 rows updated. DELETE THE EMPLOYEES. SQL> UPDATE EMP1 SET SAL=SAL+(SAL*25)/100 WHERE MONNTHS_BETWEEN(SYSDATE. DETERMINE THE COUNT OF EMPLOYEES.--------.HIREDATE))/12>32. WHOSE TOTAL SALARY IS LIKE THE MINIMUM SALARY OF ANY DEPARTMENT. SQL> SELECT * FROM EMP WHERE SAL IN(SELECT MIN(SAL) FROM EMP GROUP BY HIREDATE HAVING COUNT(HIREDATE)>1) EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO --------. WHOSE EXPERIENCE IS GREATER THAN 10 YEARS. COUNT(*) --------4 10 SQL> 9.---------.---------.--------. SQL> SELECT DEPTNO FROM DEPT1 WHERE DEPTNO NOT IN (SELECT DEPTNO FROM EMP GROUP BY DEPTNO) DEPTNO . WHO COMPLETED 32 YEARS OF SERVICE. 0 rows deleted. 7.--------.--------BLAKE 30 SCOTT 20 FORD 20 KING 10 4.--------. WHO ARE TAKING COMMISSION AND NOT TAKING COMMISSION.--------. SQL> SELECT COUNT(*) FROM EMP WHERE COMM IS NULL 2 UNION SELECT COUNT(*) FROM EMP WHERE COMM IS NOT NULL. WHO JOIN ON THE SAME DATE. SQL> SELECT * FROM EMP WHERE SAL+COMM 2 =ANY (SELECT MIN(SAL) FROM EMP GROUP BY DEPTNO). UPDATE THE EMPLOYEE SALARY BY 25%. DETERMINE THE EMPLOYEES. DETERMINE THE MINIMUM SALARY OF AN EMPLOYEE AND HIS DETAILS.

EMP E2 WHERE E1.75 20 BLAKE 3562.'DD')<15. DISPLAY THOSE MANAGERS NAME WHOSE SALARY IS MORE THAN AVERAGE SALARY OF HIS EMPLOYEES. FIND OUT THE DETAILS OF TOP 5 EARNER OF COMPANY.--------. EMPNO EMPNO HIREDATE --------.--------7566 7566 02-APR-81 7698 7698 01-MAY-81 7782 7782 09-JUN-81 7844 7844 08-SEP-81 7900 7900 03-DEC-81 7902 7902 03-DEC-81 6 rows selected.EMPNO.HIREDATE FROM EMP WHERE TO_CHAR(HIREDATE.--------.5 30 SQL> 11.SAL) ORDER BY SAL DESC ENAME SAL DEPTNO ---------.EMPNO AND E2.SAL> (SELECT AVG(SAL) FROM EMP). EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO --------.--------.ENAME) FROM EMP E1.---------. SQL> SELECT ENAME. DEPTNO FROM EMP1 X WHERE 4>= (SELECT COUNT(DISTINCT SAL) FROM EMP1 Y WHERE X. SQL> SELECT DISTINCT(E2.--------.'YY').--------. DISPLAY THOSE EMPLOYEES WHO JOINED THE COMPANY BEFORE 15TH OF THE MONTH? SQL> SELECT EMPNO. ENAME ---------BLAKE CLARK FORD JONES KING SCOTT 12.--------.--------. SAL. 13.MGR = E2.--------KING 6250 10 SCOTT 3750 20 FORD 3750 20 JONES 3718.--------7369 SMITH CLERK 7902 17-DEC-80 800 20 . DISPLAY THOSE EMPLOYEES WHOSE 10% OF SALARY IS EQUAL TO THE YEAR OF JOINING? SQL> SELECT * FROM EMP WHERE (SAL*10)/100 = TO_CHAR(HIREDATE.--------40 10. SAL<=Y.

SQL> SELECT TO_CHAR(HIREDATE. IN WHICH YEAR DID MOST PEOPLE JOIN THE COMPANY? DISPLAY THE YEAR AND NUMBER OF EMPLOYEES. ENAME ENAME ---------.SQL> 14.COUNT(EMPNO) "NO OF EMPLOYES" FROM EMP GROUP BY HIREDATE HAVING COUNT(*) IN (SELECT MAX(COUNT(EMPNO)) FROM EMP GROUP BY HIREDATE) YE NO OF EMPLOYES -.MGR IN (SELECT E1.E2.---------SCOTT FORD ALLEN WARD JAMES TURNER MARTIN MILLER ADAMS JONES CLARK JONES JONES BLAKE BLAKE BLAKE BLAKE BLAKE CLARK SCOTT KING KING .-------------81 2 SQL> SELECT E1.ENAME FROM EMP E1.MGR FROM EMP E1 GROUP BY E1.ENAME.MGR=E2.MGR).'YY') "YEAR" .EMPNO AND 2 E1.EMP E2 WHERE E1.

BLAKE SMITH KING FORD 13 rows selected. .

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.