Вы находитесь на странице: 1из 12

Useful Oracle Sql Queries

1) Display the name of employees along with their annual salary (sal*12) the name of the
employee earning highest annual salary should appear first
Select ename, sal, sal*12 "Annual Salary"
from EMP order by "Annual Salary" desc;
2) Display name! salary! "ra! pf! da! #otalSalary for each employee$ #he out put should %e
in the order of total salary! hra 1&' of salary! D( 1)' of salary $pf &' salary #otal Salary
will %e (salary*hra*da)+pf
Select ename, sal SA, sal*0.15 HA, sal*0.10 !A, sal*5"100 P#, sal$ %sal*0.15& $ %sal*0.10& '
%sal*.05& ()(A*SA*A+ from em, )!E -+ ()(A*SA*A+ !ES.;
,) Display Department num%ers and total num%er of employees wor-ing in each
Department
Select de,tno, count %*& from em, /rou, by de,tno;
.) Display the /arious 0o%s and total num%er of employees wor-ing in each 0o% group
Select 0ob, count %*& from em, /rou, by 0ob;
&) Display department num%ers and #otal Salary for each Department
Select de,tno, sum %sal& from em, /rou, by de,tno;
1) Display department num%ers and 2a3imum Salary from each Department
Select de,tno, ma1 %Sal& from em, /rou, by de,tno;
4) Display /arious 0o%s and #otal Salary for each 0o%
Select 0ob, sum %sal& from em, /rou, by 0ob;
5)Display each 0o% along with min of salary %eing paid in each 0o% group
Select 0ob, m2n %sal& from em, /rou, by 0ob;
6) Display the department 7um%er with more than three employees in each department
Select de,tno, count %*& from em, /rou, by de,tno 3a42n/ count %*&56;
1)) Display /arious 0o%s along with total salary for each of the 0o% where total salary is
greater than .))))
Select 0ob, sum %sal& from em, /rou, by 0ob 3a42n/ sum %Sal&570000;
11) Display the /arious 0o%s along with total num%er of employees in each 0o%$ #he
output should contain only those 0o%s with more than three employees
Select 0ob, count %*& from em, /rou, by 0ob 3a42n/ count %*&56;
12) Display the name of employees who earn "ighest Salary
select ename, sal from em, 83ere sal59%select ma1%sal& from em, &;
1,) Display the employee 7um%er and name for employee wor-ing as cler- and earning
highest salary among the cler-s
select ename,em,no from em, 83ere sal9%select ma1%sal& from em, 83ere 0ob9:.*E;:& and
0ob9:.*E;: ;
1.) Display the names of salesman who earns a salary more than the "ighest Salary of
the 8ler-
select ename,sal from em, 83ere sal5%select ma1%sal& from em, 83ere 0ob9:.*E;:& A<!
0ob9:SA*ESMA<:;
1&) Display the names of cler-s who earn a salary more than the lowest Salary of any
Salesman
select ename,sal from em, 83ere sal5%select m2n%sal& from em, 83ere 0ob9:SA*ESMA<:& and
0ob9:.*E;:;
11) Display the names of employees who earn a salary more than that of 0ones or that of
salary greater than that of scott
select ename,sal from em, 83ere sal5all%select sal from em, 83ere ename9:=)<ES: )
ename9:S.)((:&;
14) Display the names of employees who earn "ighest salary in their respecti/e
departments
select ename,sal,de,tno from em, 83ere sal 2n %select ma1%sal& from em, /rou, by de,tno&;
15) Display the names of employees who earn "ighest salaries in their respecti/e 0o%
9roups
select ename,0ob from em, 83ere sal 2n %select ma1%sal& from em, /rou, by 0ob&;
16)Display employee names who are wor-ing in (ccounting department
select e.ename,d.dname from em, e,de,t d 83ere e.de,tno9d.de,tno and d.dname 9
:A..)><(?<@:;
2)) Display the employee names who are :or-ing in 8hicago
select e.ename,d.loc from em, e,de,t d 83ere e.de,tno9d.de,tno and d.loc9:.H?.A@):;
21) Display the 0o% groups ha/ing #otal Salary greater than the ma3imum salary for
2anagers
select 0ob ,sum%sal& from em, /rou, by 0ob 3a42n/ sum%sal& 5%select ma1%sal& from em, 83ere
0ob9:MA<A@E:&;
22) Display the names of employees from department num%er 1) with salary greater than
that of (7; employee wor-ing in other departments
select ename,de,tno from em, 83ere sal5any%select m2n%sal& from em, 83ere de,tnoA910
/rou, by de,tno& and de,tno910 ;
2,) Display the names of employees from department num%er 1) with salary greater than
that of (<< employee wor-ing in other departments
select ename,de,tno from em, 83ere sal5all%select ma1%sal& from em, 83ere de,tnoA910
/rou, by de,tno& and de,tno910 ;
2.) Display the names of =mployees in Upper 8ase
select u,,er%ename& from em,;
2&) Display the names of employees in <ower 8ase
select *o8er%ename& from em,;
21) Display the names of employees in >roper case
select ?n2t.a,%ename&from em,;
24) ?ind the length of your name using (ppropriate ?unction
select lent/3%:AMA:& from dual;
25) Display the length of all the employee names
select len/t3%ename& from em,;
26) Display the name of employee 8oncatinate with =mployee 7um%er
select ename: :em,no from em,;
,)) Use appropriate function and e3tract , characters starting from 2 characters from the
following string @Oracle@ i$e$! the out put should %e ac
select substr%:)racle:,6,2& from dual;
,1) ?ind the first occurance of character a from the following string 8omputer
2aintenance 8orporation
select lstr%:.om,uter Ma2ntenance .or,orat2on:,:a: & from dual;
,2) Aeplace e/ery occurance of alpha%et ( with B in the string $(lliens (Use #ranslate
function)
select translate%:All2ens:,:A:,:-:& from !ual;
,,) Display the information from the employee ta%le $ where e/er 0o% 2anager is found it
should %e displayed as Boss
select ename ,re,lace%0ob,:MA<A@E:,:-)SS:& from em,;
,.) Display empno!ename!deptno from emp ta%le$ Cnstead of display department
num%ers display the related department name(Use decode function)
select em,no,ename,de,tno,!ecode%de,tno,10,:A..)><(?<@:,20, :ESEA.H: ,60 ,
:SA*ES:,:)PEA(?)<S:&!<ame from em,;
,&) Display your (ge in Days
select sysdate'toBdate%:60'0ul'1CDD:& from dual;
,1) Display your (ge in 2onths
select mont3sBbet8een%sysdate,toBdate%:60'0ul'1CDD:&& from dual;
,4) Display current date as 1&th (ugust ?riday 7ineteen 7ienty Se/en
select (oBc3ar%sysdate,:ddt3 Mont3 !ay year:& from dual;
,6) Scott has 0oined the company on 1,th (ugust ninteen ninety
select em,no,ename,toBc3ar%H2redate,:!ay ddt3 Mont3 year:& from em,;
.)) ?ind the nearest Saturday after 8urrent date
select ne1tBday%sysdate,:Saturday:& from dual;
.1) Display the current time
select (oB.3ar%sysdate,:HHEM?ESS:& from dual;
.2) Display the date three months %efore the 8urrent date
select AddBmont3s%sysdate,'6& from dual
.,) Display the common 0o%s from department num%er 1) and 2)
select 0ob from em, 83ere 0ob 2n %select 0ob from em, 83ere de,tno920& and de,tno910;
..) Display the 0o%s found in department 1) and 2) =liminate duplicate 0o%s
select !2st2nct 0ob from em, 83ere de,tno 2n%10,20&;
.&) Display the 0o%s which are unique to department 1)
select 0ob from em, 83ere de,tno910;
.1) Display the details of those employees who do not ha/e any person wor-ing under
him
select em,no,ename,0ob from em, 83ere em,no not 2n %select m/r from em, 83ere m/r 2s not
null &;
.4) Display the details of those employees who are in sales department and grade is ,
select e.ename,d.dname,/rade from em, e,de,t d ,sal/rade 83ere e.de,tno9d.de,tno and
dname9:SA*ES: and /rade96
.5) Display thoes who are not managers
select ename from em, 83ere 0obA9:MA<A@E:;
.6) Display those employees whose name contains not less than . characters
Select ename from em, 83ere len/t3 %ename&597
&)) Display those department whose name start withDSE while location name ends with
DFD
Select e.ename, d.loc from em, e, de,t d 83ere d.loc l2Fe %:G;:& and enamel2Fe %:SG:&
&1) Display those employees whose manager name is Gones
Select e.ename Su,er2or, e1.ename Subord2nate from em, e, e1 83ere e.em,no 9 e1.m/r and
e.ename9:=)<ES:
&2) Display those employees whose salary is more than ,))) after gi/ing 2)'
increment
Select ename, sal, %sal$%sal*0.20&& from em, 83ere %sal$%sal*0.20&&56000;
&,) Display all employees with their department names
Select e.ename, d.dname from em, e, de,t d 83ere e.de,tno9d.de,tno
&.) Display ename who are wor-ing in sales department
Select e.ename, d.dname from em, e, de,t d 83ere e.de,tno9d.de,tno and d.dname9:SA*ES:
&&) Display employee name! dept name! salary! and commission for those sal in %etween
2))) to &))) while location is 8hicago
Select e.ename, d.dname, e.sal, e.comm from em, e, de,t d 83ere e.de,tno9d.de,tno and sal
bet8een 2000 and 5000
&1) Display those employees whose salary is greater than his managerHs salary
Select e.ename, e.sal, e1.ename, e1.sal from em, e, e1 83ere e.m/r9e1.em,no and
e.sal5e1.sal
&4) Display those employees who are wor-ing in the same dept where his manager is
wor-
Select e.ename, e.de,tno, e1.ename, e1.de,tno from em, e, e1 83ere e.m/r9e1.em,no and
e.de,tno9e1.de,tno
&5) Display those employees who are not wor-ing under any 2anager
Select ename from em, 83ere m/r 2s null;
&6) Display the grade and employees name for the deptno 1) or ,) %ut grade is not .
while 0oined the company %efore ,1+D=8+52
Select ename, /rade, de,tno, sal from em,, sal/rade 83ere %/rade, Sal& 2n %select /rade, Sal
from sal/rade, em, 83ere sal bet8een losal and 32sal& and /radeA 97 and de,tno 2n %10,60& and
32redateH:61'!ec'I2:
1)) Update the salary of each employee %y 1)' increment that are not eligi%le for
commission
>,date em, set sal9 %sal$%sal*0.10&& 83ere comm 2s null
11) Delete those employees who 0oined the company %efore ,1+Dec+52 while their
department <ocation is 7ew ;or- or 8hicago
Select e.ename, e.32redate, d.loc from em, e, de,t d 83ere e.de,tno9d.de,tno and
32redateH:61'!ec'I2: and d.loc 2n%:<EJ +);:,:.H?.A@):&
12) Display employee name! 0o%! deptname! and loc for all who are wor-ing as manager
Select e.ename, e.0ob, d.dname, d.loc from em, e, de,t d 83ere e.de,tno9d.de,tno and
e.em,no 2n %select m/r from em, 83ere m/r 2s not null&
1,) Display those employees whose manager name is Gones and also display their
manager name
Select e.ename sub, e1.ename from em, e, em, e1 83ere e.m/r9e1.em,no and
e1.ename9:=)<ES:
1.) Display name and salary of ford if his salary is equal to hisal of his grade
Select ename, /rade, 32sal, sal from em,, sal/rade 83ere ename9:#)!: and sal932sal; )
Select /rade, sal, 32sal from em,, sal/rade 83ere ename9:#)!: and sal bet8een losal and
32sal; ) Select ename, sal, 32sal, /rade from em,, sal/rade 83ere ename9:#)!: and %/rade,
Sal& 2n %select /rade, 32sal from sal/rade,em, 83ere sal bet8een losal and 32sal&;
1&) Display employee name! 0o%! deptname! his manager name! his grade and ma-e an
under department wise
Select e.ename sub, e1.ename su,, e.0ob, d.dname, /rade from em, e1, sal/rade, de,t d
83ere e.m/r9e1.em,no and e.sal bet8een losal and 32sal and e.de,tno9d.de,tno /rou, by
d.de,tno, e.ename, e1.ename, e.0ob, d.dname, /rade ) Select e.ename sub, e1.ename su,,
e.0ob, d.dname, /rade from em, e, e1, sal/rade, de,t d 83ere e.m/r9e1.em,no and e.sal
bet8een losal and 32sal and e.de,tno9d.de,tno
11) <ist out all the employee names! 0o%! salary! grade and deptname for e/ery one in a
company e3cept I8<=AFH$ Sort on salary display the highest salary
Select e.ename, e.0ob, e.sal, d.dname, /rade from em, e, sal/rade, de,t d 83ere
%e.de,tno9d.de,tno and e.sal bet8een losal and 32sal& order by e.sal desc
14) Display employee name! 0o% and his manager$ Display also employees who are with
out managers
Select e.ename, e1.ename, e.0ob, e.sal, d.dname from em, e, em, e1, de,t d 83ere
e.m/r9e1.em,no %$& and e.de,tno9d.de,tno
15) Display #op & employee of a 8ompany 16) Display the names of those employees
who are getting the highest salary
Select ename, sal from em, 83ere sal 2n %select ma1 %sal& from em,& D0& !2s,lay t3ose
em,loyees 83ose salary 2s eKual to a4era/e of ma12mum and m2n2mumL Select * from em,
83ere sal9%select %ma1 %sal&$m2n %sal&&"2 from em,&
41) Select count of employees in each department where count J,
Select count %*& from em, /rou, by de,tno 3a42n/ count %*&56
42) Display dname where atleast three are wor-ing and display only deptname
select d.dname from de,t d, em, e 83ere e.de,tno9d.de,tno /rou, by d.dname 3a42n/
count%*&56
4,) Display name of those managers name whose salary is more than a/erage salary of
8ompany
Select d2st2nct e1.ename, e1.sal from em, e, e1, de,t d 83ere e.de,tno9d.de,tno and
e.m/r9e1.em,no and e1.sal5 %select a4/ %sal& from em,&
4.) Display those managers name whose salary is more than a/erage salary of his
employees
Select d2st2nct e1.ename, e1.sal from em, e, e1, de,t d 83ere e.de,tno9d.de,tno and
e.m/r9e1.em,no and e1.sal5any %select a4/ %sal& from em, /rou, by de,tno&
4&) Display employee name! sal! comm and net pay for those employees whose net pay
is greater than or equal to any other employee salary of the company
Select ename, sal, <M* %comm, 0&, sal$<M* %comm, 0& from em, 83ere sal$<M* %comm, 0&
5any %select e.sal from em, e&
41) Display those employees whose salary is less than his manager %ut more than salary
of other managers
Select e.ename sub, e.sal from em, e, e1, de,t d 83ere e.de,tno9d.de,tno and
e.m/r9e1.em,no and e.salany %select e2.sal from em, e2, e, de,t d1 83ere e.m/r9e2.em,no
and d1.de,tno9e.de,tno&
44) Display all employeesH names with total sal of company with each employee name
45) ?ind the last &(least) employees of company
46) ?ind out the num%er of employees whose salary is greater than their managers
salary
Select e.ename, e.sal, e1.ename, e1.sal from em, e, e1, de,t d 83ere e.de,tno9d.de,tno and
e.m/r9e1.em,no and e.sal5e1.sal
5)) Display the manager who are not wor-ing under president %ut they are wor-ing under
any other manager
Select e2.ename from em, e1, em, e2, em, e6 83ere e1.m/r9e2.em,no and
e2.m/r9e6.em,no and e6.0obA 9:PES?!E<(:;
51) Delete those department where no employee wor-ing
!elete from em, 83ere em,no 2s null;
52) Delete those records from emp ta%le whose deptno not a/aila%le in dept ta%le
!elete from em, e 83ere e.de,tno not 2n %select de,tno from de,t&
5,) Display those enames whose salary is out of grade a/aila%le in salgrade ta%le
Select em,no, sal from em, 83ere salH%select m2n %*)SA*& from sal/rade& ) sal5%select ma1
%32sal& from sal/rade&
5.) Display employee name! sal! comm and whose net pay is greater than any other in
the company
Select ename, sal, comm, sal$comm from em, 83ere sal$comm5any %select sal$comm from
em,&
5&) Display name of those employees who are going to retire ,1+Dec+66 if ma3imum 0o%
period is ,) years
Select em,no, 32redate, sysdate, toBc3ar %sysdate,:yyyy:& ' toBc3ar %32redate,:yyyy:& from em,
83ere toBc3ar %sysdate,:yyyy:& ' toBc3ar %32redate,:yyyy:&960
51) Display those employees whose salary is odd /alue
Select ename, sal from em, 83ere mod %sal, 2&A 90
54) Display those employees whose salary contains atleast , digits
Select ename, sal from em, 83ere len/t3 %sal&96
55) Display those employees who 0oined in the company in the month of Dec
Select em,no, ename from em, 83ere tr2m %toBc3ar %32redate,:Mon:&&9tr2m %:!E.:&
56) Display those employees whose name contains (
Select ename from em, 83ere ename l2Fe %:GAG:&
6)) Display those employees whose deptno is a/aila%le in salary
Select ename, sal from em, 83ere de,tno 2n %select d2st2nct sal from em,&;
61) Display those employees whose first 2 characters from hiredate + last 2 characters
sal
Select em,no, 32redate, sal from em, 83ere tr2m %substr %32redate, 1,2&&9tr2m %substr %sal, '2,2&&;
)
Select 32redate, sal from em, 83ere toB.3ar %32redate,:dd:&9tr2m %substr %sal, '2,2&&
62) Display those employees whose 1)' of salary is equal to the year 0oining
Select ename, sal, 0.10*sal from em, 83ere 0.10*sal9tr2m %toBc3ar %32redate,:yy:&&
6,) Display those employees who are wor-ing in sales or research
Select e.ename from em, e, de,t d 83ere e.de,tno9d.de,tno and d.dname 2n
%:SA*ES:,:ESEA.H:&;
6.) Display the grade of Gones
Select ename, /rade from em,, sal/rade 83ere %/rade, Sal& 9%select /rade, Sal from sal/rade,
em, 83ere sal bet8een losal and 32sal and ename9:=)<ES:&
6&) Display those employees who 0oined the company %efore 1&th of the month
select ename ,32redate from em, 83ere 32redateH:15'=ul'02: and 32redate 59:01'0ul'02:;
61) Display those employees who has 0oined %efore 1&th of the month
select ename ,32redate from em, 83ere 32redateH:15'=ul'02: CD& !elete t3ose records 83ere no
of em,loyees 2n ,art2cular de,artment 2s less t3an 6L delete from em, 83ere de,tno 2n %select
de,tno from em, /rou, by de,tno 3a42n/ count%*& H6 CI& !elete t3ose em,loyee83o 0o2ned t3e
com,any 10 years bacF from todayL delete from em, 83ere em,no 2n %select em,no from em,
83ere toBc3ar%sysdate,:yyyy:&' toBc3ar%32redate,:yyyy:&5910&
66) Display the deptname the num%er of characters of which is equal to no of employee
in any other department
1))) Display the deptname where no employee is wor-ing
select de,tno from em, 83ere em,no 2s null;
1)1) Display those employees who are wor-ing as manager
select e2.ename from em, e1,e2 83ere e1.m/r9e2.em,no and e2.em,no 2s not null
1)2) 8ount th num%er of employees who are wor-ing as managers (Using set opetrator)
select d.dname from de,t d 83ere len/t3%d.dname& 2n %select count%*& from em, e 83ere
e.de,tnoA9d.de,tno /rou, by e.de,tno&
1),) Display the name of the dept those employees who 0oined the company on the same
date
select a.ename,b.ename from em, a,em, b 83ere a.32redate9b.32redate and a.em,noA
9b.em,no
1).) Display those employees whose grade is equal to any num%er of sal %ut not equal to
first num%er of sal
select ename,sal,/rade ,substr%sal,/rade,1& from em,,sal/rade 83ere /radeA9 substr %sal,1,1&
and /rade 9 substr%sal,/rade,1& and sal bet8een losal and 32sal
1)&) 8ount the no of employees wor-ing as manager using set operation
Select count%em,no& from em, 83ere em,no 2n %select a.em,no from em, a 2ntersect select
b.m/r from em, b&
1)1) Display the name of employees who 0oined the company on the same date
select a.ename,b.ename from em, a,em, b 83ere a.32redate9b.32redate and a.em,noA
9b.em,no;
1)4) Display the manager who is ha/ing ma3imum num%er of employees wor-ing under
him
select e2.ename,count%*& from em, e1,e2 83ere e1.m/r9e2.em,no /rou, by e2.ename Ha42n/
count%*&9%select ma1%count%*&& from em, e1,e2 83ere e1.m/r9e2.em,no /rou, by e2.ename&
1)5) <ist out the employee name and salary increased %y 1&' and e3press as whole
num%er of Dollars
select ename,sal,l,ad%translate%sal,sal,%%sal $%sal*0.15&&"50&&,5,:N:& from em,
1.4) >roduce the output of the empta%le D=2><O;==K(7D GOBD for ename and 0o%
AnsE select ename"EMP*)+EEBA<!",0ob"=)-" #)M EMP;
1.5) <ist of employees with hiredate in the format of @Gune . 1655@
AnsE select ename,toBc3ar%32redate,:Mont3 dd yyyy:& from em,;
1.6) print list of employees displaying @Gust salary@ if more than 1&)) if e3actly 1&))
display @on taget@ if less than 1&)) display %elow 1&))
AnsE select ename,sal,
%
case 83en sal H 1500 t3en :-elo8B(ar/et: 83en sal91500 t3en :)nB(ar/et: 83en sal 5 1500
t3en
:Abo4eB(ar/et:
else
:FFFFF:
end
&
from em,
1&)) :hich query to calculate the length of time any employee has %een with the
company
AnsE select 32redate, toBc3ar %32redate,: HHEM?ESS:& #)M em,
1&1) 9i/en a string of the format @nnLnn@ $ Merify that the first and last 2 characters are
num%ers$ (nd that the middle character is @L@ >rint the e3pressions @;es@ C? /alid I7OH of
not /alid$ Use the following /alues to test your solution@12L&.@! )1L1a! @66L65@
AnsE
1&2) =mployes hire on OA Before 1&th of any month are paid on the last friday of that
month those hired after 1&th are paid the last friday of th following month $print a list of
employees $ their hiredate and first pay date sort those who se salary contains first digit
of their deptno
AnsE select ename,32redate, *AS(B!A+ % ne1tBday%32redate,:#r2day:&&,
%
case 83en toBc3ar%32redate,:dd:& H9%:15:& t3en *AS(B!A+ % ne1tBday%32redate,:#r2day:&& 83en
toBc3ar%32redate,:dd:&5%:15:& t3en
*AS(B!A+% ne1tBday%addBmont3s%32redate,1&,:#r2day:&&
end
& from em,
1&,) Display those managers who are getting less than his employees salary
AnsE select a.em,no, a.ename, a.sal, b.sal, b.em,no, b.ename from em, a, em, b 83ere
a.m/r9b.em,no and a.sal5b.sal
1&.) >rint the details of employees who are su%ordinates to B<(F=
AnsE select a.em,no,a.ename ,b.ename from em, a, em, b 83ere a.m/r9b.em,no
and b.ename9:-*A;E:

Вам также может понравиться