1. SQL> desc employee
Name Null? Type
----------------------------------------- ------------- ----------------------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB NUMBER(3)
MGR NUMBER(3)
HIREDATE DATE
SAL NUMBER(8,2)
COMM NUMBER(8,2)
DEPTNO NOT NULL NUMBER(2)
SQL> desc dept
Name Null? Type
----------------------------------------- ------------- ----------------------------
DEPTNO NOT NULL NUMBER(2)
NAME VARCHAR2(10)
LOCATION NOT NULL NUMBER(3)
SQL> desc location
Name Null? Type
----------------------------------------- ------------- ----------------------------
CODE NOT NULL NUMBER(3)
NAME NOT NULL VARCHAR2(10)
SQL> desc job
Name Null? Type
----------------------------------------- ------------- ----------------------------
CODE NOT NULL NUMBER(3)
NAME NOT NULL VARCHAR2(10)
SQL> select * from dept;
DEPTNO AME LOCATION
---------- ----------------- ----------
10 sales 1
20 research 3
30 accounting 4
40 admin 2
3. Exercise-1
Names of employees with a job of trainee
SQL> select ename from employee where job=(select code from job where name='trainee');
ENAME
----------
DENNIS
VISHNU
KARTHIK
KALYANI
ANUSHA
SIRISHA
VENKAT
RATNAM
8 rows selected.
List of distinct department ids
SQL> select distinct deptno from employee;
DEPTNO
----------
10
20
30
40
Names of employees sorted by their joining date
SQL> select ename from employee order by hiredate;
ENAME
----------
SRINIVAS
KEERTHI
KARTHIK
PRADEEP
KUMAR
VISHNU
ANUSHA
SIRISHA
VENKAT
PAVAN
KALYANI
DENNIS
RATNAM
13 rows selected.
4. Names of employees who is not a sales trainee
SQL> select ename from employee where deptno not in(select deptno from dept where
name='sales') and job=(select code from job where name='trainee');
ENAME
----------
DENNIS
KARTHIK
KALYANI
SIRISHA
VENKAT
RATNAM
6 rows selected.
Names of employees who did not get any commission
SQL> select ename from employee where comm is null;
ENAME
----------
PAVAN
KUMAR
PRADEEP
SRINIVAS
KEERTHI
Names of employees who do not have a manager
SQL> select ename from employee where mgr is null;
ENAME
----------
PAVAN
KUMAR
PRADEEP
SRINIVAS
KEERTHI
Names of employees in department id 20
SQL> select ename from employee where deptno=20;
ENAME
----------
KUMAR
KEERTHI
KARTHIK
SIRISHA
5. Employees with a salary of over 20000
SQL> select * from employee where sal>20000;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----------
101 PAVAN 2 01-JAN-10 99000 10
103 PRADEEP 3 04-APR-09 40000 30
104 SRINIVAS 3 18-MAR-08 50000 40
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----------
105 KEERTHI 4 19-JUN-08 60000 20
113 RATNAM 1 104 10-NOV-10 22000 340 40
Employees with a salary of over 40000, but less than 80000
SQL> select * from employee where sal between 40000 and 80000;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----------
103 PRADEEP 3 04-APR-09 40000 30
104 SRINIVAS 3 18-MAR-08 50000 40
105 KEERTHI 4 19-JUN-08 60000 20
Employees working under either Pradeep or Srinivas
SQL> select * from employee where mgr in(select empno from employee where
ename='PRADEEP' or ename='SRINIVAS');
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----------
106 DENNIS 1 104 20-OCT-10 15000 50 40
107 VISHNU 1 104 19-JUN-09 18000 900 10
108 KARTHIK 1 104 28-FEB-09 12000 150 20
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----------
109 KALYANI 1 104 30-APR-10 13000 1000 30
110 ANUSHA 1 103 07-AUG-09 19000 1500 10
111 SIRISHA 1 103 08-SEP-09 13000 1200 20
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----------
112 VENKAT 1 103 09-OCT-09 15000 230 30
113 RATNAM 1 104 10-NOV-10 22000 340 40
6. 8 rows selected.
Employees who are not working under either Pradeep or Srinivas
SQL> select * from employee where mgr not in(select empno from employee where
ename='PRADEEP' or ename='SRINIVAS') or mgr is null;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----------
101 PAVAN 2 01-JAN-10 99000 10
102 KUMAR 3 03-JUN-09 20000 20
103 PRADEEP 3 04-APR-09 40000 30
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----------
104 SRINIVAS 3 18-MAR-08 50000 40
105 KEERTHI 4 19-JUN-08 60000 20
Employees working under Pradeep and joined before him
SQL> select * from employee where mgr=(select empno from employee where
ename='PRADEEP') and hiredate<(select hiredate from employee where ename='PRADEEP');
no rows selected
Employees working under Pradeep or Srinivas and joined before them
SQL> select * from employee where (mgr=(select empno from employee where
ename='PRADEEP')and hiredate<(select hiredate from employee where ename='PRADEEP'))or
(mgr=(select empno from employee where ename='SRINIVAS')and hiredate<(select hiredate
from employee where ename='SRINIVAS'));
no rows selected
Employees with more commission than salary
SQL> select * from employee where comm>sal;
no rows selected
7. Exercise-II
Employees in ACCOUNTING department
SQL> select * from employee where deptno=(select deptno from dept where
name='accounting');
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----------
103 PRADEEP 3 04-APR-09 40000 30
109 KALYANI 1 104 30-APR-10 13000 1000 30
112 VENKAT 1 103 09-OCT-09 15000 230 30
List of all ANALYSTS in Kakinada area
SQL> select * from employee where job=(select code from job where name='analyst') and
deptno=(select deptno from dept where location=(select code from location where
name='kakinada'));
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----------
104 SRINIVAS 3 18-MAR-08 50000 40
Employees in SALES department
SQL> select * from employee where deptno=(select deptno from dept where name='sales');
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----------
101 PAVAN 2 01-JAN-10 99000 10
107 VISHNU 1 104 19-JUN-09 18000 900 10
110 ANUSHA 1 103 07-AUG-09 19000 1500 10
Find the job function of Keerthi
SQL> select name from job where code=(select job from employee where ename='KEERTHI');
NAME
----------
Clerk
All the locations having a SALES department
SQL> select name from location where code in( select location from dept where name='sales');
NAME
----------
Chicago
8. The job ids of all the employees who are managers of other employees
SQL> SELECT JOB FROM EMPLOYEE WHERE EMPNO IN(SELECT MGR FROM
EMPLOYEE);
JOB
----------
3
3
All the ANALYSTS who are also managers
SQL> SELECT * FROM EMPLOYEE WHERE EMPNO IN(SELECT MGR FROM
EMPLOYEE) AND JOB=(SELECT CODE FROM JOB
WHERE NAME='analyst');
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----------
103 PRADEEP 3 04-APR-09 40000 30
104 SRINIVAS 3 18-MAR-08 50000 40
All the employees reporting to the PRESIDENT
SQL> select * from employee where mgr=(select code from job where name='president');
no rows selected
All the ANALYSTS reporting to the PRESIDENT
SQL> select * from employee where mgr=(select code from job where name='president') and
job=(select code from job where name='analyst');
no rows selected
The job functions of all the employees who are managers of other employees
SQL> select name from job where code in(select job from employee where empno in(select mgr
from employee));
NAME
----------
analyst
Find departments(names and locations) with some employees earning less than 2000
SQL> select * from dept where deptno in(select deptno from employee where sal<2000);
no rows selected
9. Exercise-III
• Employees earning more than DENNIS’s manager
SQL> select * from employee where sal>(select sal from employee where empno=(select
mgr from employee where ename='DENNIS'));
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----------
101 PAVAN 2 01-JAN-10 99000 10
105 KEERTHI 4 19-JUN-08 60000 20
• Employees working under DENNIS and earning more than DENNIS’s manager
SQL> select * from employee where sal>(select sal from employee where empno=(select
mgr from employee where ename='DENNIS'))and mgr=(select empno from employee
where ename='DENNIS');
no rows selected
• Employees in SALES department with the same job as anyone in the RESEARCH
department
SQL> select * from employee where job in(select job from employee where
deptno=(select deptno from dept where name='sales')) and deptno=(select deptno from
dept where name='research');
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----------
108 KARTHIK 1 104 28-FEB-09 12000 150 20
111 SIRISHA 1 103 08-SEP-09 13000 1200 20
• All employees in CHICAGO area
SQL> select * from employee where deptno=(select deptno from dept where
location=(select code from location where name='chicago'));
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----------
101 PAVAN 2 01-JAN-10 99000 10
107 VISHNU 1 104 19-JUN-09 18000 900 10
110 ANUSHA 1 103 07-AUG-09 19000 1500 10
10. • Find all the employees whose join date is the earliest
SQL> select * from employee where hiredate=(select min(hiredate) from employee);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----------
104 SRINIVAS 3 18-MAR-08 50000 40
Exercise-IV
• Find all the employees whose join date is the earliest in their respective departments
SQL> select * from employee where hiredate in(select min(hiredate) from employee group by
deptno);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----------
103 PRADEEP 3 04-APR-09 40000 30
104 SRINIVAS 3 18-MAR-08 50000 40
105 KEERTHI 4 19-JUN-08 60000 20
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
107 VISHNU 1 104 19-JUN-09 18000 900 10
• Find all the employees whose salary is the maximum in their respective departments
SQL> select * from employee where sal in(select max(sal) from employee group by deptno);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
101 PAVAN 2 01-JAN-10 99000 10
103 PRADEEP 3 04-APR-09 40000 30
104 SRINIVAS 3 18-MAR-08 50000 40
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
105 KEERTHI 4 19-JUN-08 60000 20
• Find all the customers whose sales person is not originally a salesperson
Don’t have any salesperson details
11. Exercise-V
• Find all the employees whose join date is the earliest
SQL> select * from employee where hiredate=(select min(hiredate) from employee);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----------
104 SRINIVAS 3 18-MAR-08 50000 40
• Find the minimum salary in the ACCOUNTING department
SQL> select min(sal) from employee where deptno=(select deptno from dept where
name='accounting');
MIN(SAL)
----------
13000
• Find the all the employees with the minimum salary
SQL> select * from employee where sal=(select min(sal) from employee);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----------
108 KARTHIK 1 104 28-FEB-09 12000 150 20
• Find the maximum salary of all employees
SQL> select ename,sal+comm from employee;
ENAME SAL+COMM
---------- ----------
PAVAN 99000
KUMAR 20000
PRADEEP 40000
SRINIVAS 50000
KEERTHI 60000
DENNIS 15500
VISHNU 18900
KARTHIK 12150
KALYANI 14000
ANUSHA 20500
SIRISHA 14200
ENAME SAL+COMM
---------- ----------
VENKAT 15230
RATNAM 22340
12. 13 rows selected.
• Find the maximum salary of all employees from SALES department
SQL> select ename,sal+comm from employee where deptno=(select deptno from dept where
name='sales');
ENAME SAL+COMM
---------- ----------
PAVAN 99000
VISHNU 18900
ANUSHA 20500
• Find maximum salary of all ANALYSTs
SQL> select ename,sal+comm from employee where job=(select code from job where
name='analyst');
ENAME SAL+COMM
---------- ----------
KUMAR 20000
PRADEEP 40000
SRINIVAS 50000
• Find all the ANALYSTs with the maximum salary among ANALYSTs
SQL> select * from employee where sal=(select max(sal) from employee where job=(select code
from job where name='analyst'));
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----------
104 SRINIVAS 3 18-MAR-08 50000 0 40
• Find total commissions paid to all employees together
SQL> select sum(comm) from employee;
SUM(COMM)
----------
5820
• Find total salary of all CLERKs
SQL> select sum(sal) from employee where job=(select code from job where name='clerck');
SUM(SAL)
----------
60000
13. • Find the number of ANALYSTS
SQL> select sum(sal) from employee where job=(select code from job where name='clerck');
SUM(SAL)
----------
60000
• Find out number of different job_id in the RESEARCH department
SQL> select count(distinct job) from employee where deptno=(select deptno from dept where
name='research');
COUNT(DISTINCT JOB)
------------------
3
• Find employees earning more than average salary of all employees
SQL> select * from employee where sal>(select avg(sal) from employee);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----------
101 PAVAN 2 01-JAN-10 99000 0 10
103 PRADEEP 3 04-APR-09 40000 0 30
104 SRINIVAS 3 18-MAR-08 50000 0 40
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----------
105 KEERTHI 4 19-JUN-08 60000 0 20
• Maximum salary of all employees
SQL> select max(sal) from employee;
MAX(SAL)
----------
99000
• Average salary in each department_id
SQL> select avg(sal),deptno from employee group by deptno;
AVG(SAL) DEPTNO
---------- ----------
45333.3333 10
26250 20
22666.6667 30
29000 40
14. • No of employees with each job_id
SQL> select count(*),job from employee group by job;
COUNT(*) JOB
---------- ----------
8 1
1 2
3 3
1 4
• No of employees where job_id is more than 3
SQL> select count(*) from employee where job>3;
COUNT(*)
----------
1
• No of ANALYSTS in each department
SQL> select count(*) from employee where job=(select code from job where name='analyst')
group by de
ptno;
COUNT(*)
----------
1
1
1