You are on page 1of 5

EMPLOYEE and DEPARTMENT DATABASE TABLES And Related Queries

Department Table Creation:


create table dept (deptno number(3) primary key, dname varchar2(20), loc varchar2(20));

Records Insertion:
insert into dept values(10,'Accounting','Newyork');
insert into dept values(20,'Research','Dallas');
insert into dept values(30,'Sales','Chicago');
insert into dept values(40,'Operations','Boston');

SQL> select * from dept;

DEPTNO DNAME LOC


10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON

4 rows selected.

Employee Table Creation:


create table emp (empno number(4) primary key, ename varchar2(20), job varchar2(20), mgr references
emp, hiredate date,sal number(5), comm number(6,2),deptno references dept);

Records Insertion:

Inserting President Record

insert into emp values(7839,'king','president',null,'17-nov-81',5000,null,10);

Inserting Manager Records

insert into emp values(7566,'Jones','Manager',7839,'02-apr-81',2975,null,20);


insert into emp values(7698,'Blake','Manager',7839,'01-may-81',2850,null,30);
insert into emp values(7782,'Clark','Manager',7839,'09-jun-81',2450,null,10);

Inserting The Normal Employee Details

insert into emp values(7902,'Ford','Analyst',7566,'3-dec-81',3000,null,20);


insert into emp values(7369,'Smith','Clerk',7902,'17-dec-80',800,null,20);
insert into emp values(7499,'Allen','Salesman',7698,'20-feb-81',1600,300,30);
insert into emp values(7521,'Ward','Salesman',7698,'22-feb-81',1250,500,30);
insert into emp values(7654,'Martin','Salesman',7698,'28-sep-81',1250,1400,30);
insert into emp values(7788,'Scott','Analyst',7566,'19-apr-87',3000,null,20);
insert into emp values(7844,'Turner','Salesman',7698,'8-sep-81',.1500,0,30);
insert into emp values(7876,'Adams','Clerk',7788,'23-may-87',1100,null,20);
insert into emp values(7900,'James','Clerk',7698,'3-dec-81',950,null,30);
insert into emp values(7934,'Miller','Clerk',7782,'23-jan-82',1300,null,10);

Page No : 1
Department Of Computers, Bhavan’s Vivekananda College, Sainikpuri
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 17-Dec-80 800 20
7499 ALLEN SALESMAN 7698 20-Feb-81 1600 300 30
7521 WARD SALESMAN 7698 22-Feb-81 1250 500 30
7566 JONES MANAGER 7839 2-Apr-81 2975 20
7654 MARTIN SALESMAN 7698 28-Sep-81 1250 1400 30
7698 BLAKE MANAGER 7839 1-May-81 2850 30
7782 CLARK MANAGER 7839 9-Jun-81 2450 10
7788 SCOTT ANALYST 7566 19-Apr-87 3000 20
7839 KING PRESIDENT 17-Nov-81 5000 10
7844 TURNER SALESMAN 7698 8-Sep-81 1500 0 30
7876 ADAMS CLERK 7788 23-May-87 1100 20
7900 JAMES CLERK 7698 3-Dec-81 950 30
7902 FORD ANALYST 7566 3-Dec-81 3000 20
7934 MILLER CLERK 7782 23-Jan-82 1300 10

14 rows selected.

1. Find out the details of top 3 earner of company.

SQL> select rownum as Rank,empno,ename,sal from


(select empno,ename,sal from emp order by sal desc) where rownum<=3 ;

RANK EMPNO ENAME SAL


----------- --------- --------- ----------
1 7839 KING 5000
2 7788 SCOTT 3000
3 7902 FORD 3000

2. Display those employees who joined the company before 15th of the month?

SQL> select empno,ename,hiredate from emp where to_number(to_char(hiredate,'dd'))<15;

EMPNO ENAME HIREDATE


------------- ------------ ----------------
7566 JONES 02-APR-81
7698 BLAKE 01-MAY-81
7782 CLARK 09-JUN-81
7788 SCOTT 09-DEC-82
7844 TURNER 08-SEP-81
7876 ADAMS 12-JAN-83
7900 JAMES 03-DEC-81
7902 FORD 03-DEC-81

Page No : 2
Department Of Computers, Bhavan’s Vivekananda College, Sainikpuri
3. 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’?

SQL> select ename,sal||'Less Salary' from emp where sal<1500


