You are on page 1of 15

SQL QUERIES

1) Display the details of all employees


SQL>Select * from emp;
2) Display the depart information from department table
SQL>select * from dept;
3) Display the name and job for all the employees
SQL>select ename,job from emp;
4) Display the name and salary for all the employees
SQL>select ename,sal from emp;
) Display the employee no and totalsalary for all the employees
SQL>select empno,ename,sal,comm, sal+nvl(comm,0) as"total salary" from
emp
!) Display the employee name and ann"al salary for all employees#
SQL>select ename, 1*(sal+nvl(comm, 0)) as "ann!al Sal" from emp
$) Display the names of all the employees %ho are %or&in' in depart n"mber 1(#
SQL>select emame from emp "#ere deptno$10;
)) Display the names of all the employees %ho are %or&in' as *ler&s and
Dra%in' a salary more than 3(((#
SQL>select ename from emp "#ere job$%&L'()% and sal>*000;
+) Display the employee n"mber and name %ho are earnin' *omm#
SQL>select empno,ename from emp "#ere comm +s not n!ll;
1() Display the employee n"mber and name %ho do not earn any *omm#
SQL>select empno, ename from emp "#ere comm +s n!ll;
11) Display the names of employees %ho are %or&in' as *ler&s, salesman or analyst and
dra%in' a salary more than 3(((#
SQL>select ename from emp "#ere job$%&L'()% ,( -,.$%S/L'S0/1%
,( -,.$%/1/L2S3% /14 S/L>*000;
12) Display the names of the employees %ho are %or&in' in the *ompany for the past
years-
SQL>select ename from emp "#ere to5c#ar(sysdate,%2222%)6to5c#ar(#+redate,%2222%)>$7;
13) Display the list of employees %ho ha.e joined the *ompany before
3(/0U1/+( or after 31/DE2/+(#
SQL>select ename from emp "#ere #+redate 8 9*06-:161;;0< or #+redate >
9*164'&6;0<;
14) Display *"rrent Date#
SQL>select sysdate from d!al;
1) Display the list of all "sers in yo"r database 3"se *atalo' table)#
SQL>select !sername from all5!sers;
1!) Display the names of all tables from *"rrent "ser-
SQL>select tname from tab;
1$) Display the name of the *"rrent "ser#
SQL>s#o" !ser
1)) Display the names of employees %or&in' in depart n"mber 1( or 2( or 4( or
employees %or&in' as 2LER4S, S5LES651 or 515L7S8#
SQL>select ename from emp "#ere deptno +n (10, 0, =0) or job
+n (9&L'()S<,<S/L'S0/1<,</1/L2S3<);
1+) Display the names of employees %hose name starts %ith alphabet S#
SQL>select ename from emp "#ere ename l+>e 9S?<;
2() Display the Employee names for employees %hose name ends %ith alphabet S#
SQL>select ename from emp "#ere ename l+>e 9?S<;
21) Display the names of employees %hose names ha.e se*ond alphabet 5 in their
names#
SQL>select ename from emp "#ere ename l+>e 95/?<;
22) sele*t the names of the employee %hose names is e9a*tly fi.e *hara*ters in len'th#
SQL>select ename from emp "#ere len@t#(ename)$7;
23) Display the names of the employee %ho are not %or&in' as 6515:ERS#
SQL>select ename from emp "#ere job not +n(90/1/A'(<);
24) Display the names of the employee %ho are not %or&in' as S5LES651 ;R 2LER4
;R 515L7S8#
SQL>select ename from emp "#ere job not
+n(9S/L'S0/1<,<&L'()<,</1/L2S3<);
2) Display all ro%s from emp table# 8he system sho"ld %ait after e.ery
s*reen f"ll of information#
SQL>set pa!se on
2!) Display the total n"mber of employee %or&in' in the *ompany#
SQL>select co!nt(*) from emp;
2$) Display the total salary bei'in' paid to all employees#
SQL>select s!m(sal) from emp;
2)) Display the ma9im"m salary from emp table#
SQL>select maB(sal) from emp;
2+) Display the minim"m salary from emp table#
SQL>select m+n(sal) from emp;
3() Display the a.era'e salary from emp table#
SQL>select av@(sal) from emp;
31) Display the ma9im"m salary bein' paid to 2LER4#
SQL>select maB(sal) from emp "#ere job$<&L'()<;
32) Display the ma9im"m salary bein' paid to depart n"mber 2(#
SQL>select maB(sal) from emp "#ere deptno$0;
33) Display the minim"m salary bein' paid to any S5LES651#
SQL>select m+n(sal) from emp "#ere job$<S/L'S0/1<;
34) Display the a.era'e salary dra%n by 6515:ERS#
SQL>select av@(sal) from emp "#ere job$<0/1/A'(<;
3) Display the total salary dra%n by 515L7S8 %or&in' in depart n"mber 4(#
SQL>select s!m(sal) from emp "#ere job$</1/L2S3< and deptno$=0;
3!) Display the names of the employee in order of salary i#e the name of
the employee earnin' lo%est salary sho"ld salary appear first#
SQL>select ename from emp order by sal;
3$) Display the names of the employee in des*endin' order of salary#
SQL>select ename from emp order by sal desc;
3)) Display the names of the employee in order of employee name#
SQL>select ename from emp order by ename;
3+) Display empno,ename,deptno,sal sort the o"tp"t first base on name and %ithin name
by deptno and %ith in deptno by sal#
SQL>select empno,ename,deptno,sal from emp order by
4() Display the name of the employee alon' %ith their ann"al salary3sal<12)#8he name of
the employee earnin' hi'hest ann"al salary sho"ld apper first#
SQL>select ename,sal*1 from emp order by sal desc;
41) Display name,salary,hra,pf,da,total salary for ea*h employee# 8he
o"tp"t sho"ld be in the order of total salary,hra 1= of salary,da 1(= of salary,pf =
salary,total salary %ill be3salary>hra>da)/pf#
SQL>select ename,sal,salC100*17 as #ra,salC100*7 as pf,salC100*10 as
da, sal+salC100*17+salC100*106salC100*7 as total from emp;
42) Display depart n"mbers and total n"mber of employees %or&in' in ea*h department#
SQL>select deptno,co!nt(deptno)from emp @ro!p by deptno;
43) Display the .ario"s jobs and total n"mber of employees %ithin ea*h job 'ro"p#
SQL>select job,co!nt(job)from emp @ro!p by job;
44) Display the depart n"mbers and total salary for ea*h department#
SQL>select deptno,s!m(sal) from emp @ro!p by deptno;
4) Display the depart n"mbers and ma9 salary for ea*h department#
SQL>select deptno,maB(sal) from emp @ro!p by deptno;
4!) Display the .ario"s jobs and total salary for ea*h job
SQL>select job,s!m(sal) from emp @ro!p by job;
4$) Display the .ario"s jobs and total salary for ea*h job
SQL>select job,m+n(sal) from emp @ro!p by job;
4)) Display the depart n"mbers %ith more than three employees in ea*h dept#
SQL>select deptno,co!nt(deptno) from emp @ro!p by deptno #av+n@
co!nt(*)>*;
4+) Display the .ario"s jobs alon' %ith total salary for ea*h of the jobs
%here total salary is 'reater than 4((((#
SQL>select job,s!m(sal) from emp @ro!p by job #av+n@ s!m(sal)>=0000;
() Display the .ario"s jobs alon' %ith total n"mber of employees in ea*h
job#8he o"tp"t sho"ld *ontain only those jobs %ith more than three employees#
SQL>select job,co!nt(empno) from emp @ro!p by job #av+n@ co!nt(job)>*
1) Display the name of the empployee %ho earns hi'hest salary#
SQL>select ename from emp "#ere sal$(select maB(sal) from emp);
2) Display the employee n"mber and name for employee %or&in' as *ler& and earnin'
hi'hest salary amon' *ler&s#
SQL>select empno,ename from emp "#ere "#ere job$%&L'()%
and sal$(select maB(sal) from emp "#ere job$%&L'()%);
3) Display the names of salesman %ho earns a salary more than the hi'hest salary of
any *ler&#
SQL>select ename,sal from emp "#ere job$%S/L'S0/1% and sal>(select
maB(sal) from emp
"#ere job$%&L'()%);
4) Display the names of *ler&s %ho earn a salary more than the lo%est
salary of any salesman#
SQL>select ename from emp "#ere job$%&L'()% and sal>(select m+n(sal)
from emp
"#ere job$%S/L'S0/1%);
Display the names of employees %ho earn a salary more than that of
0ones or that of salary 'rether than that of s*ott#
SQL>select ename,sal from emp "#ere sal>
(select sal from emp "#ere ename$%-,1'S%)and sal> (select sal from emp
"#ere ename$%S&,33%);
) Display the names of the employees %ho earn hi'hest salary in their
respe*ti.e departments#
SQL>select ename,sal,deptno from emp "#ere sal +n(select maB(sal) from
emp @ro!p by deptno);
!) Display the names of the employees %ho earn hi'hest salaries in their
respe*ti.e job 'ro"ps#
SQL>select ename,sal,job from emp "#ere sal +n(select maB(sal) from emp
@ro!p by job)
$) Display the employee names %ho are %or&in' in a**o"ntin' department#
SQL>select ename from emp "#ere deptno$(select deptno from dept "#ere
dname$%/&&,:13D1A%)
)) Display the employee names %ho are %or&in' in 2hi*a'o#
SQL>select ename from emp "#ere deptno$(select deptno from dept "#ere
L,&$%&ED&/A,%)
+) Display the 0ob 'ro"ps ha.in' total salary 'reater than the ma9im"m
salary for mana'ers#
SQL>S'L'&3 -,.,S:0(S/L) F(,0 '0G A(,:G .2 -,. E/HD1A S:0(S/L)>(S'L'&3
0/I(S/L) F(,0 '0G JE'(' -,.$%0/1/A'(%);
!() Display the names of employees from department n"mber 1( %ith salary 'rether than
that of any employee %or&in' in other department#
SQL>select ename from emp "#ere deptno$10 and sal>any(select sal from
emp "#ere deptno not +n 10)K
!1) Display the names of the employees from department n"mber 1( %ith
salary 'reater than that of all employee %or&in' in other departments#
SQL>select ename from emp "#ere deptno$10 and sal>all(select sal from
emp "#ere deptno not +n 10)K
!2) Display the names of the employees in Upper*ase#
SQL>select !pper(ename)from emp
!3) Display the names of the employees in Lo%e*ase#
SQL>select lo"er(ename)from emp
!4) Display the names of the employees in ?roper*ase#
SQL>select +n+tcap(ename)from emp;
!) Display the len'th of 7o"r name "sin' appropriate f"n*tion#
SQL>select len@t#(%name%) from d!al
!!) Display the len'th of all the employee names#
SQL>select len@t#(ename) from emp;
!$) sele*t name of the employee *on*atenate %ith employee n"mber#
SQL>select enameLLempno from emp;
!)) User appropriate f"n*tion and e9tra*t 3 *hara*ters startin' from 2
*hara*ters from the follo%in' strin' @;ra*le@# i#e the o"t p"t sho"ld be @a*@#
SQL>select s!bstr(%oracle%,*,) from d!al
!+) Aind the Airst o**"ran*e of *hara*ter @a@ from the follo%in' strin' i#e
@2omp"ter 6aintenan*e 2orporation@#
SQL>S'L'&3 D1S3((%&omp!ter 0a+ntenance &orporat+on%,%a%,1) F(,0 4:/L
$() Repla*e e.ery o**"ran*e of alphabhet 5 %ith B in the strin' 5llens3"se
translate f"n*tion)
SQL>select translate(%/llens%,%/%,%.%) from d!al
$1) Display the informa*tion from emp table#Chere job mana'er is fo"nd it
sho"ld be displayed as boos3Use repla*e f"n*tion)#
SQL>select replace(-,.,%0/1/A'(%,%.,SS%) F(,0 '0G;
$2) Display empno,ename,deptno from emp table#Instead of display department n"mbers
display the related department name3Use de*ode f"n*tion)#
SQL>select
empno,ename,decode(deptno,10,%/&&,:13D1A%,0,%('S'/(&E%,*0,%S/L'S%,=0,%,G(/3D,1
S%) from emp;
$3) Display yo"r a'e in days#
SQL>select to5date(sysdate)6to5date(%106sep6MM%)from d!al
$4) Display yo"r a'e in months#
SQL>select mont#s5bet"een(sysdate,%106sep6MM%) from d!al
$) Display the *"rrent date as 1th 5"'est Ariday 1ineteen 1inety Sa.en#
SQL>select to5c#ar(sysdate,%ddt# 0ont# day year%) from d!al
$!) Display the follo%in' o"tp"t for ea*h ro% from emp table#
s*ott has joined the *ompany on %ednesday 13th 5"'"st ninten nintey#
SQL>select '1/0'LL% E/S -,D1'4 3E' &,0G/12 ,1 %LLto5c#ar(ED('4/3',%day
ddt# 0ont# year%) from '0G;
$$) Aind the date for nearest sat"rday after *"rrent date#
SQL>S'L'&3 1'I354/2(S2S4/3',%S/3:(4/2%)F(,0 4:/L;
$)) Display *"rrent time#
SQL>select to5c#ar(sysdate,%##N00Nss%) from d!alK
$+) Display the date three months Before the *"rrent date#
SQL>select add5mont#s(sysdate,*) from d!al;
)() Display the *ommon jobs from department n"mber 1( and 2(#
SQL>select job from emp "#ere deptno$10 and job +n(select job from emp
"#ere deptno$0);
)1) Display the jobs fo"nd in department 1( and 2( Eliminate d"pli*ate jobs#
SQL>select d+st+nct(job) from emp "#ere deptno$10 or deptno$0
(or)
SQL>select d+st+nct(job) from emp "#ere deptno +n(10,0);
)2) Display the jobs %hi*h are "niD"e to department 1(#
SQL>select d+st+nct(job) from emp "#ere deptno$10
)3) Display the details of those %ho do not ha.e any person %or&in' "nder them#
SQL>select eKename from emp,emp e "#ere empKm@r$eKempno @ro!p by
eKename #av+n@ co!nt(*)$1;
)4) Display the details of those employees %ho are in sales department and 'rade is 3#
SQL>select * from emp "#ere deptno$(select deptno from dept "#ere
dname$%S/L'S%)and sal bet"een(select losal from sal@rade "#ere @rade$*)and
(select #+sal from sal@rade "#ere @rade$*);
)) Display those %ho are not mana'ers and %ho are mana'ers any one#
i)display the mana'ers names
SQL>select d+st+nct(mKename) from emp e,emp m "#ere mKempno$eKm@r;
ii)display the %ho are not mana'ers
SQL>select ename from emp "#ere ename not +n(select d+st+nct(mKename)
from emp e,emp m "#ere mKempno$eKm@r);
)!) Display those employee %hose name *ontains not less than 4 *hara*ters#
SQL>select ename from emp "#ere len@t#(ename)>=;
)$) Display those department %hose name start %ith ESE %hile the lo*ation name ends
%ith E4E#
SQL>select dname from dept "#ere dname l+>e %S?% and loc l+>e %?)%;
))) Display those employees %hose mana'er name is 0;1ES#
SQL>select pKename from emp e,emp p "#ere eKempno$pKm@r and
eKename$%-,1'S%;
)+) Display those employees %hose salary is more than 3((( after 'i.in' 2(= in*rement#
SQL>select ename,sal from emp "#ere (sal+sal*K)>*000;
+() Display all employees %hile their dept names-
SQL>select ename,dname from emp,dept "#ere empKdeptno$deptKdeptno
+1) Display ename %ho are %or&in' in sales dept#
SQL>select ename from emp "#ere deptno$(select deptno from dept "#ere
dname$%S/L'S%);
+2) Display employee name,deptname,salary and *omm for those sal in bet%een 2((( to
((( %hile lo*ation is *hi*a'o#
SQL>select ename,dname,sal,comm from emp,dept "#ere sal bet"een 000
and 7000
and loc$%&ED&/A,% and empKdeptno$deptKdeptno;
+3)Display those employees %hose salary 'reter than his mana'er salary#
SQL>select pKename from emp e,emp p "#ere eKempno$pKm@r and pKsal>eKsal
+4) Display those employees %ho are %or&in' in the same dept %here his
mana'er is %or&#
SQL>select pKename from emp e,emp p "#ere eKempno$pKm@r and
pKdeptno$eKdeptno;
+) Display those employees %ho are not %or&in' "nder any mana'er#
SQL>select ename from emp "#ere m@r +s n!ll
+!) Display 'rade and employees name for the dept no 1( or 3( b"t 'rade is not 4 %hile
joined the *ompany before 31/de*/)2#
SQL>select ename,@rade from emp,sal@rade "#ere sal bet"een losal and
#+sal and deptno +n(10,*0) and @rade8>= and #+redate8%*164'&6O%;
+$) Update the salary of ea*h employee by 1(= in*rement %ho are not
eli'ibl% for *ommission#
SQL>!pdate emp set sal$sal+sal*10C100 "#ere comm +s n!ll;
+)) SELE28 those employee %ho joined the *ompany before 31/de*/)2 %hile their dept
lo*ation is ne%yor& or 2hi*a'o#
SQL>S'L'&3 '0G1,,'1/0',ED('4/3',41/0',L,& F(,0 '0G,4'G3
JE'(' ('0GK4'G31,$4'G3K4'G31,)/14
ED('4/3' 8%*164'&6O% /14 4'G3KL,& D1(%&ED&/A,%,%1'J 2,()%);
++) DIS?L57 E6?L;7EE 156E,0;B,DE?5R86E18,L;258I;1 A;R 5LL CF; 5RE
C;R4I1: 5S 6515:ERG
SQL>select ename,-,.,41/0',L,&/3D,1 from emp,4'G3 "#ere m@r +s not
n!ll;
1(() DIS?L57 8F;SE E6?L;7EES CF;SE 6515:ER 156E IS 0;1ESG //
H51D 5LS; DIS?L57 8FEIR 6515:ER 156EIG
SQL> S'L'&3 GK'1/0' F(,0 '0G ', '0G G JE'(' 'K'0G1,$GK0A( /14
'K'1/0'$%-,1'S%;
1(1) Display name and salary of ford if his salary is eD"al to hisal of his
'rade
SQL>select ename,sal,@rade from emp,sal@rade "#ere sal bet"een losal and
#+sal
and ename $%F,(4% /14 EDS/L$S/L;
1(2) Display employee name,job,depart name ,mana'er name,his 'rade and ma&e o"t an
"nder department %iseG
SQL>S'L'&3 'K'1/0','K-,.,41/0','0GK'1/0',A(/4' F(,0 '0G,'0G
',S/LA(/4',4'G3
JE'(' '0GKS/L .'3J''1 L,S/L /14 EDS/L /14 '0GK'0G1,$'K0A(
/14 '0GK4'G31,$4'G3K4'G31, ,(4'( .2 41/0'
1(3) List o"t all employees name,job,salary,'rade and depart name for e.ery one in the
*ompany e9*ept @2LER4@#Sort on salary display the hi'hest salaryG
SQL>S'L'&3 '1/0',-,.,41/0',S/L,A(/4' F(,0 '0G,S/LA(/4',4'G3 JE'('
S/L .'3J''1 L,S/L /14 EDS/L /14 '0GK4'G31,$4'G3K4'G31, /14 -,.
1,3 D1(%&L'()%),(4'( .2 S/L /S&;
1(4) Display the employee name,job and his mana'er#Display also employee %ho are
%itho"t mana'erG
SQL>select eKename,eKjob,e0GKename /S 0ana@er from emp,emp e "#ere
empKempno(+)$eKm@r
1() Aind o"t the top earners of *ompanyG
SQL>S'L'&3 4DS3D1&3 S/L F(,0 '0G ' JE'(' 7>$(S'L'&3 &,:13(4DS3D1&3 S/L)
F(,0
'0G / JE'(' /KS/L>$'KS/L),(4'( .2 S/L 4'S&;
1(!) Display name of those employee %ho are 'ettin' the hi'hest salaryG
SQL>select ename from emp "#ere sal$(select maB(sal) from emp);
1($) Display those employee %hose salary is eD"al to a.era'e of ma9im"m and
minim"mG
SQL>select ename from emp "#ere sal$(select maB(sal)+m+n(sal)C from
emp);
1()) Sele*t *o"nt of employee in ea*h department %here *o"nt 'reater than 3G
SQL>select co!nt(*) from emp @ro!p by deptno #av+n@ co!nt(deptno)>*
1(+) Display dname %here at least 3 are %or&in' and display only department nameG
SQL>select d+st+nct dKdname from dept d,emp e "#ere dKdeptno$eKdeptno
and *>any
(select co!nt(deptno) from emp @ro!p by deptno)
11() Display name of those mana'ers name %hose salary is more than a.era'e salary of
his *ompanyG
SQL>S'L'&3 'K'1/0','0GK'1/0' F(,0 '0G,'0G '
JE'(' '0GK'0G1,$'K0A( /14 'KS/L>(S'L'&3 /HA(S/L) F(,0 '0G);
111)Display those mana'ers name %hose salary is more than a.era'e salary of his
employeeG
SQL>S'L'&3 4DS3D1&3 '0GK'1/0' F(,0 '0G,'0G ' JE'('
'KS/L 8(S'L'&3 /HA('0GKS/L) F(,0 '0G
JE'(' '0GK'0G1,$'K0A( A(,:G .2 '0GK'1/0') /14
'0GK'0G1,$'K0A(;
112) Display employee name,sal,*omm and net pay for those employee
%hose net pay is 'reter than or eD"al to any other employee salary of
the *ompanyG
SQL>select ename,sal,comm,sal+nvl(comm,0) as 1etGay from emp
"#ere sal+nvl(comm,0) >any (select sal from emp)
113) Display all employees names %ith total sal of *ompany %ith ea*h
employee nameG
SQL>S'L'&3 '1/0',(S'L'&3 S:0(S/L) F(,0 '0G) F(,0 '0G;
114) Aind o"t last 3least)earners of the *ompany#G
SQL>S'L'&3 4DS3D1&3 S/L F(,0 '0G ' JE'('
7>$(S'L'&3 &,:13(4DS3D1&3 S/L) F(,0 '0G / JE'('
/KS/L8$'KS/L)
,(4'( .2 S/L 4'S&;
11) Aind o"t the n"mber of employees %hose salary is 'reater than their
mana'er salaryG
SQL>S'L'&3 'K'1/0' F(,0 '0G ,'0G ' JE'(' '0GK'0G1,$'K0A(
/14 '0GKS/L8'KS/L;
11!) Display those department %here no employee %or&in'G
SQL>select dname from emp,dept "#ere empKdeptno not +n(empKdeptno)
11$) Display those employee %hose salary is ;DD .al"eG
SQL>select * from emp "#ere sal80;
11)) Display those employee %hose salary *ontains alleast 3 di'itsG
SQL>select * from emp "#ere len@t#(sal)>$*;
11+) Display those employee %ho joined in the *ompany in the month of De*G
SQL>select ename from emp "#ere to5c#ar(#+redate,%0,1%)$%4'&%;
12() Display those employees %hose name *ontains E5EG
SQL>select ename from emp "#ere +nstr(ename,%/%)>0;
or
SQL>select ename from emp "#ere ename l+>e(%?/?%);
121) Display those employee %hose deptno is a.ailable in salaryG
SQL>select empKename from emp, emp e "#ere empKsal$eKdeptno;
122) Display those employee %hose first 2 *hara*ters from hiredate /last 2
*hara*ters of salaryG
SQL>select ename,S:.S3((#+redate,1,)LL'1/0'LLs!bstr(sal,6,) from emp
123) Display those employee %hose 1(= of salary is eD"al to the year of
joinin'G
SQL>select ename from emp "#ere to5c#ar(#+redate,%22%)$sal*0K1;
124) Display those employee %ho are %or&in' in sales or resear*hG
SQL>S'L'&3 '1/0' F(,0 '0G JE'(' 4'G31, D1(S'L'&3 4'G31, F(,0 4'G3
JE'('
41/0' D1(%S/L'S%,%('S'/(&E%));
12) Display the 'rade of jonesG
SQL>S'L'&3 '1/0',A(/4' F(,0 '0G,S/LA(/4'
JE'(' S/L .'3J''1 L,S/L /14 EDS/L /14 'name$%-,1'S%;
12!) Display those employees %ho joined the *ompany before 1 of the monthG
SQL>select ename from emp "#ere to5c#ar(#+redate,%44%)817;
12$) Display those employee %ho has joined before 1th of the month#
SQL>select ename from emp "#ere to5c#ar(#+redate,%44%)817;
12)) Delete those re*ords %here no of employees in a parti*"lar department is less than
3#
SQL>delete from emp "#ere deptno$(select deptno from emp
@ro!p by deptno #av+n@ co!nt(deptno)8*);
12+) Display the name of the department %here no employee %or&in'#
SQL> S'L'&3 'K'1/0','K-,.,0K'1/0',0K-,. F(,0 '0G ','0G 0
JE'(' 'K0A($0K'0G1,
13() Display those employees %ho are %or&in' as mana'er#
SQL>S'L'&3 0K'1/0' 0/1/A'( F(,0 '0G 0 ,'0G '
JE'(' 'K0A($0K'0G1, A(,:G .2 0K'1/0'
131) Display those employees %hose 'rade is eD"al to any n"mber of sal b"t not eD"al to
first n"mber of salG
SQL> S'L'&3 '1/0',A(/4' F(,0 '0G,S/LA(/4'
JE'(' A(/4' 1,3 D1(S'L'&3 S:.S3((S/L,0,1)F(,0 '0G)
132) ?rint the details of all the employees %ho are S"b/ordinate to BL54EG
SQL>select empKename from emp, emp e "#ere empKm@r$eKempno and
eKename$%.L/)'%;
133) Display employee name and his salary %hose salary is 'reater than
hi'hest a.era'e of department n"mberG
SQL>S'L'&3 S/L F(,0 '0G JE'(' S/L>(S'L'&3 0/I(/HA(S/L)) F(,0 '0G
A(,:G .2 4'G31,);
134) Display the 1(th re*ord of emp table3%itho"t "sin' ro%id)
SQL>S'L'&3 * F(,0 '0G JE'(' (,J1:0811
0D1:S
S'L'&3 * F(,0 '0G JE'(' (,J1:0810
13) Display the half of the ename@s in "pper *ase and remainin' lo%er*aseG
SQL>S'L'&3
S:.S3((L,J'(('1/0'),1,*)LLS:.S3((:GG'(('1/0'),*,L'1A3E('1/0'))
F(,0 '0G;
13!) Display the 1(th re*ord of emp table %itho"t "sin' 'ro"p by and ro%idG
SQL>S'L'&3 * F(,0 '0G JE'(' (,J1:0811
0D1:S
S'L'&3 * F(,0 '0G JE'(' (,J1:0810
4elete t#e 10t# record of emp tableK
SQL>4'L'3' F(,0 '0G JE'(' '0G1,$(S'L'&3 '0G1, F(,0 '0G JE'('
(,J1:0811
0D1:S
S'L'&3 '0G1, F(,0 '0G JE'(' (,J1:0810)
13$) 2reate a *opy of emp table-
SQL>create table ne"5table as select * from emp "#ere 1$;

13)) Sele*t ename if ename e9ists more than on*e#
SQL>select ename from emp e @ro!p by ename #av+n@ co!nt(*)>1;
13+) Display all enames in re.erse orderG3S6I8FJF8I6S)#
SQL>S'L'&3 ('H'(S'('1/0')F(,0 '0G;
14() Display those employee %hose joinin' of month and 'rade is eD"al#
SQL>S'L'&3 '1/0' F(,0 '0G JE'(' S/L .'3J''1
(S'L'&3 L,S/L F(,0 S/LA(/4' JE'('
A(/4'$3,5&E/((ED('4/3',%00%)) /14
(S'L'&3 EDS/L F(,0 S/LA(/4' JE'('
A(/4'$3,5&E/((ED('4/3',%00%));

141) Display those employee %hose joinin' D58E is a.ailable in deptno#
SQL>S'L'&3 '1/0' F(,0 '0G JE'(' 3,5&E/((ED('4/3',%44%)$4'G31,
142) Display those employees name as follo%s
/ /LL'1
. .L/)'
SQL> S'L'&3 S:.S3(('1/0',1,1),'1/0' F(,0 '0G;
143) List o"t the employees ename,sal,?A32(= ;A S5L) from emp-
SQL>S'L'&3 '1/0',S/L,S/L*K /S GF F(,0 '0G;
144) 2reate table emp %ith only one *ol"mn empno-
SQL>&reate table emp as select empno from emp "#ere 1$;
14) 5dd this *ol"mn to emp table ename .ra*har232()#
SQL>alter table emp add(ename varc#ar(0));
14!) ;ops I for'ot 'i.e the primary &ey *onstraint# 5dd in no%#
SQL>alter table emp add pr+mary >ey(empno);
14$) 1o% in*rease the len'th of ename *ol"mn to 3( *hara*ters#
SQL>alter table emp mod+fy(ename varc#ar(*0));
14)) 5dd salary *ol"mn to emp table#
SQL>alter table emp add(sal n!mber(10));
14+) I %ant to 'i.e a .alidation sayin' that salary *annot be 'reater 1(,((( 3note 'i.e a
name to this *onstraint)
SQL>alter table emp add constra+nt c#>5001 c#ec>(sal8$10000)
1() Aor the time bein' I ha.e de*ided that I %ill not impose this .alidation#6y boss has
a'reed to pay more than 1(,(((#
SQL>a@a+n alter t#e table or drop constra+nt "+t# alter table emp drop constra+nt c#>5001
(or)4+sable t#e constra+nt by !s+n@ alter table emp mod+fy constra+nt c#>5001 d+sable;
11) 6y boss has *han'ed his mind# 1o% he doesn@t %ant to pay more than 1(,(((#so
re.o&e that salary *onstraint#
SQL>alter table emp mod+fy constra+nt c#>5001 enable;
12) 5dd *ol"mn *alled as m'r to yo"r emp table-
SQL>alter table emp add(m@r n!mber(7));
13) ;hK 8his *ol"mn sho"ld be related to empno# :i.e a *ommand to add this
*onstraint#
SQL>/L3'( 3/.L' '0G /44 &,1S3(/D13 0A(54'G3 F,('DA1 )'2(0A()
('F'('1&'S
'0G('0G1,)

14) 5dd deptno *ol"mn to yo"r emp table-
SQL>alter table emp add(deptno n!mber(7));
1) 8his deptno *ol"mn sho"ld be related to deptno *ol"mn of dept table-
SQL>alter table emp add constra+nt dept5001 fore+@n >ey(deptno)
reference dept(deptno)
Pdeptno s#o!ld be pr+mary >eyQ
1!) :i.e the *ommand to add the *onstraint#
SQL>alter table 8table5name) add constra+nt 8constra+nt5name>
8constra+nt type>
1$) 2reate table *alled as ne%emp# Usin' sin'le *ommand *reate this table as %ell as
'et data into this table3"se *reate table as)-
SQL>create table ne"emp as select * from emp;
2reate table *alled as ne%emp# 8his table sho"ld *ontain only
empno,ename,dname#
SQL>create table ne"emp as select empno,ename,dname from emp,dept "#ere
1$;
1)) Delete the ro%s of employees %ho are %or&in' in the *ompany for more than 2
years#
SQL>delete from emp "#ere (sysdate6#+redate)C*R7>;
1+) ?ro.ide a *ommission31(= 2omm ;f Sal) to employees %ho are not earnin' any
*ommission#
SQL>select sal*0K1 from emp "#ere comm +s n!ll
1!() If any employee has *ommission his *ommission sho"ld be in*remented by 1(= of
his salary#
SQL>!pdate emp set comm$sal*K1 "#ere comm +s not n!ll;
1!1) Display employee name and department name for ea*h employee#
SQL>select empno,dname from emp,dept "#ere empKdeptno$deptKdeptno
1!2)Display employee n"mber,name and lo*ation of the department in %hi*h he is
%or&in'#
SQL>select empno,ename,loc,dname from emp,dept "#ere
empKdeptno$deptKdeptno;
1!3) Display ename,dname e.en if there are no employees %or&in' in a
parti*"lar department3"se o"ter join)#
SQL>select ename,dname from emp,dept "#ere empKdeptno$deptKdeptno(+)
1!4) Display employee name and his mana'er name#
SQL>select pKename,eKename from emp e,emp p "#ere eKempno$pKm@r;
1!) Display the department name and total n"mber of employees in ea*h
department#
SQL>select dname,co!nt(ename) from emp,dept "#ere
empKdeptno$deptKdeptno @ro!p by dname;
1!!)Display the department name alon' %ith total salary in ea*h department#
SQL>select dname,s!m(sal) from emp,dept "#ere empKdeptno$deptKdeptno
@ro!p by dname;
1!$) Display itemname and total sales amo"nt for ea*h item#
SQL>select +temname,s!m(amo!nt) from +tem @ro!p by +temname;
1!)) Crite a Q"ery 8o Delete 8he Repeted Ro%s from emp table-
SQL>4elete from emp "#ere ro"+d not +n(select m+n(ro"+d)from emp @ro!p
by ename)

1!+) 8; DIS?L57 8; $ R;CS AR;6 5 85BLE
SQL>select ename from emp
"#ere ro"+d +n(select ro"+d from emp "#ere ro"n!m8$M
m+n!s
select ro"+d from emp+ "#ere ro"n!m87)
1$() DIS?L57 8;? 1 R;CS AR;6 85BLEG
SQL>S'L'&3 * F(,0
(S'L'&3 * F(,0 '0G ,(4'( .2 '1/0' 4'S&)
JE'(' (,J1:0 810;
1$1) DIS?L57 8;? 3 S5L5RIES AR;6 E6?-
SQL>S'L'&3 S/L F(,0 ( S'L'&3 * F(,0 '0G ,(4'( .2 S/L 4'S& )
JE'(' (,J1:0 8=
1$2) DIS?L57 +th AR;6 8FE E6? 85BLEG
SQL>S'L'&3 '1/0' F(,0 '0G
JE'(' (,JD4$(S'L'&3 (,JD4 F(,0 '0G JE'(' (,J1:08$10
0D1:S
S'L'&3 (,JD4 F(,0 '0G JE'(' (,J1:0 810)
select second maB salary from emp;
select maB(sal) fromemp "#ere sal8(select maB(sal) from emp);

You might also like