You are on page 1of 28

SQL Queries(1)

1.Display the dept information from department table


select * from dept;
2.Display the details of all employees
select * from emp;
3.Display the name and job for all employees
select ename,job from emp;
4.Display name and salary for all employees
select ename,sal from emp;
5.Display employee number and total salary for eah employee
select empno,sal+comm from emp;
!.Display employee name and annual salary for all employees
select empno,ename,12*sal+nvl(comm,0) annualsal from emp;
".Display the names of all employees #ho are #or$in% in department number 1&
select ename from emp where deptno = 10;
'.Display the names of all employees #or$in% as ler$s and dra#in% a salary more than 3&&&
select ename from emp wher job = '!"#$' and sal % &000;
(.Display employee number and names for employees #ho earn ommission
select empno,ename from emp where comm 's not null and comm % 0;
1&.Display names of employees #ho do not earn any ommission
select empno,ename from emp where comm 's null and comm = 0;
11.Display the names of employees #ho are #or$in% as ler$ ) salesman or analyst and dra#in%
a salary more than 3&&&
select ename from emp where (job='!"#$' or job='()!"(*)+' or job=')+)!,(-') and
sal%&000;
12.Display the names of employees #ho are #or$in% in the ompany for the past 5 years
select ename from emp where s.sdate / h'redate % 0*&10;
13.Display the list of employees #ho ha*e joined the ompany before 3& th june (& or after 31 st
de (&
select * from emp where h'redate between '&0/jun/1220' and '&1/dec/1220';
14.Display urrent date
select s.sdate from dual;
15.Display the list of users in your database (usin% lo% table)
select * from dba3users;
1!.Display the names of all tables from the urrent user
select * from tab;
1".Display the name of the urrent user
show user;
1'.Display the names of employees #or$in% in department number 1& or 2& or 4& or employees
#or$in% as ler$s ) salesman or analyst
select ename from emp where deptno 'n (10,20,40) or job 'n ('!"#$','()!"(*)+',')+)!,(-');
1(.Display the names of employees #hose name starts #ith alphabet s
select ename from emp where ename l'5e '(6';
2&.Display employee name from employees #hose name ends #ith alphabet S
select ename from emp where ename l'5e '6(';
21.Display the names of employees #hose names ha*e senond alphabet + in their names
select ename from emp where ename l'5e '3(6';
22.Display the names of employees #hose name is e,atly fi*e haraters in len%th
select ename from emp where len7th(ename)=0;
or
select ename from emp where ename l'5e '33333';
23.Display the names of employees #ho are not #or$in% as mana%ers
select * from emp m'nus (select * from emp where empno 'n (select m7r from emp));
or
select * from emp where empno not 'n (select m7r from emp where m7r 's not null);
or
select * from emp e where empno not 'n (select m7r from emp where e8empno=m7r);
24.Display the names of employees #ho are not #or$in% as S+L-S.+/ or 0L-12 or
+/+L3S4
select job from emp where job not 'n ('!"#$',')+)!,(-','()!"(*)+');
25.Display all ro#s from emp table. 4he system should #ait after e*ery sreen full of
information
set pause on;
2!.Display the total number of employees #or$in% in the ompany
select count(*) from emp;
2".Display the total salary and total ommission to all employees
select sum(sal), sum(nvl(comm,0)) from emp;
2'.Display the ma,imum salary from emp table
select ma9(sal) from emp;
2(.Display the minimum salary from emp table
select m'n(sal) from emp;
3&.Display the a*era%e salary from emp table
select av7(sal) from emp;
31.Display the ma,imum salary bein% paid to 0L-12
select ma9(sal) from emp where job='!"#$';
32.Display the ma,imum salary bein% paid in dept no 2&
select ma9(sal) from emp where deptno=20;
33.Display the minimum salary bein% paid to any S+L-S.+/
select m'n(sal) from emp where job='()!"(*)+';
34.Display the a*era%e salary dra#n by mana%ers.
select av7(sal) from emp where job='*)+):"#';
35.Display the total salary dra#n by analyst #or$in% in dept no 4&
select sum(sal)+sum(nvl(comm,0)) from emp where deptno=40;
3!.Display the names of employees in order of salary i.e. the name of the employee earnin%
lo#est salary shoud appear first
select ename from emp order b. sal;
3".Display the names of employees in desendin% order of salary
select ename from emp order b. sal desc;
3'.Display the details from emp table in order of emp name
select ename from emp order b. ename;
3(.Display empnno)ename)deptno and sal. Sort the output first based on name and #ithin
name by deptno and #itdhin deptno by sal5
select * from emp order b. ename,deptno,sal;
4&) Display the name of employees alon% #ith their annual salary(sal612).
the name of the emplo.ee earn'n7 h'7hest annual salar. should appear f'rst;
)ns<select ename,sal,sal*12 =)nnual (alar.= from emp order b. =)nnual (alar.= desc;
41) Display name)salary)7ra)pf)da)4otalSalary for eah employee.
4he out put should be in the order of total salary )hra 158 of salary )D+ 1&8 of salary .pf 58
salary 4otal Salary #ill be (salary9hra9da):pf;
)ns< select ename,sal (),sal*0810 >#),sal*0810 ?),sal*0@100 AB, sal+(sal*0810)+(sal*0810)/
(sal*800) -C-)!()!)#,
from emp C#?"# D, -C-)!()!)#, ?"(;
42) Display Department numbers and total number of employees #or$in% in eah Department;
)ns< select deptno,count(*) from tvsemp 7roup b. deptno;
43) Display the *arious jobs and total number of employees #or$in% in eah job %roup;
)ns< select job,count(*) from tvsemp 7roup b. job;
44)Display department numbers and 4otal Salary for eah Department;
)ns< select deptno,sum(sal) from tvsemp 7roup b. deptno;
45)Display department numbers and .a,imum Salary from eah Department;
)ns< select deptno,ma9((al) from tvsemp 7roup b. deptno;
4!)Display *arious jobs and 4otal Salary for eah job;
)ns< select job,sum(sal) from tvsemp 7roup b. job;
4")Display eah job alon% #ith min of salary bein% paid in eah job %roup;
)ns< select job ,m'n(sal) from tvsemp 7roup b. job;
4') Display the department /umber #ith more than three employees in eah department;
)ns< select deptno ,count(*) from tvsemp 7roup b. deptno hav'n7 count(*)%&;
4() Display *arious jobs alon% #ith total salary for eah of the job #here total salary is %reater
than 4&&&&;
)ns< select job,sum(sal) from tvsemp 7roup b. job hav'n7 sum(()l)%40000;
5&) Display the *arious jobs alon% #ith total number of employees in eah job.4he
output should ontain only those jobs #ith more than three employees;
)ns< select job,count(*) from tvsemp 7roup b. job hav'n7 count(*)%&;
51) Display the name of employees #ho earn 7i%hest Salary;
)ns< select ename, sal from tvsemp where sal%=(select ma9(sal) from tvsemp );
52) Display the employee /umber and name for employee #or$in% as ler$ and earnin% hi%hest
salary amon% the ler$s;
)ns< select ename,empno from tvsemp where sal=(select ma9(sal) from tvsemp where job='!"#$')
and job='!"#$' ;
53) Display the names of salesman #ho earns a salary more than the 7i%hest Salary of the
ler$;
)ns< select ename,sal from tvsemp where sal%(select ma9(sal) from tvsemp where job='!"#$')
)+? job='()!"(*)+';
54) Display the names of ler$s #ho earn a salary more than the lo#est Salary of any
salesman;
)ns< select ename,sal from tvsemp where sal%(select m'n(sal) from tvsemp where
job='()!"(*)+') and job='!"#$';
55) Display the names of employees #ho earn a salary more than that of jones or that of salary
%reater than that of sott;
)ns< select ename,sal from tvsemp where sal%all(select sal from tvsemp where ename='EC+"(' C#
ename='(C--');
5!) Display the names of employees #ho earn 7i%hest salary in their respeti*e departments;
)ns< select ename,sal,deptno from tvsemp where sal 'n (select ma9(sal) from tvsemp 7roup b.
deptno);
5") Display the names of employees #ho earn 7i%hest salaries in their respeti*e job <roups;
)ns< select ename,job from tvsemp where sal 'n (select ma9(sal) from tvsemp 7roup b. job);
5') Display employee names #ho are #or$in% in +ountin% department;
)ns< select e8ename,d8dname from emp e,dept d where e8deptno=d8deptno and
d8dname=')CF+-G+:';
5() Display the employee names #ho are =or$in% in 0hia%o;
)ns< select e8ename,d8loc from emp e,tvsdept d where e8deptno=d8deptno and d8loc='>G):C';
!&) Display the job %roups ha*in% 4otal Salary %reater than the ma,imum salary for
.ana%ers;
)ns< select job ,sum(sal) from tvsemp 7roup b. job hav'n7 sum(sal) %(select ma9(sal) from tvsemp
where job='*)+):"#');
!1) Display the names of employees from department number 1& #ith salary %reater than that
of +/3 employee #or$in% in other departments;
)ns< select ename,deptno from tvsemp where sal%an.(select m'n(sal) from tvsemp where deptnoH
=10 7roup b. deptno) and deptno=10 ;
!2) Display the names of employees from department number 1& #ith salary %reater than that
of +LL employee #or$in% in other departments;
)ns< select ename,deptno from tvsemp where sal%all(select ma9(sal) from tvsemp where deptnoH=10
7roup b. deptno) and deptno=10 ;
!3) Display the names of mployees in >pper 0ase;
)ns< select upper(ename) from tvsemp;
!4) Display the names of employees in Lo#er 0ase;
)ns< select !ower(ename) from tvsemp;
!5) Display the names of employees in ?roper ase;
)ns< select Gn'tap(ename)from tvsemp;
Q@!!) Aind the len%th of your name usin% +ppropriate Auntion;
)ns< select lent7h('#)*)') from dual;
!") Display the len%th of all the employee names;
)ns< select len7th(ename) from tvsemp;
!') Display the name of employee 0onatinate #ith -mployee /umber;
)ns< select enameII' 'IIempno from tvsemp;
!() >se appropriate funtion and e,trat 3 haraters startin% from 2 haraters from the
follo#in% strin% BCraleB i.e.) the out put should be a;
)ns< select substr('Cracle',&,2) from dual;
"&) Aind the first ourane of harater a from the follo#in% strin% 0omputer .aintenane
0orporation;
)ns< select lstr('omputer *a'ntenance orporat'on','a' ) from dual;
"1) 1eplae e*ery ourane of alphabet + #ith D in the strin% .+lliens (>se 4ranslate
funtion);
)ns< select translate(')ll'ens',')','D') from ?ual;
"2) Display the information from the employee table . #here e*er job .ana%er is found it
should be displayed as Doss;
)ns< select ename ,replace(job,'*)+):"#','DC((') from tvsemp;
"3) Display empno)ename)deptno from t*semp table. Enstead of display department numbers
display the related department name(>se deode funtion);
)ns< select empno,ename,deptno,?ecode(deptno,10,')CF+-G+:'
,20,'#"(")#>',&0,'()!"(','CA"#)-GC+(')?+ame from tvsemp;
"4) Display your +%e in Days;
)ns< select s.sdate/to3date('&0/jul/12JJ') from dual;
"5) Display your +%e in .onths;
)ns< select months3between(s.sdate,to3date('&0/jul/12JJ')) from dual;
"!) Display urrent date as 15th +u%ust Ariday /ineteen /ienty Se*en;
)ns< select -o3char(s.sdate,'ddth *onth ?a. .ear') from dual;
"") Display the follo#in% output for eah ro# from t*semp table;
)ns< K<JL
"') Sott has joined the ompany on 13th +u%ust ninteen ninety;
)ns< select empno,ename,to3char(>'redate,'?a. ddth *onth .ear') from tvsemp;
"() Aind the nearest Saturday after 0urrent date;
)ns< select ne9t3da.(s.sdate,'(aturda.') from dual;
'&) Display the urrent time;
)ns< select -o3har(s.sdate,'>><*G<((') from dual;
'1) Display the date three months before the 0urrent date;
)ns< select )dd3months(s.sdate,/&) from dual
'2) Display the ommon jobs from department number 1& and 2&;
)ns< select job from tvsemp where job 'n (select job from tvsemp where deptno=20) and deptno=10;
'3) Display the jobs found in department 1& and 2& -liminate dupliate jobs;
)ns< select ?'st'nct job from tvsemp where deptno 'n(10,20);
'4) Display the jobs #hih are uniFue to department 1&;
)ns< select job from tvsemp where deptno=10;
'5) Display the details of those employees #ho do not ha*e any person #or$in% under him;
)ns< select empno,ename,job from tvsemp where empno not 'n (select m7r from tvsemp where m7r
's not null );
'!) Display the details of those employees #ho are in sales department and %rade is 3;
)ns< select e8ename,d8dname,7rade from emp e,dept d ,sal7rade where e8deptno=d8deptno and
dname='()!"(' and 7rade=&;
'") Display thoes #ho are not mana%ers;
)ns< select ename from tvsemp where jobH='*)+):"#';
'') Display those employees #hose name ontains not less than 4 haraters;
)ns< select ename from tvsemp where len7th(ename)%=4
'() Display those department #hose name start #ithGSG #hile loation name ends #ith G2G;
)ns< select e8ename,d8loc from tvsemp e ,tvsdept d where d8loc l'5e('6$') and ename l'5e('(6')
(&) Display those employees #hose mana%er name is Hones;
)ns< select e8ename (uper'or,e18ename (ubord'nate from tvsemp e,e1 where e8empno=e18m7r and
e8ename='EC+"(';
(1) Display those employees #hose salary is more than 3&&& after %i*in% 2&8 inrement;
)ns< select ename,sal,(sal+(sal*0820)) from tvsemp where (sal+(sal*0820))%&000;
(2) Display all employees #ith their department names;
)ns< select e8ename,d8dname from tvsemp e, tvsdept d where e8deptno=d8deptno
(3) Display ename #ho are #or$in% in sales department;
)ns< select e8ename,d8dname from emp e,dept d where e8deptno=d8deptno and d8dname='()!"(';
(4) Display employee name)dept name)salary)and ommission for those sal in bet#een 2&&&
to 5&&& #hile loation is 0hia%o;
)ns< (elect e8ename,d8dname,e8sal,e8comm from tvsemp e,dept d where e8deptno=d8deptno and sal
between 2000 and 0000;
(5) Display those employees #hose salary is %reater than his mana%ers salary;
)ns< (elect e8ename,e8sal,e18ename,e18sal from tvsemp e,e1 where e8m7r=e18empno and e8sal%e18sal;
(!) Display those employees #ho are #or$in% in the same dept #here his mana%er is #or$;
)ns< select e8ename,e8deptno,e18ename,e18deptno from tvsemp e,e1 where e8m7r=e18empno and
e8deptno=e18deptno;
(") Display those employees #ho are not #or$in% under any .ana%er;
)ns< select ename from tvsemp where m7r 's null;
(') Display the %rade and employees name for the deptno 1& or 3& but %rade is not 4 #hile
joined the ompany before 31:D-0:'2;
)ns< select ename,7rade,deptno,sal from tvsemp ,sal7rade where ( 7rade,sal) 'n
( select 7rade,sal from sal7rade,tvsemp where sal between losal and h'sal)
and 7radeH=4 and deptno 'n (10,&0) and h'redateM'&1/?ec/L2';
(() >pdate the salary of eah employee by 1&8 inrement #ho are not eli%ible for ommission;
)ns< update tvsemp set sal= (sal+(sal*0810)) where comm 's null;
1&&) Delete those employees #ho joined the ompany before 31:De:'2 #hile their department
Loation is /e# 3or$ or 0hia%o;
)ns< select e8ename,e8h'redate,d8loc from tvsemp e,tvsdept d where
e8deptno=d8deptno and h'redateM'&1/?ec/L2' and d8loc 'n('+"N ,C#$','>G):C');
1&1) Display employee name )job)deptname)lo for all #ho are #or$in% as mana%er;
)ns< select e8ename,e8job,d8dname,d8loc from tvsemp e,tvsdept d where e8deptno=d8deptno
and e8empno 'n (select m7r from tvsemp where m7r 's not null);
1&2) Display those employees #hose mana%er name is jones and also display their mana%er
name;
)ns< select e8ename sub,e18ename from tvsemp e,e1 where e8m7r=e18empno and e18ename='EC+"(';
1&3) Display name and salary of ford if his salary is eFual to hisal of his %rade;
)ns< select ename,7rade,h'sal,sal from emp,sal7rade where ename='BC#?' and sal=h'sal;
C#
select 7rade,sal,h'sal from tvsemp,sal7rade where ename='BC#?' and sal between losal and h'sal;
C#
select ename,sal,h'sal,7rade from tvsemp,sal7rade where ename='BC#?'
and (7rade,sal) 'n (select 7rade,h'sal from sal7rade,tvsemp where
sal between losal and h'sal);
1&4) Display employee name )job)deptname)his mana%er name )his %rade and ma$e an
under department #ise;
)ns< select e8ename sub,e18ename sup,e8job,d8dname ,7rade from tvsemp e,e1,sal7rade,tvsdept d
where e8m7r=e18empno and e8sal between losal and h'sal and e8deptno=d8deptno 7roup b.
d8deptno,e8ename,e18ename,e8job,d8dname,7rade;
C#
select e8ename sub,e18ename sup,e8job,d8dname ,7rade from tvsemp e,e1,sal7rade,tvsdept d where
e8m7r=e18empno and e8sal between losal and h'sal and e8deptno=d8deptno
1&5) List out all the employee names )job)salary)%rade and deptname for e*ery one in a
ompany e,ept B0L-12B . Sort on salary display the hi%hest salary;
)ns< select e8ename ,e8job,e8sal,d8dname ,7rade from tvsemp e,sal7rade,tvsdept d where
(e8deptno=d8deptno and e8sal between losal and h'sal ) order b. e8sal desc
1&!) Display employee name)job abd his mana%er .Display also employees #ho are #ith out
mana%ers;
)ns< select e8ename ,e18ename,e8job,e8sal,d8dname from tvsemp e,e1,tvsdept d where
e8m7r=e18empno(+) and e8deptno=d8deptno
1&") Display 4op 5 employee of a 0ompany;
)ns<
1&') Display the names of those employees #ho are %ettin% the hi%hest salary;
)ns< select ename,sal from tvsemp where sal 'n (select ma9(sal) from tvsemp)
1&() Display those employees #hose salary is eFual to a*era%e of ma,imum and minimum;
)ns< select * from tvsemp
where sal=(select (ma9(sal)+m'n(sal))@2 from tvsemp)
11&) Selet ount of employees in eah department #here ount I3;
)ns< select count(*) from tvsemp 7roup b. deptno hav'n7 count(*)%&
111) Display dname #here atleast three are #or$in% and display only deptname;
)ns< select d8dname from tvsdept d, tvsemp e where e8deptno=d8deptno 7roup b. d8dname hav'n7
count(*)%&;
112) Display name of those mana%ers name #hose salary is more than a*era%e salary of
0ompany;
)ns< select d'st'nct e18ename,e18sal from tvsemp e,e1,dept d where e8deptno=d8deptno and
e8m7r=e18empno and e18sal% (select av7(sal) from tvsemp);
113) Display those mana%ers name #hose salary is more than a*era%e salary salary of his
employees;
)ns< select d'st'nct e18ename,e18sal from tvsemp e,e1,dept d where e8deptno=d8deptno and
e8m7r=e18empno and e18sal%an. (select av7(sal) from tvsemp 7roup b. deptno);
114) Display employee name)sal)omm and netpay for those employees #hose netpay is
%reater than or eFual to any other employee salary of the ompany;
)ns< select ename,sal,+O!(comm,0),sal++O!(comm,0) from tvsemp where
sal++O!(comm,0) %an. (select e8sal from tvsemp e );
115) Display those employees #hose salary is less than his mana%er but more than salary of
other mana%ers;
)ns< select e8ename sub,e8sal from tvsemp e,e1,tvsdept d where
e8deptno=d8deptno and e8m7r=e18empno
and e8salMe18sal
and e8sal %an. (select e28sal from tvsemp e2, e,tvsdept d1 where
e8m7r=e28empno and d18deptno=e8deptno);
11!) Display all employees names #ith total sal of ompany #ith eah employee name;
)ns<
11") Aind the last 5(least) employees of ompany;
)ns<
11') Aind out the number of employees #hose salary is %reater than their mana%ers salary;
)ns< select e8ename,e8sal,e18ename,e18sal from tvsemp e,e1,tvsdept d where e8deptno=d8deptno and
e8m7r=e18empno and e8sal%e18sal
11() Display the mana%er #ho are not #or$in% under president but they are #or$in% under
any other mana%er;
)ns< select e28ename from emp e1,emp e2,emp e& where e18m7r=e28empno and e28m7r=e&8empno
and e&8jobH='A#"(G?"+-';
12&) Delete those department #here no employee #or$in%;
)ns< delete from tvsemp where empno 's null;
121) Delete those reords from emp table #hose deptno not a*ailable in dept table;
)ns< delete from tvsemp e where e8deptno not 'n (select deptno from tvsdept)
122) Display those enames #hose salary is out of %rade a*ailable in sal%rade table;
)ns< select empno,sal from tvsemp where salM(select m'n(!C()!) from sal7rade )
C# sal%(select ma9(h'sal) from sal7rade)
123) Display employee name)sal)omm and #hose netpay is %reater than any othere in the
ompany;
)ns< select ename,sal,comm,sal+comm from tvsemp where sal+comm%an.
(select sal+comm from tvsemp )
124) Display name of those employees #ho are %oin% to retire 31:De:(( if ma,imum job period
is 3& years;
)ns< select empno, h'redate,s.sdate, to3char(s.sdate,'....') / to3char(h'redate,'....')
from tvsemp where to3char(s.sdate,'....') / to3char(h'redate,'....')=&0
125) Display those employees #hose salary is odd *alue;
)ns< select ename ,sal from tvsemp where mod(sal,2)H=0
12!) Display those employees #hose salary ontains atleast 3 di%its;
)ns< select ename,sal from tvsemp where len7th(sal)=&
12") Display those employees #ho joined in the ompany in the month of De;
)ns< (elect empno,ename from tvsemp where tr'm(to3char(h'redate,'*on'))=tr'm('?"')
12') Display those employees #hose name ontains +;
)ns< select ename from tvsemp where ename l'5e('6)6')
12() Display those employees #hose deptno is a*ailable in salary;
)ns< select ename,sal from tvsemp where deptno 'n (select d'st'nct sal from tvsemp);
13&) Display those employees #hose first 2 haraters from hiredate : last 2 haraters sal;
)ns< select empno,h'redate,sal from tvsemp where tr'm(substr(h'redate,1,2))=tr'm(substr(sal,/2,2));
or
select h'redate,sal from tvsemp where to3har(h'redate,'dd')=tr'm(substr(sal,/2,2))
131) Display those employeess #hose 1&8 of salary is eFual to the year joinin%;
)ns< select ename ,sal,0810*sal from tvsemp where 0810*sal=tr'm(to3char(h'redate,'..'))
132) Display those employees #ho are #or$in% in sales or researh;
)ns< select e8ename from tvsemp e ,tvsdept d where e8deptno=d8deptno and d8dname
'n('()!"(','#"(")#>');
133) Display the %rade of jones;
)ns< select ename,7rade from tvsemp,sal7rade where ( 7rade,sal) =
(select 7rade,sal from sal7rade,tvsemp where sal between losal and h'sal and ename='EC+"(')
134) Display those employees #ho joined the ompany before 15th of the month;
)ns< select ename ,h'redate from tvsemp where h'redateM'10/Eul/02' and h'redate %='01/jul/02';
135) Display those employees #ho has joined before 15th of the month;
)ns< select ename ,h'redate from tvsemp where h'redateM'10/Eul/02'
13!) Delete those reords #here no of employees in partiular department is less than 3;
)ns< delete from tvsemp where deptno 'n (select deptno from tvsemp 7roup b. deptno hav'n7
count(*) M&
13"+) Delete those employee#ho joined the ompany 1& years ba$ from today;
)ns< delete from tvsemp where empno 'n (select empno from tvsemp
where to3char(s.sdate,'....')/ to3char(h'redate,'....')%=10)
13"D) Display the deptname the number of haraters of #hih is eFual to no of employee
in any other department;
)ns<
13') Display the deptname #here no employee is #or$in%;
)ns< select deptno from tvsemp where empno 's null;
13() Display those employees #ho are #or$in% as mana%er;
)ns< select e28ename from tvsemp e1,e2 where e18m7r=e28empno and e28empno 's not null
14&) 0ount th number of employees #ho are #or$in% as mana%ers (>sin% set opetrator);
)ns< select d8dname from tvsdept d where len7th(d8dname) 'n (select count(*) from tvsemp e where
e8deptnoH=d8deptno 7roup b. e8deptno)
141) Display the name of the dept those employees #ho joined the ompany on the same date;
)ns< select a8ename,b8ename from tvsemp a,tvsemp b where a8h'redate=b8h'redate and a8empnoH
=b8empno
142) Display those employees #hose %rade is eFual to any number of sal but not eFual to first
number of sal;
)ns< select ename,sal,7rade ,substr(sal,7rade,1) from tvsemp,sal7rade where
7radeH=substr(sal,1,1) and 7rade = substr(sal,7rade,1)
and sal between losal and h'sal
143) 0ount the no of employees #or$in% as mana%er usin% set operation;
)ns< (elect count(empno) from tvsemp where
empno 'n (select a8empno from tvsemp a
'ntersect
select b8m7r from tvsemp b)
144) Display the name of employees #ho joined the ompany on the same date;
)ns< select a8ename,b8ename from tvsemp a,tvsemp b where a8h'redate=b8h'redate and a8empnoH
=b8empno;
145) Display the mana%er #ho is ha*in% ma,imum number of employees #or$in% under him;
)ns< select e28ename,count(*) from tvsemp e1,e2 where e18m7r=e28empno 7roup b. e28ename
>av'n7 count(*)=(select ma9(count(*)) from tvsemp e1,e2 where e18m7r=e28empno 7roup b.
e28ename)
14!) List out the employee name and salary inreased by 158 and e,press as #hole number of
Dollars;
)ns< select ename,sal,lpad(translate(sal,sal,((sal +(sal*0810))@00)),0,'P') from tvsemp
14") ?rodue the output of the emptable G-.?LC3--J+/D HCDG for ename and job ;
)ns< select ename="*A!C,""3)+?=,job=ECD= B#C* -O("*A;
14') Lust of employees #ith hiredate in the format of BHune 4 1(''B;
)ns< select ename,to3char(h'redate,'*onth dd ....') from tvsemp;
14() print list of employees displayin% BHust salaryB if more than 15&& if e,atly 15&& display Bon
ta%etB if less than 15&& display belo# 15&&;
)ns< select ename,sal,
(
case when sal M 1000 then
'Delow3-ar7et'
when sal=1000 then
'Cn3-ar7et'
when sal % 1000 then
')bove3-ar7et'
else
'55555'
end
)
from tvsemp
15&) =hih Fuery to alulate the len%th of time any employee has been #ith the ompany
)ns< select h'redate,to3char(h'redate,' >><*G<((') B#C* tvsemp
151) <i*en a strin% of the format BnnKnnB . Lerify that the first and last 2 haraters are
numbers .+nd that the middle harater is BKB ?rint the e,pressions B3esB EA *alid B/CB of not
*alid . >se the follo#in% *alues to test your solutionB12K54B)&1K1a)B((K('B;
)ns<
152) -mployes hire on C1 Defore 15th of any month are paid on the last friday of that month
those h'red after 10th are pa'd the last fr'da. of th follow'n7 month 8pr'nt a l'st of emplo.ees 8the'r
h'redate and f'rst pa. date sort those who se salar. conta'ns f'rst
d'7't of the'r deptno;
)ns< select ename,h'redate, !)(-3?), ( ne9t3da.(h'redate,'Br'da.')),
(
case when to3char(h'redate,'dd') M=('10') then
!)(-3?), ( ne9t3da.(h'redate,'Br'da.'))
when to3char(h'redate,'dd')%('10') then
!)(-3?),( ne9t3da.(add3months(h'redate,1),'Br'da.'))
end
)
from tvsemp
153) Display those mana%ers #ho are %ettin% less than his employees salary;
)ns< select a8empno,a8ename ,a8sal,b8sal,b8empno,b8ename from tvsemp a, tvsemp b where
a8m7r=b8empno and a8sal%b8sal
154) ?rint the details of employees #ho are subordinates to DL+2-;
)ns< select a8empno,a8ename ,b8ename from tvsemp a, tvsemp b where a8m7r=b8empno
and b8ename='D!)$"'
**********************
151.Display those #ho #or$in% as mana%er usin% o related sub Fuery
select * from emp where empno 'n (select m7r from emp);
152.Display those employees #hose mana%er name is HC/-S and also #ith his mana%er name
select * from emp where m7r=(select empno from emp where ename='EC+"(') un'on select * from
emp where empno =
(select m7r from emp where ename='EC+"(');
153.Define *ariable representin% the e,pressions used to alulate on employees total annual
renumaration
def'ne emp3ann3sal=(sal+nvl(comm,0))*812;
154.>se the *ariable in a statement #hih finds all employees #ho an earn 3&&&& a year or
more
select * from emp where Qemp3ann3sal%&0000;
155.Aind out ho# many mana%ers are there #ith out listin% them
select count(*) from emp where empno 'n (select m7r from emp);
15!.Aind out the a*% sal and a*% total remuneration for eah job type remember salesman earn
ommission
select job,av7(sal+nvl(comm,0)),sum(sal+nvl(comm,0)) from emp 7roup b. job;
15".0he$ #hether all employees number are indeed uniFue
select count(empno) ,count(d'st'nct(empno)) from emp hav'n7
count(empno)=(count(d'st'nct(empno));
15'.List out the lo#est paid employees #or$in% for eah mana%er) e,lude any %roups #here
minsal is less than
1&&& sort the output by sal
select e8ename,e8m7r,e8sal from emp e where sal 'n (select m'n(sal) from emp where m7r=e8m7r) and
e8sal%1000 order b. sal;
15(.List ename)job)annual sal)depno)dname and %rade #ho earn 3&&&& per year and #ho are
not ler$s
select e8ename,e8job,(e8sal+nvl(e8comm,0))*12,e8deptno,d8dname,s87rade from emp e,sal7rade s,dept
d
where e8sal between s8losal and s8h'sal and e8deptno=d8deptno and (e8sal+nvl(comm,0))*12 % &0000
and e8jobM%'!"#$';
1!&.Aind out th job that #as falled in the first half of 1('3 and the same job that #as falled
durin% the
same period on 1('4
1!1.Aind out the all employees #ho joined the ompany before their mana%er
select * from emp e where h'redate M(select h'redate from emp where empno=e8m7r);
1!2.List out the all employees by name and number alon% #ith their mana%erBs name and
number also display
B/C .+/+<-1B #ho has no mana%er
select e8empno,e8ename,m8empno *ana7er,m8ename *ana7er+ame from emp e,emp m where
e8m7r=m8empno
un'on
select empno,ename,m7r,'+C *ana7er' from emp where m7r 's null;
1!3.Aind out the employees #ho earned the hi%hest sal in eah job typed sort in desendin% sal
order
select * from emp e where sal=(select ma9(sal) from emp where job=e8job);
1!4.Aind out the employees #ho earned the min sal for their job in asendin% order
select * from emp e where sal=(select m'n(sal) from emp where job=e8job) order b. sal;
1!5.Aind out the most reently hired employees in eah dept order by hire date
select * from emp order b. deptno,h'redate desc;
1!!.Display ename)sal and deptno for eah employee #ho earn a sal %reater than the a*% of
their department
order by deptno
select ename,sal,deptno from emp e where sal%(select av7(sal) from emp where deptno=e8deptno)
order b. deptno;
1!".Display the department #here there are no employees
select deptno,dname from dept where deptno not 'n (select d'st'nct(deptno) from emp);
1!'.Display the dept no #ith hi%hest annual remuneration bill as ompensation
select deptno,sum(sal) from emp 7roup b. deptno hav'n7 sum(sal)=(select ma9(sum(sal)) from emp
7roup b. deptno);
1!(.En #hih year did most people join the ompany. Display the year and number of
employees
select count(*),to3char(h'redate,'....') from emp 7roup b. to3char(h'redate,'....');
1"&.Display a*% sal fi%ure for the dept
select deptno,av7(sal) from emp 7roup b. deptno;
1"1.=rite a Fuery of display a%ainst the ro# of the most reently hierd employee.display ename
hire date
and olumn ma, date sho#in%
select empno,h'redate from emp wher h'redate=(select ma9(h'redate) from emp);
1"2.Display employees #ho an earn more than lo#est sal in dept no 3&
select * from emp where sal % (select m'n(sal) from emp where deptno=&0);
1"3.Aind employees #ho an earn more than e*ery employees in dept no 3&
select * from emp where sal%(select ma9(sal) from emp where deptno=&0);
select * from emp where sal%all(select sal from emp where deptno=&0);
1"4.selet dept name and deptno and sum of sal
brea5 on deptno on dname;
select e8deptno,d8dname,sal from emp e,dept d where e8deptno=d8deptno order b. e8deptno;
1"5.Aind out a*% sal and a*% total remainders for eah job type
1J18B'nd all dept's wh'ch have more than & emplo.ees
select deptno from emp 7roup b. deptno hav'n7 count(*)%&;
1"".Ef the pay day is ne,t Ariday after 15th and 3&th of e*ery month. =hat is the ne,t pay day
from
the'r h're date for emplo.ee 'n emp table
1"'.Ef an employee is ta$en by you today in your or%aniMation and is a poliy in your ompany
to ha*e a
re*ie# after ( months the joined date (and of 1st of ne,t month after ( months) ho# many days
from today
your employee has to #ait for a re*ie#
1"(.Display employee name and his sal #hose sal is %reater than hi%hest a*% of deptno
1'&.Display the 1& th reord of emp table (#ithout usin% ro#id)
1'1.Display the half of the enames in upper ase and remainin% lo#er ase
select
concat(upper(substr(ename,0,len7th(ename)@2),lower(substr(ename,len7th(ename)@2+1,len7th(ename
)))) from
emp;
1'2.Display the 1&th reord of emp table #ithout usin% %roup by and ro#id
1'3.Delete the 1&th reord of emp table
1'4.0reate a opy of emp table
create table emp1 as select * from emp;
1'5.selet ename if ename e,ists more than one
select d'st'nct(ename) from emp e where ename 'n (select ename from emp where
e8empnoM%empno);
1'!.Display all enames in re*erse order
select ename from emp order b. ename desc;
1'".Display those employee #hose joinin% of month and %rade is eFual
select empno,ename from emp e,sal7rade s where e8sal between s8losal and s8h'sal and
to3char(h'redate,
'mm')=7rade;
1''.Display those employee #hose joinin% date is a*ailable in deptno
select * from emp where to3char(h'redate,'dd') =deptno;
1'(.Display those employee name as follo#s + +LL-/) D DL+2-
select substr(ename,1,1)II''IIename from emp;
1(&.List out the employees ename)sal)pf from emp
select ename,sal,sal*10@100 pf from emp;
1(1.Display 1S?S from emp #ithout usin% updatin%)insertin%
1(2.0reate table emp #ith only one olumn empno
create table emp (empno number(0));
1(3.+dd this olumn to emp table ename *arhar2(2&)
alter table emp add ename varchar2(20) not null;
1(4.CC?SE i for%et to %i*e the primary $ey onstraint. +dd it no#
alter table emp add constra'nt emp3empno pr'mar. 5e. (empno);
1(5./o# inrease the len%th of ename olumn to 3& haraters
alter table emp mod'f. ename varchar2(&0);
1(!.+dd salary olumn to emp table
alter table emp add sal number(J,2);
1(".E #ant to %i*e a *alidation sayin% that sal an not be %reater 1&&&&(note %i*e a name to this
olumn)
alter table emp add constra'nt emp3sal3chec5 chec5(salM10000);
1('.Aor the time bein% i ha*e deided that i #ill not impose this *alidation. .y boss has a%reed
to pay
more than 1&&&&
alter table emp d'sable constra'nt emp3sal3chec5;
1((..y boss has han%ed his mind. /o# he doesnBt #ant to pay more than 1&&&& So re*o$e
that salary onstraint
alter table emp enable constra'nt emp3sal3chec5;
2&&.+dd olumn alled as m%r to your emp table
alter table emp add m7r number(0);
2&1.ChN 4his olumn should be related to empno) <i*e a ommand tdo add this onstraint
)lter table emp add constra'nt emp3m7r fore'7n 5e. (empno);
2&2.+dd dept no olumn to your emp table
alter table emp add deptno number(&);
2&3.4his deptno olumn should be related to deptno olumn of dept table
alter table emp1 add constra'nt emp13deptno fore'7n 5e. (deptno) references dept(deptno);
2&4.0reate table alled as ne# emp. >sin% sin%le ommand reate this table as #ell as to %et
data into
this table (use reate table as)
create table newemp as select * from emp;
2&5.0reate table alled as ne#emp. 4his table should ontain only empno)ename)dname
create table newemp as select empno,ename,dname from emp e,dept d where e8deptno=d8deptno;
2&!.Delete the ro#s of employees #ho are #or$in% in the ompany for more than 2 years
delete from emp where floor(s.sdate/h'redate)%2*&10;
2&".?ro*ides a ommission to employees #ho are not earnin% any ommission
select emp set comm=&00 where comm 's null;
2&'.Ef any employee has ommission his ommission should be inremented by 1&&8 of his
salary
update emp set comm=comm*10@100 where comm 's not null;
2&(.Display employee name and department name for eah employee
select ename,dname from emp e,dept d where e8deptno=d8deptno;
21&.Display employee number)name and loation of the department in #hih he is #or$in%
select empno,ename,loc from emp e,dept d where e8detpno=d8deptno;
211.Display ename)dname e*en if there no employees #or$in% in a partiular department(use
outer join)
select ename,dname from emp e,dept d where e8deptno(+)=d8deptno;
212.Display employee name and his mana%er name.
select e8ename,m8ename from emp e,emp m where e8m7r=m8empno;
213.Display the department name alon% #ith total salary in eah department
select deptno,sum(sal) from emp 7roup b. deptno;
214.Display the department name and total number of employees in eah department
select deptno,count(*) from emp 7roup b. deptno;
SQL Queries(2)
Q@1) Display the name of employees alon% #ith their annual salary(sal612).
the name of the employee earnin% hi%hest annual salary should appear first;
+ns@ select ename,sal,sal*12 =)nnual (alar.= from emp order b. =)nnual (alar.= desc;
Q@2)Display name)salary)7ra)pf)da)4otalSalary for eah employee.
4he out put should be in the order of total salary )hra 158 of salary )
D+ 1&8 of salary .pf 58 salary 4otal Salary #ill be (salary9hra9da):pf;
+ns@ select ename,sal (),sal*0810 >#),sal*0810 ?),sal*0@100 AB,
sal+(sal*0810)+(sal*0810)/(sal*800) -C-)!()!)#, from emp C#?"# D, -C-)!()!)#,
?"(;
Q@3) Display Department numbers and total number of employees #or$in% in eah
Department;
+ns@ select deptno,count(*) from emp 7roup b. deptno;
Q@4) Display the *arious jobs and total number of employees #or$in% in eah job %roup;
+ns@ select job,count(*) from emp 7roup b. job;
Q@5) Display department numbers and 4otal Salary for eah Department;
+ns@ select deptno,sum(sal) from emp 7roup b. deptno;
Q@!) Display department numbers and .a,imum Salary from eah Department;
+ns@ select deptno,ma9(sal) from emp 7roup b. deptno;
Q@") Display *arious jobs and 4otal Salary for eah job;
+ns@ select job,sum(sal) from emp 7roup b. job;
Q@') Display eah job alon% #ith min of salary bein% paid in eah job %roup;
+ns@ select job ,m'n(sal) from emp 7roup b. job;
Q@() Display the department /umber #ith more than three employees in eah department;
+ns@ select deptno ,count(*) from emp 7roup b. deptno hav'n7 count(*)%&;
Q@1&) Display *arious jobs alon% #ith total salary for eah of the job
#here total salary is %reater than 4&&&&;
+ns@ select job,sum(sal) from emp 7roup b. job hav'n7 sum(sal)%40000;
Q@11) Display the *arious jobs alon% #ith total number of employees in eah job.4he output
should ontain only those jobs #ith more than three employees;
+ns@ select job,count(*) from emp 7roup b. job hav'n7 count(*)%&;
Q@12) Display the name of employee #ho earn 7i%hest Salary;
+ns@ select ename, sal from emp where sal%=(select ma9(sal) from emp );
Q@13) Display the employee /umber and name for employee #or$in% as ler$ and earnin%
hi%hest salary amon% the ler$s;
+ns@ select ename,empno from emp where sal=(select ma9(sal) from emp where
job='!"#$') and job='!"#$' ;
Q@14) Display the names of salesman #ho earns a salary more than the 7i%hest Salary of the
0ler$;
+ns@ select ename,sal from emp where sal%(select ma9(sal) from emp
where job='!"#$') )+? job='()!"(*)+';
Q@15) Display the names of ler$s #ho earn a salary more than the lo#est Salary of any
Salesman;
+ns@ select ename,sal from emp where sal%(select m'n(sal) from emp where job='()!"(*)+') and
job='!"#$';
Q@1!) Display the names of employees #ho earn a salary more than that of jones or that of
salary %reater than that of sott;
+ns@ select ename,sal from emp where sal%all(select sal from emp where
ename='EC+"(' C# ename='(C--');
Q@1") Display the names of employees #ho earn 7i%hest salary in their respeti*e
departments;
+ns@ select ename,sal,deptno from emp where sal 'n (select ma9(sal) from emp 7roup b. deptno);
Q@1') Display the names of employees #ho earn 7i%hest salaries in their respeti*e job
<roups;
+ns@ select ename,job from emp where sal 'n (select ma9(sal) from emp 7roup b. job);
Q@1() Display employee names #ho are #or$in% in +ountin% department;
+ns@ select e8ename,d8dname from emp e,dept d where e8deptno=d8deptno and
d8dname=')CF+-G+:';
Q@2&) Display the employee names #ho are =or$in% in 0hia%o;
+ns@ select e8ename,d8loc from emp e,dept d where e8deptno=d8deptno and d8loc='>G):C';
Q@21) Display the job %roups ha*in% 4otal Salary %reater than the ma,imum salary for
.ana%ers;
+ns@ select job ,sum(sal) from emp 7roup b. job hav'n7 sum(sal) %(select ma9(sal) from emp where
job='*)+):"#');
Q@22) Display the names of employees from department number 1& #ith salary %reater than
that of +/3 employee #or$in% in other departments;
+ns@ select ename,deptno from emp where sal%an.(select m'n(sal) from emp where deptnoH=10
7roup b. deptno) and deptno=10 ;
Q@23) Display the names of employees from department number 1& #ith salary %reater than
that of +LL employee #or$in% in other departments;
+ns@ select ename,deptno from emp where sal%all(select ma9(sal) from emp where deptnoH=10
7roup b. deptno) and deptno=10 ;
Q@24) Display the names of employees in >pper 0ase;
+ns@ select upper(ename) from emp;
Q@25) Display the names of employees in Lo#er 0ase;
+ns@ select !ower(ename) from emp;
Q@2!) Display the names of employees in ?roper ase;
+ns@ select Gn'tap(ename)from emp;
Q@2") Aind the len%th of your name usin% +ppropriate Auntion;
+ns@ select lent7h('(#G+GO)()#)C') from dual;
Q@2') Display the len%th of all the employee names;
+ns@ select len7th(ename) from emp;
Q@2() Display the name of employee 0onatinate #ith -mployee /umber;
+ns@ select enameII' 'IIempno from emp;
Q@3&) >se appropriate funtion and e,trat 3 haraters startin% from 2 haraters from the
follo#in% strin% BCraleB i.e.) the out put should be a;
+ns@ select substr('Cracle',&,2) from dual;
Q@31) Aind the first ourane of harater a from the follo#in% strin% 0omputer .aintenane
0orporation;
+ns@ select lstr('omputer *a'ntenance orporat'on','a' ) from dual;
Q@32) 1eplae e*ery ourane of alphabet + #ith D in the strin% .+lliens (>se 4ranslate
funtion);
+ns@ select translate(')ll'ens',')','D') from ?ual;
Q@33) Display the information from the employee table . #here e*er job .ana%er is found it
should be displayed as Doss;
+ns@ select ename ,replace(job,'*)+):"#','DC((') from emp;
Q@34) Display empno)ename)deptno from emp table. Enstead of display department numbers
display the related department name(>se deode funtion);
+ns@ select empno,ename,deptno,?ecode(deptno,10,')CF+-G+:'
,20,'#"(")#>',&0,'()!"(','CA"#)-GC+(')?+ame from emp;
Q@35) Display your +%e in Days;
+ns@ select s.sdate/to3date('&0/jul/12JJ') from dual;
Q@3!) Display your +%e in .onths;
+ns@ select months3between(s.sdate,to3date('&0/jul/12JJ')) from dual;
Q@3") Display urrent date as 15th +u%ust Ariday /ineteen /ienty Se*en;
+ns@ select -o3char(s.sdate,'ddth *onth ?a. .ear') from dual;
Q@3') Display the follo#in% output for eah ro# from emp table;
+ns@ K<&2
Q@3() Sott has joined the ompany on 13th +u%ust ninteen ninety;
+ns@ select empno,ename,to3char(>'redate,'?a. ddth *onth .ear') from emp;
Q@4&) Aind the nearest Saturday after 0urrent date;
+ns@ select ne9t3da.(s.sdate,'(aturda.') from dual;
Q@41) Display the urrent time;
+ns@ select -o3har(s.sdate,'>><*G<((') from dual;
Q@42) Display the date three months before the 0urrent date;
+ns@ select )dd3months(s.sdate,/&) from dual;
Q@43) Display the ommon jobs from department number 1& and 2&;
+ns@ select job from emp where job 'n (select job from emp where deptno=20) and deptno=10;
Q@44) Display the jobs found in department 1& and 2& -liminate dupliate jobs;
+ns@ select ?'st'nct job from emp where deptno 'n(10,20);
Q@45) Display the jobs #hih are uniFue to department 1&;
+ns@ select job from emp where deptno=10;
Q@4!) Display the details of those employees #ho do not ha*e any person #or$in% under him;
+ns@ select empno,ename,job from emp where empno not 'n (select m7r from emp where m7r 's not
null );
Q@4")Display the details of those employees #ho are in sales department and %rade is 3;
+ns@ select e8ename,d8dname,7rade from emp e,dept d ,sal7rade where e8deptno=d8deptno and
dname='()!"(' and 7rade=&;
Q@4') Display those #ho are not mana%ers;
+ns@ select ename from emp where jobH='*)+):"#';
Q@4() Display those employees #hose name ontains not less than 4 haraters;
+ns@ select ename from emp where len7th(ename)%=4;
Q@5&) Display those department #hose name start #ithGSG #hile loation name ends #ith
G2G;
+ns@ select e8ename,d8loc from emp e ,dept d where d8loc l'5e('6$') and ename l'5e('(6');
Q@51) Display those employees #hose mana%er name is Hones;
+ns@ select e8ename (uper'or,e18ename (ubord'nate from emp e,e1 where e8empno=e18m7r and
e8ename='EC+"(';
Q@52) Display those employees #hose salary is more than 3&&& after %i*in% 2&8 inrement;
+ns@ select ename,sal,(sal+(sal*0820)) from emp where (sal+(sal*0820))%&000;
Q@53) Display all employees #ith their department names;
+ns@ select e8ename,d8dname from emp e, dept d where e8deptno=d8deptno;
Q@54) Display ename #ho are #or$in% in sales department;
+ns@ select e8ename,d8dname from emp e,dept d where e8deptno=d8deptno and d8dname='()!"(';
Q@5!) Display employee name)dept name)salary)and ommission for those sal in bet#een 2&&&
to 5&&& #hile loation is 0hia%o;
+ns@ (elect e8ename,d8dname,e8sal,e8comm from emp e,dept d where e8deptno=d8deptno and sal
between 2000 and 0000;
Q@5") Display those employees #hose salary is %reater than his mana%ers salary;
+ns@ (elect e8ename,e8sal,e18ename,e18sal from emp e,e1 where e8m7r=e18empno and e8sal%e18sal;
Q@5') Display those employees #ho are #or$in% in the same dept #here his mana%er is #or$;
+ns@ select e8ename,e8deptno,e18ename,e18deptno from emp e,e1 where e8m7r=e18empno and
e8deptno=e18deptno;
Q@5() Display those employees #ho are not #or$in% under any .ana%er;
+ns@ select ename from emp where m7r 's null;
Q@!&) Display the %rade and employees name for the deptno 1& or 3& but %rade is not 4 #hile
joined the ompany before 31:D-0:'2;
+ns@ select ename,7rade,deptno,sal from emp ,sal7rade where ( 7rade,sal) 'n
( select 7rade,sal from sal7rade,emp where sal between losal and h'sal)
and 7radeH=4 and deptno 'n (10,&0) and h'redateM'&1/?ec/L2';
Q@!1) >pdate the salary of eah employee by 1&8 inrement #ho are not eli%ible for
ommission;
+ns@ update emp set sal= (sal+(sal*0810)) where comm 's null;
Q@!2) Delete those employees #ho joined the ompany before 31:De:'2 #hile their department
Loation is /e# 3or$ or 0hia%o;
+ns@ select e8ename,e8h'redate,d8loc from emp e,dept d where
e8deptno=d8deptno and h'redateM'&1/?ec/L2' and d8loc 'n('+"N ,C#$','>G):C');
Q@!3) Display employee name )job)deptname)lo for all #ho are #or$in% as mana%er;
+ns@ select e8ename,e8job,d8dname,d8loc from emp e,dept d where e8deptno=d8deptno
and e8empno 'n (select m7r from emp where m7r 's not null);
Q@!4) Display those employees #hose mana%er name is jones and also display their mana%er
name;
+ns@ select e8ename sub,e18ename from emp e,e1 where e8m7r=e18empno and e18ename='EC+"(';
Q@!5) Display name and salary of ford if his salary is eFual to hisal of his %rade;
+ns@ select ename,7rade,h'sal,sal from emp,sal7rade where ename='BC#?' and sal=h'sal;
C#
select 7rade,sal,h'sal from emp,sal7rade where ename='BC#?' and sal between losal and h'sal;
C#
select ename,sal,h'sal,7rade from emp,sal7rade where ename='BC#?'
and (7rade,sal) 'n (select 7rade,h'sal from sal7rade,emp where
sal between losal and h'sal);
Q!!) Display employee name )job)deptname)his mana%er name )his %rade and ma$e an under
department #ise;
+ns@ select e8ename sub,e18ename sup,e8job,d8dname ,7rade from emp e,e1,sal7rade,dept d where
e8m7r=e18empno and e8sal between losal and h'sal and e8deptno=d8deptno 7roup b.
d8deptno,e8ename,e18ename,e8job,d8dname,7rade;
C#
select e8ename sub,e18ename sup,e8job,d8dname ,7rade from emp e,e1,sal7rade,tvsdept d where
e8m7r=e18empno and e8sal between losal and h'sal and e8deptno=d8deptno;
Q@!") List out all the employee names )job)salary)%rade and deptname for e*ery one in a
ompany e,ept B0L-12B . Sort on salary display the hi%hest salary;
+ns@ select e8ename ,e8job,e8sal,d8dname ,7rade from emp e,sal7rade,dept d where
(e8deptno=d8deptno and e8sal between losal and h'sal ) order b. e8sal desc;
Q@!') Display employee name)job abd his mana%er .Display also employees #ho are #ith out
mana%ers;
+ns@ select e8ename ,e18ename,e8job,e8sal,d8dname from emp e,e1,dept d where e8m7r=e18empno(+)
and e8deptno=d8deptno;
Q@!() Display 4op 5 employee of a 0ompany;
+ns@
Q@"&) Display the names of those employees #ho are %ettin% the hi%hest salary;
+ns@ select ename,sal from emp where sal 'n (select ma9(sal) from emp);
Q@"1) Display those employees #hose salary is eFual to a*era%e of ma,imum and minimum;
+ns@ select * from emp
where sal=(select (ma9(sal)+m'n(sal))@2 from emp);
Q@"2) Selet ount of employees in eah department #here ount I3;
+ns@ select count(*) from emp 7roup b. deptno hav'n7 count(*)%&
Q@"3) Display dname #here atleast three are #or$in% and display only deptname;
+ns@ select d8dname from dept d, emp e where e8deptno=d8deptno 7roup b. d8dname hav'n7
count(*)%&;
Q@"4) Display name of those mana%ers name #hose salary is more than a*era%e salary of
0ompany;
+ns@ select d'st'nct e18ename,e18sal from emp e,e1,dept d where e8deptno=d8deptno and
e8m7r=e18empno and e18sal% (select av7(sal) from emp);
Q@"5) Display those mana%ers name #hose salary is more than a*era%e salary salary of his
employees;
+ns@ select d'st'nct e18ename,e18sal from emp e,e1,dept d where e8deptno=d8deptno and
e8m7r=e18empno and e18sal%an. (select av7(sal) from emp 7roup b. deptno);
Q@"!) Display employee name)sal)omm and netpay for those employees #hose netpay is
%reater than or eFual to any other employee salary of the ompany;
+ns@ select ename,sal,+O!(comm,0),sal++O!(comm,0) from emp where
sal++O!(comm,0) %an. (select e8sal from emp e );
Q@"") Display those employees #hose salary is less than his mana%er but more than salary of
other mana%ers;
+ns@ select e8ename sub,e8sal from emp e,e1,dept d where
e8deptno=d8deptno and e8m7r=e18empno
and e8salMe18sal
and e8sal %an. (select e28sal from emp e2, e,dept d1 where
e8m7r=e28empno and d18deptno=e8deptno);
Q@"') Display all employees names #ith total sal of ompany #ith eah employee name;
+ns@
Q@"() Aind the last 5(least) employees of ompany;
+ns@
Q@'&) Aind out the number of employees #hose salary is %reater than their
mana%ers salary;
+ns@ select e8ename,e8sal,e18ename,e18sal from emp e,e1,dept d where e8deptno=d8deptno and
e8m7r=e18empno and e8sal%e18sal;
Q@'1) Display the mana%er #ho are not #or$in% under president but they are #or$in% under
any other mana%er;
+ns@ select e28ename from emp e1,emp e2,emp e& where e18m7r=e28empno and e28m7r=e&8empno
and e&8jobH='A#"(G?"+-';
Q@'2) Delete those department #here no employee #or$in%;
+ns@ delete from emp where empno 's null;
Q@'3) Delete those reords from emp table #hose deptno not a*ailable in dept table;
+ns@ delete from emp e where e8deptno not 'n (select deptno from dept);
Q@'4) Display those enames #hose salary is out of %rade a*ailable in sal%rade table;
+ns@ select empno,sal from emp where salM(select m'n(!C()!) from sal7rade )
C# sal%(select ma9(h'sal) from sal7rade);
Q@'5) Display employee name)sal)omm and #hose netpay is %reater than any other in the
ompany;
+ns@ select ename,sal,comm,sal+comm from emp where sal+comm%an.
(select sal+comm from emp );
Q@'!) Display name of those employees #ho are %oin% to retire 31:De:(( if ma,imum job
period is 3& years;
+ns@ select empno, h'redate,s.sdate, to3char(s.sdate,'....') / to3char(h'redate,'....')
from emp where to3char(s.sdate,'....') / to3char(h'redate,'....')=&0;
Q@'") Display those employees #hose salary is odd *alue;
+ns@ select ename ,sal from emp where mod(sal,2)H=0;
Q@'') Display those employees #hose salary ontains atleast 3 di%its;
+ns@ select ename,sal from emp where len7th(sal)=&;
Q@'() Display those employees #ho joined in the ompany in the month of De;
+ns@ (elect empno,ename from emp where tr'm(to3char(h'redate,'*on'))=tr'm('?"');
Q@(&) Display those employees #hose name ontains +;
+ns@ select ename from emp where ename l'5e('6)6');
Q@(1) Display those employees #hose deptno is a*ailable in salary;
+ns@ select ename,sal from emp where deptno 'n (select d'st'nct sal from emp);
Q@(2) Display those employees #hose first 2 haraters from hiredate : last 2 haraters sal;
+ns@ select empno,h'redate,sal from emp where tr'm(substr(h'redate,1,2))=tr'm(substr(sal,/2,2));
or
select h'redate,sal from emp where to3har(h'redate,'dd')=tr'm(substr(sal,/2,2));
Q@(3) Display those employeess #hose 1&8 of salary is eFual to the year joinin%;
+ns@ select ename ,sal,0810*sal from emp where 0810*sal=tr'm(to3char(h'redate,'..'));
Q@(4) Display those employees #ho are #or$in% in sales or researh;
+ns@ select e8ename from emp e ,dept d where e8deptno=d8deptno and d8dname
'n('()!"(','#"(")#>');
Q@(5) Display the %rade of jones;
+ns@ select ename,7rade from emp,sal7rade where ( 7rade,sal) =
(select 7rade,sal from sal7rade,emp where sal between losal and h'sal and ename='EC+"(');
Q@(!) Display those employees #ho joined the ompany before 15th of the month;
+ns@ select ename ,h'redate from emp where h'redateM'10/Eul/02' and h'redate %='01/jul/02';
Q@(") Display those employees #ho has joined before 15th of the month;
+ns@ select ename ,h'redate from emp where h'redateM'10/Eul/02'
Q@(') Delete those reords #here no of employees in partiular department is less than 3;
+ns@ delete from emp where deptno 'n (select deptno from emp 7roup b. deptno hav'n7 count(*)
M& ;
Q@((+) Delete those employee#ho joined the ompany 1& years ba$ from today;
+ns@ delete from emp where empno 'n (select empno from emp
where to3char(s.sdate,'....')/ to3char(h'redate,'....')%=10);
Q@((D) Display the deptname the number of haraters of #hih is eFual to no of employee in
any other department;
+ns@
Q@1&&) Display the deptname #here no employee is #or$in%;
+ns@ select deptno from emp where empno 's null;
Q@1&1) Display those employees #ho are #or$in% as mana%er;
+ns@ select e28ename from emp e1,e2 where e18m7r=e28empno and e28empno 's not null;
Q@1&2) 0ount th number of employees #ho are #or$in% as mana%ers (>sin% set opetrator);
+ns@ select d8dname from dept d where len7th(d8dname) 'n (select count(*) from emp e where
e8deptnoH=d8deptno 7roup b. e8deptno);
Q@1&3) Display the name of the dept those employees #ho joined the ompany on the same
date;
+ns@ select a8ename,b8ename from emp a,emp b where a8h'redate=b8h'redate and a8empnoH=b8empno;
Q@1&4) Display those employees #hose %rade is eFual to any number of sal but not eFual to first
number of sal;
+ns@ select ename,sal,7rade ,substr(sal,7rade,1) from emp,sal7rade where
7radeH=substr(sal,1,1) and 7rade = substr(sal,7rade,1)
and sal between losal and h'sal;
Q@1&5) 0ount the no of employees #or$in% as mana%er usin% set operation;
+ns@ (elect count(empno) from emp where
empno 'n (select a8empno from emp a
'ntersect
select b8m7r from emp b);
Q@1&!) Display the name of employees #ho joined the ompany on the same date;
+ns@ select a8ename,b8ename from emp a,emp b where a8h'redate=b8h'redate and a8empnoH=b8empno;
Q@1&") Display the mana%er #ho is ha*in% ma,imum number of employees #or$in% under
him;
+ns@ select e28ename,count(*) from emp e1,e2 where e18m7r=e28empno 7roup b. e28ename >av'n7
count(*)=(select ma9(count(*)) from emp e1,e2 where e18m7r=e28empno 7roup b. e28ename);
Q@1&') List out the employee name and salary inreased by 158 and e,press as #hole number
of Dollars;
+ns@ select ename,sal,lpad(translate(sal,sal,((sal +(sal*0810))@00)),0,'P') from emp;
Q@1&() ?rodue the output of the emptable G-.?LC3--J+/D HCDG for ename and job ;
+ns@ select ename="*A!C,""3)+?=,job=ECD= B#C* "*A;
Q@11&) Lust of employees #ith hiredate in the format of BHune 4 1(''B;
+ns@ select ename,to3char(h'redate,'*onth dd ....') from emp;
Q@111) print list of employees displayin% BHust salaryB if more than 15&& if e,atly 15&& display
Bon ta%etB if less than 15&& display belo# 15&&;
+ns@ select ename,sal,
(
case when sal M 1000 then
'Delow3-ar7et'
when sal=1000 then
'Cn3-ar7et'
when sal % 1000 then
')bove3-ar7et'
else
'55555'
end
)
from emp;
Q@112) =hih Fuery to alulate the len%th of time any employee has been #ith the ompany
+ns@ select h'redate,to3char(h'redate,' >><*G<((') B#C* emp;
Q@113) <i*en a strin% of the format BnnKnnB . Lerify that the first and last 2 haraters are
numbers .+nd that the middle harater is BKB ?rint the e,pressions B3esB EA *alid B/CB of not
*alid . >se the follo#in% *alues to test your solutionB12K54B)&1K1a)B((K('B;
+ns@
Q@114) -mployes hire on C1 Defore 15th of any month are paid on the last friday of that
month those hired after 15th are paid the last friday of th follo#in% month .print a list of
employees .their hiredate and first pay date sort those #ho se salary ontains first di%it of their
deptno;
+ns@ select ename,h'redate, !)(-3?), ( ne9t3da.(h'redate,'Br'da.')),
(
case when to3char(h'redate,'dd') M=('10') then
!)(-3?), ( ne9t3da.(h'redate,'Br'da.'))
when to3char(h'redate,'dd')%('10') then
!)(-3?),( ne9t3da.(add3months(h'redate,1),'Br'da.'))
end
)
from emp;
Q@115) Display those mana%ers #ho are %ettin% less than his employees salary;
+ns@ select a8empno,a8ename ,a8sal,b8sal,b8empno,b8ename from emp a, emp b where a8m7r=b8empno
and a8sal%b8sal;
Q@11!) ?rint the details of employees #ho are subordinates to DL+2-;
+ns@ select a8empno,a8ename ,b8ename from emp a, emp b where a8m7r=b8empno
and b8ename='D!)$"';
SQL Queries(3)
Q: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?
Ans: select ename,sal,sal*12 "Annual Salary" from emp order by "Annual Salary" desc;
Q:2)Display name,salary,Hra,pf,da,otal!alary for ea"h employee. he out put should #e in the
order of total salary ,hra 1$% of salary , D& 1'% of salary .pf $% salary otal !alary will #e
(salary(hra(da))pf?
Ans: select ename,sal SA,sal*0.15 HRA,sal*0.10 A,sal*5!100 "#,sal$%sal*0.15&$%sal*0.10&'%sal*.05&
()(A*SA*AR+ from emp )R,R -+ ()(A*SA*AR+ ,S.;
Q:*) Display Department num#ers and total num#er of employees wor+ing in ea"h Department?
Ans: select deptno,count%*& from emp /roup by deptno;
Q:,) Display the -arious .o#s and total num#er of employees wor+ing in ea"h .o# group?
Ans: select 0ob,count%*& from emp /roup by 0ob;
Q:$) Display department num#ers and otal !alary for ea"h Department?
Ans: select deptno,sum%sal& from emp /roup by deptno;
Q:/) Display department num#ers and 0a1imum !alary from ea"h Department?
Ans: select deptno,ma1%sal& from emp /roup by deptno;
Q:2) Display -arious .o#s and otal !alary for ea"h .o#?
Ans: select 0ob,sum%sal& from emp /roup by 0ob;
Q:3) Display ea"h .o# along with min of salary #eing paid in ea"h .o# group?
Q:4) Display the department 5um#er with more than three employees in ea"h department?
Ans: select deptno ,count%*& from emp /roup by deptno 2a34n/ count%*&56;
Q:1') Display -arious .o#s along with total salary for ea"h of the .o#
where total salary is greater than ,''''?
Ans: select 0ob,sum%sal& from emp /roup by 0ob 2a34n/ sum%sal&570000;
Q:11) Display the -arious .o#s along with total num#er of employees in ea"h .o#.he output
should "ontain only those .o#s with more than three employees?
Ans: select 0ob,count%*& from emp /roup by 0ob 2a34n/ count%*&56;
Q:12) Display the name of employee who earn Highest !alary?
Ans: select ename, sal from emp 82ere sal59%select ma1%sal& from emp &;
Q:1*) Display the employee 5um#er and name for employee wor+ing as "ler+ and earning highest
salary among the "ler+s?
Ans: select ename,empno from emp 82ere sal9%select ma1%sal& from emp 82ere
0ob9:.*,R;:& and 0ob9:.*,R;: ;
Q:1,) Display the names of salesman who earns a salary more than the Highest !alary of the
6ler+?
Ans: select ename,sal from emp 82ere sal5%select ma1%sal& from emp
82ere 0ob9:.*,R;:& A< 0ob9:SA*,S=A<:;
Q:1$) Display the names of "ler+s who earn a salary more than the lowest !alary of any
!alesman?
Ans: select ename,sal from emp 82ere sal5%select m4n%sal& from emp 82ere 0ob9:SA*,S=A<:& and
0ob9:.*,R;:;
Q:1/) Display the names of employees who earn a salary more than that of .ones or that of salary
greater than that of s"ott?
Ans: select ename,sal from emp 82ere sal5all%select sal from emp 82ere
ename9:>)<,S: )R ename9:S.)((:&;
Q:12) Display the names of employees who earn Highest salary in their respe"ti-e departments?
Ans: select ename,sal,deptno from emp 82ere sal 4n %select ma1%sal& from emp /roup by deptno&;
Q:13) Display the names of employees who earn Highest salaries in their respe"ti-e .o# 7roups?
Ans: select ename,0ob from emp 82ere sal 4n %select ma1%sal& from emp /roup by 0ob&;
Q:14) Display employee names who are wor+ing in &""ounting department?
Ans: select e.ename,d.dname from emp e,dept d 82ere e.deptno9d.deptno and
d.dname9:A..)?<(@<A:;
Q:2') Display the employee names who are 8or+ing in 6hi"ago?
Ans: select e.ename,d.loc from emp e,dept d 82ere e.deptno9d.deptno and d.loc9:.H@.AA):;
Q:21) Display the .o# groups ha-ing otal !alary greater than the ma1imum salary for 0anagers?
Ans: select 0ob ,sum%sal& from emp /roup by 0ob 2a34n/ sum%sal& 5%select ma1%sal& from emp 82ere
0ob9:=A<AA,R:&;
Q:22) Display the names of employees from department num#er 1' with salary greater than that of
&59 employee wor+ing in other departments?
Ans: select ename,deptno from emp 82ere sal5any%select m4n%sal& from emp 82ere deptnoB910 /roup by
deptno& and deptno910 ;
Q:2*) Display the names of employees from department num#er 1' with salary greater than that of
&:: employee wor+ing in other departments?
Ans: select ename,deptno from emp 82ere sal5all%select ma1%sal& from emp 82ere deptnoB910 /roup by
deptno& and deptno910 ;
Q:2,) Display the names of employees in ;pper 6ase?
Ans: select upper%ename& from emp;
Q:2$) Display the names of employees in :ower 6ase?
Ans: select *o8er%ename& from emp;
Q:2/) Display the names of employees in <roper "ase?
Ans: select @n4t.ap%ename&from emp;
Q:22) =ind the length of your name using &ppropriate =un"tion?
Ans: select lent/2%:SR@<@CASARA):& from dual;
Q:23) Display the length of all the employee names?
Ans: select len/t2%ename& from emp;
Q:24) Display the name of employee 6on"atinate with >mployee 5um#er?
Ans: select enameDD: :DDempno from emp;
Q:*') ;se appropriate fun"tion and e1tra"t * "hara"ters starting from 2 "hara"ters from the
following string ?@ra"le? i.e., the out put should #e a"?
Ans: select substr%:)racle:,6,2& from dual;
Q:*1) =ind the first o""uran"e of "hara"ter a from the following string 6omputer 0aintenan"e
6orporation?
Ans: select lstr%:.omputer =a4ntenance .orporat4on:,:a: & from dual;
Q:*2) Aepla"e e-ery o""uran"e of alpha#et & with B in the string .&lliens (;se ranslate fun"tion)?
Ans: select translate%:All4ens:,:A:,:-:& from ual;
Q:**) Display the information from the employee ta#le . where e-er .o# 0anager is found it should
#e displayed as Boss?
Ans: select ename ,replace%0ob,:=A<AA,R:,:-)SS:& from emp;
Q:*,) Display empno,ename,deptno from emp ta#le. Cnstead of display department num#ers
display the related department name(;se de"ode fun"tion)?
Ans: select empno,ename,deptno,ecode%deptno,10,:A..)?<(@<A:
,20,:R,S,AR.H:,60,:SA*,S:,:)",RA(@)<S:&<ame from emp;
Q:*$) Display your &ge in Days?
Ans: select sysdate'toEdate%:60'0ul'1FGG:& from dual;
Q:*/) Display your &ge in 0onths?
Ans: select mont2sEbet8een%sysdate,toEdate%:60'0ul'1FGG:&& from dual;
Q:*2) Display "urrent date as 1$th &ugust =riday 5ineteen 5ienty !e-en?
Ans: select (oEc2ar%sysdate,:ddt2 =ont2 ay year:& from dual;
Q:*3) Display the following output for ea"h row from emp ta#le?
Ans: H:6F
Q:*4) !"ott has .oined the "ompany on 1*th &ugust ninteen ninety?
Ans: select empno,ename,toEc2ar%H4redate,:ay ddt2 =ont2 year:& from emp;
Q:,') =ind the nearest !aturday after 6urrent date?
Ans: select ne1tEday%sysdate,:Saturday:& from dual;
Q:,1) Display the "urrent time?
Ans: select (oE.2ar%sysdate,:HH:=@:SS:& from dual;
Q:,2) Display the date three months #efore the 6urrent date?
Ans: select AddEmont2s%sysdate,'6& from dual;
Q:,*) Display the "ommon .o#s from department num#er 1' and 2'?
Ans: select 0ob from emp 82ere 0ob 4n %select 0ob from emp 82ere deptno920& and deptno910;
Q:,,) Display the .o#s found in department 1' and 2' >liminate dupli"ate .o#s?
Ans: select 4st4nct 0ob from emp 82ere deptno 4n%10,20&;
Q:,$) Display the .o#s whi"h are uniDue to department 1'?
Ans: select 0ob from emp 82ere deptno910;
Q:,/) Display the details of those employees who do not ha-e any person wor+ing under him?
Ans: select empno,ename,0ob from emp 82ere empno not 4n %select m/r from emp 82ere m/r 4s not
null &;
Q:,2)Display the details of those employees who are in sales department and grade is *?
Ans: select e.ename,d.dname,/rade from emp e,dept d ,sal/rade 82ere e.deptno9d.deptno and
dname9:SA*,S: and /rade96;
Q:,3) Display those who are not managers?
Ans: select ename from emp 82ere 0obB9:=A<AA,R:;
Q:,4) Display those employees whose name "ontains not less than , "hara"ters?
Ans: select ename from emp 82ere len/t2%ename&597;
Q:$') Display those department whose name start withE!E while lo"ation name ends with EFE?
Ans: select e.ename,d.loc from emp e ,dept d 82ere d.loc l4Ie%:J;:& and ename l4Ie%:SJ:&;
Q:$1) Display those employees whose manager name is Gones?
Ans: select e.ename Super4or,e1.ename Subord4nate from emp e,e1 82ere e.empno9e1.m/r and
e.ename9:>)<,S:;
Q:$2) Display those employees whose salary is more than *''' after gi-ing 2'% in"rement?
Ans: select ename,sal,%sal$%sal*0.20&& from emp 82ere %sal$%sal*0.20&&56000;
Q:$*) Display all employees with their department names?
Ans: select e.ename,d.dname from emp e, dept d 82ere e.deptno9d.deptno;
Q:$,) Display ename who are wor+ing in sales department?
Ans: select e.ename,d.dname from emp e,dept d 82ere e.deptno9d.deptno and d.dname9:SA*,S:;
Q:$/) Display employee name,dept name,salary,and "ommission for those sal in #etween 2''' to
$''' while lo"ation is 6hi"ago?
Ans: Select e.ename,d.dname,e.sal,e.comm from emp e,dept d 82ere e.deptno9d.deptno and sal
bet8een 2000 and 5000;
Q:$2) Display those employees whose salary is greater than his managers salary?
Ans: Select e.ename,e.sal,e1.ename,e1.sal from emp e,e1 82ere e.m/r9e1.empno and e.sal5e1.sal;
Q:$3) Display those employees who are wor+ing in the same dept where his manager is wor+?
Ans: select e.ename,e.deptno,e1.ename,e1.deptno from emp e,e1 82ere e.m/r9e1.empno and
e.deptno9e1.deptno;
Q:$4) Display those employees who are not wor+ing under any 0anager?
Ans: select ename from emp 82ere m/r 4s null;
Q:/') Display the grade and employees name for the deptno 1' or *' #ut grade is not , while
.oined the "ompany #efore *1)D>6)32?
Ans: select ename,/rade,deptno,sal from emp ,sal/rade 82ere % /rade,sal& 4n
% select /rade,sal from sal/rade,emp 82ere sal bet8een losal and 24sal&
and /radeB97 and deptno 4n %10,60& and 24redateK:61'ec'L2:;
Q:/1) ;pdate the salary of ea"h employee #y 1'% in"rement who are not eligi#le for "ommission?
Ans: update emp set sal9 %sal$%sal*0.10&& 82ere comm 4s null;
Q:/2) Delete those employees who .oined the "ompany #efore *1)De")32 while their department
:o"ation is 5ew 9or+ or 6hi"ago?
Ans: select e.ename,e.24redate,d.loc from emp e,dept d 82ere
e.deptno9d.deptno and 24redateK:61'ec'L2: and d.loc 4n%:<,M +)R;:,:.H@.AA):&;
Q:/*) Display employee name ,.o#,deptname,lo" for all who are wor+ing as manager?
Ans: select e.ename,e.0ob,d.dname,d.loc from emp e,dept d 82ere e.deptno9d.deptno
and e.empno 4n %select m/r from emp 82ere m/r 4s not null&;
Q:/,) Display those employees whose manager name is .ones and also display their manager
name?
Ans: select e.ename sub,e1.ename from emp e,e1 82ere e.m/r9e1.empno and e1.ename9:>)<,S:;
Q:/$) Display name and salary of ford if his salary is eDual to hisal of his grade?
Ans: select ename,/rade,24sal,sal from emp,sal/rade 82ere ename9:#)R: and sal924sal;
)R
select /rade,sal,24sal from emp,sal/rade 82ere ename9:#)R: and sal bet8een losal and 24sal;
)R
select ename,sal,24sal,/rade from emp,sal/rade 82ere ename9:#)R:
and %/rade,sal& 4n %select /rade,24sal from sal/rade,emp 82ere
sal bet8een losal and 24sal&;
Q//) Display employee name ,.o#,deptname,his manager name ,his grade and ma+e an under
department wise?
Ans: select e.ename sub,e1.ename sup,e.0ob,d.dname ,/rade from emp e,e1,sal/rade,dept d 82ere
e.m/r9e1.empno and e.sal bet8een losal and 24sal and e.deptno9d.deptno /roup by
d.deptno,e.ename,e1.ename,e.0ob,d.dname,/rade;
)R
select e.ename sub,e1.ename sup,e.0ob,d.dname ,/rade from emp e,e1,sal/rade,t3sdept d 82ere
e.m/r9e1.empno and e.sal bet8een losal and 24sal and e.deptno9d.deptno;
Q:/2) :ist out all the employee names ,.o#,salary,grade and deptname for e-ery one in a "ompany
e1"ept ?6:>AF? . !ort on salary display the highest salary?
Ans: select e.ename ,e.0ob,e.sal,d.dname ,/rade from emp e,sal/rade,dept d 82ere %e.deptno9d.deptno
and e.sal bet8een losal and 24sal & order by e.sal desc;
Q:/3) Display employee name,.o# a#d his manager .Display also employees who are with out
managers?
Ans: select e.ename ,e1.ename,e.0ob,e.sal,d.dname from emp e,e1,dept d 82ere e.m/r9e1.empno%$&
and e.deptno9d.deptno;
Q:/4) Display op $ employee of a 6ompany?
Ans:
Q:2') Display the names of those employees who are getting the highest salary?
Ans: select ename,sal from emp 82ere sal 4n %select ma1%sal& from emp&;
Q:21) Display those employees whose salary is eDual to a-erage of ma1imum and minimum?
Ans: select * from emp
82ere sal9%select %ma1%sal&$m4n%sal&&!2 from emp&;
Q:22) !ele"t "ount of employees in ea"h department where "ount H*?
Ans: select count%*& from emp /roup by deptno 2a34n/ count%*&56
Q:2*) Display dname where atleast three are wor+ing and display only deptname?
Ans: select d.dname from dept d, emp e 82ere e.deptno9d.deptno /roup by d.dname 2a34n/ count%*&56;
Q:2,) Display name of those managers name whose salary is more than a-erage salary of
6ompany?
Ans: select d4st4nct e1.ename,e1.sal from emp e,e1,dept d 82ere e.deptno9d.deptno and
e.m/r9e1.empno and e1.sal5 %select a3/%sal& from emp&;
Q:2$) Display those managers name whose salary is more than a-erage salary salary of his
employees?
Ans: select d4st4nct e1.ename,e1.sal from emp e,e1,dept d 82ere e.deptno9d.deptno and
e.m/r9e1.empno and e1.sal5any %select a3/%sal& from emp /roup by deptno&;
Q:2/) Display employee name,sal,"omm and netpay for those employees whose netpay is greater
than or eDual to any other employee salary of the "ompany?
Ans: select ename,sal,<C*%comm,0&,sal$<C*%comm,0& from emp 82ere
sal$<C*%comm,0& 5any %select e.sal from emp e &;
Q:22) Display those employees whose salary is less than his manager #ut more than salary of
other managers?
Ans: select e.ename sub,e.sal from emp e,e1,dept d 82ere
e.deptno9d.deptno and e.m/r9e1.empno
and e.salKe1.sal
and e.sal 5any %select e2.sal from emp e2, e,dept d1 82ere
e.m/r9e2.empno and d1.deptno9e.deptno&;
Q:23) Display all employees names with total sal of "ompany with ea"h employee name?
Ans:
Q:24) =ind the last $(least) employees of "ompany?
Ans:
Q:3') =ind out the num#er of employees whose salary is greater than their managers salary?
Ans: select e.ename,e.sal,e1.ename,e1.sal from emp e,e1,dept d 82ere e.deptno9d.deptno and
e.m/r9e1.empno and e.sal5e1.sal;
Q:31) Display the manager who are not wor+ing under president #ut they are wor+ing under any
other manager?
Ans: select e2.ename from emp e1,emp e2,emp e6 82ere e1.m/r9e2.empno and e2.m/r9e6.empno and
e6.0obB9:"R,S@,<(:;
Q:32) Delete those department where no employee wor+ing?
Ans: delete from emp 82ere empno 4s null;
Q:3*) Delete those re"ords from emp ta#le whose deptno not a-aila#le in dept ta#le?
Ans: delete from emp e 82ere e.deptno not 4n %select deptno from dept&;
Q:3,) Display those enames whose salary is out of grade a-aila#le in salgrade ta#le?
Ans: select empno,sal from emp 82ere salK%select m4n%*)SA*& from sal/rade &
)R sal5%select ma1%24sal& from sal/rade&;
Q:3$) Display employee name,sal,"omm and whose netpay is greater than any other in the
"ompany?
Ans: select ename,sal,comm,sal$comm from emp 82ere sal$comm5any
%select sal$comm from emp &;
Q:3/) Display name of those employees who are going to retire *1)De")44 if ma1imum .o# period
is *' years?
Ans: select empno, 24redate,sysdate, toEc2ar%sysdate,:yyyy:& ' toEc2ar%24redate,:yyyy:&
from emp 82ere toEc2ar%sysdate,:yyyy:& ' toEc2ar%24redate,:yyyy:&960;
Q:32) Display those employees whose salary is odd -alue?
Ans: select ename ,sal from emp 82ere mod%sal,2&B90;
Q:33) Display those employees whose salary "ontains atleast * digits?
Ans: select ename,sal from emp 82ere len/t2%sal&96;
Q:34) Display those employees who .oined in the "ompany in the month of De"?
Ans: Select empno,ename from emp 82ere tr4m%toEc2ar%24redate,:=on:&&9tr4m%:,.:&;
Q:4') Display those employees whose name "ontains &?
Ans: select ename from emp 82ere ename l4Ie%:JAJ:&;
Q:41) Display those employees whose deptno is a-aila#le in salary?
Ans: select ename,sal from emp 82ere deptno 4n %select d4st4nct sal from emp&;
Q:42) Display those employees whose first 2 "hara"ters from hiredate ) last 2 "hara"ters sal?
Ans: select empno,24redate,sal from emp 82ere tr4m%substr%24redate,1,2&&9tr4m%substr%sal,'2,2&&;
or
select 24redate,sal from emp 82ere toE.2ar%24redate,:dd:&9tr4m%substr%sal,'2,2&&;
Q:4*) Display those employeess whose 1'% of salary is eDual to the year .oining?
Ans: select ename ,sal,0.10*sal from emp 82ere 0.10*sal9tr4m%toEc2ar%24redate,:yy:&&;
Q:4,) Display those employees who are wor+ing in sales or resear"h?
Ans: select e.ename from emp e ,dept d 82ere e.deptno9d.deptno and d.dname
4n%:SA*,S:,:R,S,AR.H:&;
Q:4$) Display the grade of .ones?
Ans: select ename,/rade from emp,sal/rade 82ere % /rade,sal& 9
%select /rade,sal from sal/rade,emp 82ere sal bet8een losal and 24sal and ename9:>)<,S:&;
Q:4/) Display those employees who .oined the "ompany #efore 1$th of the month?
Ans: select ename ,24redate from emp 82ere 24redateK:15'>ul'02: and 24redate 59:01'0ul'02:;
Q:42) Display those employees who has .oined #efore 1$th of the month?
Ans: select ename ,24redate from emp 82ere 24redateK:15'>ul'02:
Q:43) Delete those re"ords where no of employees in parti"ular department is less than *?
Ans: delete from emp 82ere deptno 4n %select deptno from emp /roup by deptno 2a34n/ count%*& K6 ;
Q:44&) Delete those employeewho .oined the "ompany 1' years #a"+ from today?
Ans: delete from emp 82ere empno 4n %select empno from emp
82ere toEc2ar%sysdate,:yyyy:&' toEc2ar%24redate,:yyyy:&5910&;
Q:44B) Display the deptname the num#er of "hara"ters of whi"h is eDual to no of employee in any
other department?
Ans:
Q:1'') Display the deptname where no employee is wor+ing?
Ans: select deptno from emp 82ere empno 4s null;
Q:1'1) Display those employees who are wor+ing as manager?
Ans: select e2.ename from emp e1,e2 82ere e1.m/r9e2.empno and e2.empno 4s not null;
Q:1'2) 6ount th num#er of employees who are wor+ing as managers (;sing set opetrator)?
Ans: select d.dname from dept d 82ere len/t2%d.dname& 4n %select count%*& from emp e 82ere e.deptnoB
9d.deptno /roup by e.deptno&;
Q:1'*) Display the name of the dept those employees who .oined the "ompany on the same date?
Ans: select a.ename,b.ename from emp a,emp b 82ere a.24redate9b.24redate and a.empnoB9b.empno;
Q:1',) Display those employees whose grade is eDual to any num#er of sal #ut not eDual to first
num#er of sal?
Ans: select ename,sal,/rade ,substr%sal,/rade,1& from emp,sal/rade 82ere
/radeB9substr%sal,1,1& and /rade 9 substr%sal,/rade,1&
and sal bet8een losal and 24sal;
Q:1'$) 6ount the no of employees wor+ing as manager using set operation?
Ans: Select count%empno& from emp 82ere
empno 4n %select a.empno from emp a
4ntersect
select b.m/r from emp b&;
Q:1'/) Display the name of employees who .oined the "ompany on the same date?
Ans: select a.ename,b.ename from emp a,emp b 82ere a.24redate9b.24redate and a.empnoB9b.empno;
Q:1'2) Display the manager who is ha-ing ma1imum num#er of employees wor+ing under him?
Ans: select e2.ename,count%*& from emp e1,e2 82ere e1.m/r9e2.empno /roup by e2.ename Ha34n/
count%*&9%select ma1%count%*&& from emp e1,e2 82ere e1.m/r9e2.empno /roup by e2.ename&;
Q:1'3) :ist out the employee name and salary in"reased #y 1$% and e1press as whole num#er of
Dollars?
Ans: select ename,sal,lpad%translate%sal,sal,%%sal $%sal*0.15&&!50&&,5,:N:& from emp;
Q:1'4) <rodu"e the output of the empta#le E>0<:@9>>I&5D G@BE for ename and .o# ?
Ans: select ename",="*)+,,EA<",0ob">)-" #R)= ,=";
Q:11') :ust of employees with hiredate in the format of ?Gune , 1433??
Ans: select ename,toEc2ar%24redate,:=ont2 dd yyyy:& from emp;
Q:111) print list of employees displaying ?Gust salary? if more than 1$'' if e1a"tly 1$'' display ?on
taget? if less than 1$'' display #elow 1$''?
Ans: select ename,sal,
%
case 82en sal K 1500 t2en
:-elo8E(ar/et:
82en sal91500 t2en
:)nE(ar/et:
82en sal 5 1500 t2en
:Abo3eE(ar/et:
else
:IIIII:
end
&
from emp;
Q:112) 8hi"h Duery to "al"ulate the length of time any employee has #een with the "ompany
Ans: select 24redate,toEc2ar%24redate,: HH:=@:SS:& #R)= emp;
Q:11*) 7i-en a string of the format ?nnJnn? . Kerify that the first and last 2 "hara"ters are
num#ers .&nd that the middle "hara"ter is ?J? <rint the e1pressions ?9es? C= -alid ?5@? of not -alid .
;se the following -alues to test your solution?12J$,?,'1J1a,?44J43??
Ans:
Q:11,) >mployes hire on @A 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 "ontains first digit of their deptno?
Ans: select ename,24redate, *AS(EA+ % ne1tEday%24redate,:#r4day:&&,
%
case 82en toEc2ar%24redate,:dd:& K9%:15:& t2en
*AS(EA+ % ne1tEday%24redate,:#r4day:&&
82en toEc2ar%24redate,:dd:&5%:15:& t2en
*AS(EA+% ne1tEday%addEmont2s%24redate,1&,:#r4day:&&
end
&
from emp;
Q:11$) Display those managers who are getting less than his employees salary?
Ans: select a.empno,a.ename ,a.sal,b.sal,b.empno,b.ename from emp a, emp b 82ere a.m/r9b.empno
and a.sal5b.sal;
Q:11/) <rint the details of employees who are su#ordinates to B:&F>?
Ans: select a.empno,a.ename ,b.ename from emp a, emp b 82ere a.m/r9b.empno
and b.ename9:-*A;,:;

You might also like