union
select ename,sal||'More Salary' from emp where sal>1500
union
select ename,sal||'Exact Salary'from emp where sal=1500;

ENAME SAL|| 'LESSSALARY'


---------- -----------------------------------------------------
ADAMS 1100 Less Salary
ALLEN 1600 More Salary
BLAKE 2850 More Salary
CLARK 2450 More Salary
FORD 3000 More Salary
JAMES 950 Less Salary
JONES 2975 More Salary
KING 5000 More Salary
MARTIN 1250 Less Salary
MILLER 1300 Less Salary
SCOTT 3000 More Salary
SMITH 960 Less Salary
TURNER 1500 Exact Salary
WARD 1250 Less Salary

14 rows selected.

4. Update the employee salary by 15%, whose experience is greater than 10 years.

SQL>update emp set sal=sal+(sal*0.15) where trunc(months_between(sysdate,hiredate)/12)>10;

14 rows updated.

SQL> select * from emp;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


7369 SMITH CLERK 7902 17-Dec-80 1104 20
7499 ALLEN SALESMAN 7698 20-Feb-81 1840 300 30
7521 WARD SALESMAN 7698 22-Feb-81 1437.5 500 30
7566 JONES MANAGER 7839 2-Apr-81 3421.25 20
7654 MARTIN SALESMAN 7698 28-Sep-81 1437.5 1400 30
7698 BLAKE MANAGER 7839 1-May-81 3277.5 30
7782 CLARK MANAGER 7839 9-Jun-81 2817.5 10
7788 SCOTT ANALYST 7566 19-Apr-87 3450 20
7839 KING PRESIDENT 17-Nov-81 5750 10
7844 TURNER SALESMAN 7698 8-Sep-81 1725 0 30
7876 ADAMS CLERK 7788 23-May-87 1265 20

Page No : 3
Department Of Computers, Bhavan’s Vivekananda College, Sainikpuri
7900 JAMES CLERK 7698 3-Dec-81 1092.5 30
7902 FORD ANALYST 7566 3-Dec-81 3450 20
7934 MILLER CLERK 7782 23-Jan-82 1495 10
14 rows selected.

5. Delete the employees, who completed 30 years of service.

SQL> delete from emp where


trunc(months_between(sysdate,hiredate)/12)>=30;
12 rows deleted.

SQL> select * from emp;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


7788 SCOTT ANALYST 7566 09-DEC-82 3000 20
7876 ADAMS CLERK 7788 12-JAN-83 1100 20

6. Determine the minimum salary of an employee and his details, who joined on the same date.

SQL> select ename,deptno,sal from emp where sal in (select min(sal) from emp group by hiredate having
count(hiredate)>1);

ENAME DEPTNO SAL


---------- -------------- ----------
JAMES 30 950

7. Determine the count of employees, who are taking commission.

SQL> select count(empno) from emp where comm is not null;

COUNT(EMPNO)
------------
4

8. Create a view to display employee details of sales department.

SQL> create view emp_sales as select * from emp where deptno=(select deptno from dept where
dname='SALES');

SQL> select empno,ename,deptno,sal from emp_sales;

EMPNO ENAME DEPTNO SAL


---------- ---------- ---------- ----------
7499 ALLEN 30 1600
7521 WARD 30 1250
7654 MARTIN 30 1250
7698 BLAKE 30 2850
7844 TURNER 30 1500
7900 JAMES 30 950
6 rows selected.
Page No : 4
Department Of Computers, Bhavan’s Vivekananda College, Sainikpuri
9. Determine the employees, who are located at the same place.

SQL> select ename,dept.deptno,loc from dept,emp


where emp.deptno=dept.deptno
order by loc;
ENAME DEPTNO LOC
---------- ---------- -------------
BLAKE 30 CHICAGO
TURNER 30 CHICAGO
ALLEN 30 CHICAGO
MARTIN 30 CHICAGO
WARD 30 CHICAGO
JAMES 30 CHICAGO
SCOTT 20 DALLAS
JONES 20 DALLAS
SMITH 20 DALLAS
ADAMS 20 DALLAS
FORD 20 DALLAS
KING 10 NEW YORK
MILLER 10 NEW YORK
CLARK 10 NEW YORK

14 rows selected.

10. Determine the department which does not contain any employees.

SQL> select deptno from dept


minus
select deptno from emp;

DEPTNO
----------
40

Page No : 5
Department Of Computers, Bhavan’s Vivekananda College, Sainikpuri

You might also like