Oracle Queries
Oracle Queries
Oracle Queries
SQL
1. To display the dept information from department table
Select empno,ename from emp where comm is not null and comm.>0;
10. To display name of employees who do not earn any commission.
SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
Select ename from emp where sysdate-hiredate>5*365;
15. To display the list of wmployees who have joined the company before 30th june 90 or
after 31st dec 90.
Show user
20. To display the names of employees workin in department number 10 or 20 or 40 or
employees working as clerks ,salesman or analyst?
Select ename from emp where length(ename)=5; (or) select ename from emp where
ename like ‘_____’;
25. To display the names of employees who are not working as managers
26. Select * from emp minus (select * from emp where empno in (select mgr from emp));
27. (or)
28. Select * from emp where empno not in(select mgr from emp where mgr is not null);
SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
29. (or)
Select * from emp e where not in(select mgr from emp where e.empno=’mgr’);
30. Display the ename of employees who are not woeking as salesman or clerk or analyst
SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
Select ename from emp order by sal;
43. To display the names of employees in descending order of salary.
SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
56. To display the various jobs along with total sal for each of the hobs where total sal is
greater then 40000.
Select ename from emp where sal<(select sal from emp where ename=’SCOTT’) and
sal>(select sal from rmp where ename=’JAMES’);
66. To display the names of the emploees who earn highest salary in their respective
departments.
Select * from emp e where sal=(select max(sal) from emp where deptno=e.deptno);
67. To display the names of employees who earn highest salaries in thir respective job
groups.
Select * from emp e where sal in (select max(sal) from emp group by job having
e.job=job);
68. To display the employee names who are working in accountging dept.
Select ename from emp where deptno in(select deptno from dept where
dname=’ACCOUNTING’);
69. To display the employee names who are working in Chicago.
SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
Select ename from emp where deptno=(select deptno from dept where
loc=’CHICAGO’);
70. To display the hob groups having total salary greater then the maximum salary for
managers.
Selct job,sum(sal) from emp group by job having sum(sal)>(select max(sal) from emp
where job=’MANAGER’);
71. To display the names of employees from department number 10 with salary greater
than that of any employee working in other departments.
Select ename,sal,deptni from emp e where deptno=10 and sal>any(select sal from emp
where e.deptni!=deptno);
72. Display the names of employees from department number 10 with salaty greater then
that of all employees working in other departments.
Select ename,sal,deptno from emp e where deptno=10 and sal>any(select sal from emp
where e.deptno!=deptno);
73. To display the names of employees in upper case.
80. (or)
SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
82. Find the first occurrence of character a from the following string ‘computer
maintenance corporation’.
Select ename||’has joined the company on ‘|| to_char (hiredate,’day ddth month year’)
from emp;
90. To display the date of nearest saturday after current day.
SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
94. Select job from emp where deptno=10 and fob in(select job from emp where
deptno=20); (or)
Select job from emp where deptno=10 intersect select job from emp where
deptno=20;
95. To display the jobs found in department numer 10 and 20 eliminate duplicate jobs.
96. Select distinct(job) from emp where deptno=10 and job in(select job from emp where
deptno=20);
(or)
Select job from emp where deptno=10 intersect job from emp where deptno=20;
97. To display the jobs which are unique to deptno 10.
98. Select job from emp where deptno=10 minus select job from emp where deptno!=10;
(or)
Select job from emp where deptno=10 and job not in (select job from emp
where deptno<>10);
99. To display the details of those who do not have any person working under them.
Select empno from emp where empno not in(select mgr from emp where mgr is not
null);
100. To display the details of employees who are in dept and grade is 3.
101. Select * from emp where sal>=(select losal from salgtade where grade=3) and
102. sal<=(select hisal from salgrade where grade=3) and deptno=(select
deptno from
dept where dname=’SALES’);
103. To display those who are not managers and who are manager any one.
Select * from emp where empno in(select mgr from emp where mgr is not null);
104. To display tose employees whose name contains not less than 4 chars.
Select * from depdt where dname like ‘s’ and loc like’o’;
106. To display thode emplouees whode manager name is JONES.
Select * from emp where mgr=(select empno from emp where ename=’JONES’);
107. Display those employees whode salary is more than 3000 after giving 20%
increment .
SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
108. Select * from emp where sal*120/100>3000;
109. (or)
Select empno,ename from emp where deptno=(select deptno from dept where
dname=’SALES’);
112. To display employee name,deptname,salary and comm. For those sal in
between 2000 and 5000 while location is Chicago.
Select empno ,ename,deptno from emp where deprno=(select deptno from dept where
loc=’CHICAGO’) and sal between 2000 and 5000;
113. To display those employees whose salaru greater than his manager salary.
Selecr * from emp e where sal>(selecr sal from emp where empno=e.mgr);
114. To display those employees who are working in the same dept where his
manager is working .
Select * from emp e where deptno=(select deptno from emp where empno=e.mgr);
115. To display those emplouees who are not working under any manager.
Delete from emp where hiredate<’31-dec-1982’and deptno in(select deptno from dept
where loc in(‘NEW YORK’,’CHICAGO’));
SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
120. To display employee name,job,deptname,location for all who are working as
managers.
Select * from emp e where 5>(select count(*) from emp where sal>e.sal) order by sal
desc;
131. To display the name of those employees who are getting highest salary.
SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
Select deprno,count(*) from emp group by deptno having count(*)>3;
134. To display dname where at least 3 are working and display only dname.
Select dname from dept where deptno in(select deptno from emp group by deptno
having count(*)>3);
135. To display name of those manager name whose salary is more than average
ssalary of company.
Select ename,sal from emp where empno in(select mgr from emp) and sal>(select
avg(sal) from emp);
136. To display those managers name whose salary is more than an average salary
of his employees.
Select ename,sal from emp e where empno in(select mgr from emp) and e.sal>(select
avg(sal) from emp where mgr=e.empno);
137. To display wmployee name,sal,comm. and net pay for those employees whose
net pay are greater than or equal to any other employee salary of the company.
Select * from emp e where sal<(select sal from emp where empno=e.mgr) and sal>any
(select sal from emp where empno!=e.mgr);
139. To display the last 5(least) earner of the company.
Select * from emp e where 5>(select count(*) from emp where sal<e.sal) order by sal;
140. To display the number of employees whose salary is greater than there
manager salary
Select count(*) from emp e where sal>(select sal from emp where cempno=e.mge);
141. To display those manager who are not working under president but they are
working under any other manager.
Select * from femp e where mge in(select empno from emp where ename ‘KING’);
142. To delete those department where no employees working.
Delete from dept d where 0=(select coountA(A*) from rmp where deptno=d.deptno);
143. To delete those records frtom emp table whose deptno not available in dept
table.
SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
Delete from emp where deptno not in(select deptno from dept);
144. To display those earners whose salary is out of the grade available in sal grade
table.
Select * from emp where sal<(select min(losal) from salgrade) or sal>(select max(sal)
from salgrade);
145. To display employee name, sal ,comm. and whose net pay is greater than any
other in the company.
146. Selecrt ename,sal ,comm, from emp wher sal+sal*15/100-
sal*5/100+sal*10/100=(select max(sal+sal*15/100-sal*5/100+sal*10/100) from
emp);
147. To display name of those employees who are going to retire 31-dec-99. if the
maximum job is period is 18 years.
Select * from emp where deptno in(select deptno from dept where dname
in(‘SALES’,RESEARCH’));
SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
156. To display the grade of jones.
157. Select grade from salgrade where losal<=(select(sal) from emp where
ename=’JONES’) and hisal>+(select(sal) from emp where
ename=’JONES’);
158. To display those employees who joined the company before 15th of the month.
Delete from emp where deprno in(select deptno from emp group by deptno having
count(*)>3);
160. To delete those employees who hoined the company 21 years back from today.
161. Select * from emp where round((sysdate-hiredate/365)>21;
162. (or)
Select dname from dept where length(dname) in(select count(*) from emp group by
deptno);
164. To display those employees who are working as manager.
Select count(*) from emp where empno in(select mgr from emp);
166. To display the name of then dept those employees who joined the company on
the same date.
Select mgr from emp group by mgr having count(*)=(select max(count(mgr)) from
emp group by mgr);
168. To display employeesname and salary increased by 15% and expredded as
whole number of dollars.
SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
Select empno,ename,lpad(concat(‘$’,round(sal*115/100),7) salary from emp;
169. Produce the output of the emp table “EMPLOYEE_AND_JOB” for ename and job.
173. Union
175. Union
Select empno from emp e where sal<any(select sal from emp where mgr=e.empno);
178. To display the details of all the employees who are sub ordinate to blake.
Select * from emp where mgr=(select empno from emp where ename=’BLAKE’);
179. To display those who working as manager using co related sub query.
182. union
Select * from emp where empno=(select mgt from emp where ename=’JONES’);
183. To define variable represenring the expressions used to calculate on employee’s
total Annual renumaration.
Define efmp_ann_sal=(sal+nvl(comm.,0))*12;
SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
184. To use the variable in a statement which finds all employees who can earn
30000 a year or more.
Select e.ename,e.mgr,e.sal from emp e where sal in (select min(sal) from emp where
mgr=e.mgr) and e.sal>1000 order by sal;
189. To display ename ,job ,annual sal,deptno,dname and grade who earn 30000 per
year and who are not clerk.
190. Select e.ename,e.job,(e.sal+nvl(e.comm,0))*12,e.deptno, d.dname,s.grade from emp
191. e,salgrade s ,dept d where e.sal between s.losal and s.hisal and e.deprno=d.deptno
and(e.sal+nvl(comm.,0))*12>30000 and e.job<>’CLERK’;
192. To display the all employees who joined the company before their manager .
Select * from emp e where hiredate <(select hiredate from emp where empno=e.mgr);
193. To display the all employees by name and number along with their managerd
name and number also display ‘no manager’ who has no manager.
194. Select e.empno,e.ename ,m.empno manafer,m.ename managername from emp e.emp
m where e.mgr=m.empno
195. Union
Select * from emp e where sal=(select max(sal) from emp where job=e.job);
197. To display the employees who earned the min sal for their job in ascending
order.
SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
Select * from emp e where sal=(select min(sal) from emp where job=e.job) order by sal;
198. To display the most recently hired employees in each dept order by hiredate.
Select ename,sal,deptno from emp e where sal>(select avg(sal) from emp where
deptno=e.deptno) orser by deptno;
200. To display the department where there are no employees.
Select deptno,dname from dept where deptno not in(select distinct (deptno) from
emp);
201. To display the dept no with highest annual remuneration bill as compensation.
Select * from emp where sal>(select min(sal) from emp where deptno=30);
206. To display employees who can earn more than every employees in deptno 30.
Sleect * from emp where sal>(select max(sal) from emp where deptno=30);
207. To display avg sal and avg total remainders for each job type.
SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
Selectconcat(upper(substr(ename,0,length(ename)/2)),
lower(substr(ename,lengtah(ename)/2+1,length(ename))))from emp;
210. create copy of emp table.
Select distinct(ename) from emp e where ename in (select ename from emp where
e.empno<>empno);
212. To display all enames in reverse order
Select empno,ename from emp e,salgrade s where e.sal between s.losal and s.hisal and
to_char(hiredate,’mm’)=grade;
214. To display those employee whose hoining date is available in deptno .
SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
Alter table emp add constraint emp_sal_check check (sal<10000);
223. for the time being I have decided that I will not impose this validation. My boss
has agreed to pay more than 10,000
SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
Update emp set comm=comm*10/100 where comm Is not null;
234. Display employee name and department name for each employee.
Note: No rows are released from aggregator until all rows are aggregated
SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)