Sie sind auf Seite 1von 27

Oracle Queries

By PenchalaRaju.Yanamala

001. Display all the information of the emp table.
SQL> Select * from emp ;
002. Display unique jobs from !" table.
SQL> SL#$ D%S$%&#$ 'ob ()*! !" +)*," -. 'ob ;
00/. List the 0etails of the emps in asc or0er of their salaries.
SQL> SL#$ * ()*! !"
*)D) -. Sal 1S# ;
002. List the 0etails of the emps in asc or0er of the Deptnos an0 0esc of
'obs.
SQL> SL#$ * ()*! !"
*)D) -. Deptno 1S#3 'ob DS# ;
004. Display all the unique job 5roups in the 0escen0in5 or0er
SQL> select unique job from emp or0er by job 0esc ;
006. Display all the 0etails of all 7!5rs8
SQL> select * from emp 9here job : ;!1&1+); ;
00<. List the emps 9ho joine0 before 1=>1.
SQL> select * from emp 9here hire0ate ? ;01@'an@1=>1; ;
00>. List the mpno3 name3 Sal3 Daily Sal of all mployees in the 1S#
or0er of 1nnSal.
SQL> SL#$ mpno3 name3 sal3 SalA/0 DailySal
()*! mp *)D) -. Sal*12 ;
00=. Display the empno 3 ename3 job3 hire0ate3 eBp of all !5rs
SQL> select empno3 ename3 sal3
monthsCbet9eenDsys0ate3hire0ateEA12 Bp
from emp 9here job : ;!1&1+); ;
010. List the empno3 ename3 sal3 eBp of all emps 9orFin5 for !5r <>/=.
SQL> select empno3 ename3 sal3
monthsCbet9eenDsys0ate3hire0ateEA12 Bp
from emp - 9here !5r : <>/= ;
011. Display the 0etails of the emps 9hose #omm. %s more than their sal.
SQL> select * from emp 9here comm > sal ;
012. List the emps in the asc or0er of Desi5nations
SQL> select * from emp or0er by job ;
01/. List the emps alon5 9ith their eBp an0 0aily sal is more than )s.100
SQL> Select emp.*3 monthsCbet9eenDsys0ate3hire0ateEA12 Bp
from emp 9here salA/0 > 100 ;
012. List the emps 9ho are either 7#L)G8 or 71&1L.S$8 in the 0esc or0er
SQL> Select * from emp 9here job in D7#L)G8371&1L.S$8E or0er by job
0esc ;
014. List the emps 9ho joine0 on 1!ay>13/1Dec>13 1<Dec>13 1='an>0 in
asc or0er of seniority.
SQL>
Select ename3hire0ate3monthsCbet9eenDsys0ate3hire0ateEA12 H"
from emp
9here hire0ate liFe ;01@!1.@>1; or hire0ate liFe ;/1@D#@>1;
or hire0ate liFe ;1<@D#@>1; or hire0ate liFe ;1=@'1&@>0;
or0er by hire0ate 0esc ;
016. List the emps 9ho are 9orFin5 for the 0eptno 10 or 20
SQL> Select * from emp 9here 0eptno in D10320E ;
01<. List the emps 9ho are joine0 in the year 1=>1
SQL> Select * from emp 9here hire0ate liFe 7I>18 ;
01>. List the emps 9ho are joine0 in the month of 1u5 1=>0
SQL> Select * from emp 9here hire0ate liFe I1,+@>08 ;
01=. List the emps 9hose annul sal ran5in5 from 22000 an0 24000
SQL> Select * from emp 9here sal*12 bet9een 22000 an0 24000 ;
020. List the emps those are haJin5 fiJe characters in their names.
SQL> Select * from emp 9here ename liFe 7CCCCC8 ;
021. List the enames those are startin5 9ith 7s8 an0 9ith fire characters
SQL> Select * from emp 9here ename liFe 7SCCCC8 ;
022. List the emps those are haJin5 four chars an0 thir0 char must be 7r8
SQL> Select * from emp 9here ename liFe 7CC)C8 ;
02/. List the 4 character names startin5 9ith 7s8 an0 en0in5 9ith 7h8
SQL> Select * from emp 9here ename liFe 7SCCCK8 ;
022. List the emps 9ho joine0 in 'anuary
SQL> Select * from emp 9here hire0ate liFe 7I'1&I8 ;
024. List the emps 9ho joine0 in the month of 9hich secon0 character is 7a8
SQL> Select * from emp 9here hire0ate liFe 7CC@C1I8 ;
026. List the emps 9hose sal is 2 0i5it number en0in5 9ith Lero
SQL> Select * from emp 9here sal liFe 7CCC08 ;
02<. List the emps 9hose names haJin5 a character set 7ll8 to5ether
SQL> Select * from emp 9here ename liFe 7ILLI8 ;
02>. List the emps those 9ho joine0 in >08s
SQL> Select * from emp 9here hire0ate liFe 7I>08 ;
02=. List the emps 9ho 0oes not belon5 to 0eptno 20
SQL> Select * from emp 9here 0eptno ?> 20 ;
0/0. List all the emps eBcept 7presi0ent8 M 7!5r8 in asc or0er of salaries
SQL> Select * from emp 9here job not in D7")S%D&$838!1&1+)8E
or0er by sal ;
0/1. List the emps 9ho joine0 in before or after 1=>1
SQL> Select * from emp 9here hire0ate not liFe ;I>1; ;
0/2. List the emps 9hose empno not startin5 9ith 0i5it <>
SQL> Select * from emp 9here empno not liFe 7<>I8 ;
0//. List the emps 9ho are 9orFin5 un0er 7!5r8
SQL> Select * from emp 9here m5r in
Dselect empno from emp 9here job : 7!1&1+)8E ;
0/2. List the emps 9ho joine0 in any year but not belon5s to the month of
!arch
SQL> Select * from emp 9here hire0ate not liFe 7I!1)I8 ;
0/4. List all the clerFs of 0eptno 20.
SQL> Select * from emp 9here 0eptno:20 an0 job :8#L)G8 ;
0/6. List the emps of 0eptno /0 or10 joine0 in the year 1=>1
SQL> Select * from emp 9here 0eptno inD103/0E an0 hire0ate liFe 8I>18 ;
0/<. Display the 0etails of 7Smith8
SQL> Select * from emp 9here ename : 7S!%$K8 ;
0/>. Display the location of 7Smith8
SQL> Select loc from emp 9here ename : 7S!%$K8 ;
0/=. List the total information of emp table alon5 9ith 0name an0 loc of all
the emps 9orFin5 un0er 71ccountin58 M 7)esearch8 in the asc 0eptno
SQL> SL#$ !".*3D&1!3L*# ()*! mp3 Dept
NK) Dname %& D;1##*,&$%&+;3;)S1)#K;E
1&D !".D"$&* : D"$.D"$&*
*)D) -. !".D"$&*
020. List the empno3 ename3 sal3 0name of all the 7!5rs8 an0 71nalyst8
9orFin5 in &N.*)G3 D1LL1S 9ith an eBp more than < years 9ithout
receiJin5 the #omma 1sc or0er of Loc.
SQL> SL#$ !"&*3 &1!3S1L3 0name ()*! !"3 D"$
NK) L*# %& D;&N .*)G;3;D1LL1S;E 1&D '*- %&
D;!1&1+);3;1&1L.S$;E
1&D !*&$KSC-$N&DS.SD1$3K%)D1$EA12 > < 1&D
#*!! %S &,LL 1&D !".D"$&* : D"$.D"$&*
*)D) -. L*# ;
021. List the empno3 ename3 sal3 0name3 loc3 0eptno3 job of all emps
9orFin5 at #K%#1+* or 9orFin5 for 1##*,&$%&+ 0ept 9it ann sal >
2>0003 but the sal shoul0 not be : /000 or 2>00 9ho 0oesn8t belon5s
to the !5r an0 9hose no is haJin5 a 0i5it 7<8 or 7>8 in /
r0
position in the
asc or0er of 0eptno an0 0esc or0er of job.
SQL> SL#$ !"&*3 &1!3'*-3S1L3!".D"$&*3 0name3 loc
()*! !"3 D"$
NK) DL*# : ;#K%#1+*; *) D&1! : ;1##*,&$%&+;E
1&D S1L*12 > 2>000 1&D S1L &*$ %&D/00032>00E
1&D !+) %S &,LL 1&D !"&* L%G ;CC<C; *) !"&* L%G
;CC>C;
1&D !".D"$&* : D"$.D"$&*
*)D) -. !".D"$&* 1S#3 '*- DS# ;
022. Display the total information of the emps alon5 9ith 5ra0es in the asc
or0er
SQL> Select emp.*3 5ra0e from emp3sal5ra0e 9here sal bet9een losal an0
hisal ;
02/. List all the 5ra0e2 an0 5ra0e / emps
SQL> Select emp.*3 5ra0e from emp3sal5ra0e 9here 5ra0e inD23/E an0
sal bet9een losal an0 hisal
022. Display all 5ra0e 234 1nalyst an0 !5r
SQL> Select emp.*35ra0e from emp3sal5ra0e 9here 5ra0e inD234E an0
'ob in D;1&1L.S$;3;!1&1+);E an0 sal bet9een losal an0 hisal ;
024. List the empno3 ename3 sal3 0name3 5ra0e3 eBp3 ann sal of emps
9orFin5 for 0ept 20 or 10.
SQL> Select empno3 ename3 sal3 sal*12 1nnSal3 0name3 5ra0e3
monthsCbet9eenDsys0ate3hire0ateEA12 Bp from emp3 0ept3
sal5ra0e
9here emp.0eptno inD10320E an0 emp.0eptno:0ept.0eptno an0 sal
bet9een losal an0 hisal
026. List all the information of emps 9ith loc an0 the 5ra0e of all the emps
belon5 to the 5ra0e ran5es from 2 to 2 9orFin5 at the 0ept those are
not startin5 9ith char set 7*"8 an0 not en0in5 9ith 7S8 9ith the 0esi5n
haJin5 a char 7a8 any 9here joine0 in the year >1 but not in the month
of !ar or Sep an0 sal not en0 9ith 7008 in the asc or0er of 5ra0es.
SQL> Select emp.*3 loc3 5ra0e from emp3 0ept3 sal5ra0e 9here
+ra0e bet9een 2 an0 2 an0 0name not liFe 7*"I8 an0
0name not liFe 7IS8 an0 'ob liFe 7I1I8 an0
Dhire0ate liFe 7I>18 an0 hire0ate not liFe 7I!1)I8 an0 hire0ate not
liFe 7IS"I8E an0
sal not liFe I00 an0 emp.0eptno:0ept.0eptno
an0 sal bet9een losal an0 hisal ;
02<. List the 0etails of the 0epts alon5 9ith empno3 ename or 9ithout the
emps
SQL> Select empno3 ename3 0ept.* from emp30ept 9here
emp.0eptno:0ept.0eptno ;
02>. List the 0etails of the emps 9hose salaries more than the employee
-L1G
SQL> Using self-join:
select 1.* from emp 13 emp - 9here
1.sal > -.sal an0 -.ename : ;-L1G;
Using sub-query:
select * from emp 9here
sal > Dselect sal from emp 9here ename : ;-L1G;E
02=. List the 0etails of the emps 9hose job is same as 1LL&.
SQL> select * from emp 9here job : Dselect job from emp 9here ename :
;1LL&;E
040. List the emps 9ho are senior to Gin5
SQL> select * from emp 9here hire0ate ? Dselect hire0ate from emp 9here
ename : ;G%&+;E
041. List the emps 9ho are senior to their o9n !+)s
SQL> select * from emp 1 9here hire0ate ?
Dselect hire0ate from emp - 9here b.mpno : 1.!5rE
042. List the emps of 0eptno 20 9hose jobs are same as 0eptno 10
SQL> Select * from emp 9here 0eptno:20 an0 job in Dselect job from emp
9here 0eptno:10E ;
04/. List the emps 9hose Sal is same as (*)D or S!%$K in 0esc or0er of
Sal.
SQL> Select 0istinct * from mp 9here sal in
Dselect sal from emp 9here ename in D;(*)D;3;S!%$K;EE
an0 ename not in D;(*)D;3;S!%$K;E
*r0er -y sal Desc ;
042. List the emps 9hose 'obs are same as !%LL) or Sal is more than
1LL&.
SQL> Select * from mp 9here job : Dselect job from emp 9here
ename:8!%LL)8E *) Sal > Dselect sal from emp 9here ename
:;1LL&;E ;
044. List the emps 9hose Sal is > the total remuneration of the
S1LS!1&.
SQL> Select * from mp 9here sal > Dselect S,!DsalE from emp 9here
'*-:8S1LS!1&;E;
&ote O no ro9s 9ill be selecte0. %f you choose job:8#L)G83 one recor0
9ill be fetche0.
046. List the emps 9ho are Senior to 7-L1G8 9orFin5 at #K%#1+* M
-*S$*&.
SQL> Select * from mp30ept 9here Kire0ate ?
Dselect hire0ate from emp 9here ename:;-L1G;E
1&D loc in D;#K%#1+*;3;-*S$*&;E an0 emp.0eptno : 0ept.0eptno ;
04<. List the emps of +ra0e /32 belon5s to the 0ept 1##*,&$%&+ an0
)S1)#K 9hose sal is more than 1LL& an0 eBp more than
S!%$K in the asc or0er of Bp.
SQL> Select * from mp30ept3sal5ra0e 9here 5ra0e inD/32E
1&D 0name in D;1##*,&$%&+;3;)S1)#K;E an0
Sal > Dselect sal from emp 9here ename:;1LL&;E
an0 Kire0ate ? Dselect hire0ate from emp 9here ename:;S!%$K;E
an0 emp.0eptno : 0ept.0eptno an0 sal bet9een losal an0 hisal;
&ote O no ro9s 9ill be selecte0. -ecause3 8S!%$K8 is the senior most in
the emp table. %f you choose ename:8$,)&)83 $9o recor0 9ill be
fetche0.
04>. List the emps 9hose jobs same as 1LL& *r S!%$K.
SQL> Select * from mp 9here job in
Dselect 0istinct job from emp 9here ename in
D;1LL&;3;S!%$K;EE an0 ename not in D;1LL&;3;S!%$K;E.
04=. Nrite a Query to 0isplay the 0etails of emps 9hose Sal is same as of
aE mployee Sal of mp1 table.
bE P Sal of any !5r of mp2 table.
cE $he Sal of any sales person 9ith eBp of 4 yrs belon5s to the
sales 0ept of emp/ table.
0E 1ny +ra0e 2 employees of mp2 table.
eE 1ny +ra0e 2M/ employee 9orFin5 for S1LS 0ept or
*")1$%*&S 0ept joine0 in >=.
SQL> Select * from emp 9here sal in Dselect sal from emp1E *)
sal in Dselect sal*/ from mp2 9here job:;!1&1+);E *)
sal in Dselect sal from mp/ 9here
monthsCbet9eenDsys0ate3hire0ateEA12 > 4E *)
sal in Dselect sal from mp23sal5ra0e 9here 5ra0e:2 an0 sal
bet9een losal an0 hisalE *)
sal in Dselect sal from mp3sal5ra0e30ept 9here 5ra0e inD23/E an0
0name in D;S1LS;3;*")1$%*&S;E an0 hire0ate liFe ;I>=; an0
sal bet9een losal an0 hisal an0 emp.0eptno:0ept.0eptnoE
060. List the jobs of Deptno 10 those are not foun0 in 0ept 20.
SQL> Select * from emp 9here 0eptno : 10 an0 job not in Dselect job from
emp 9here 0eptno:20E
061. List the emps of mp1 9ho are not foun0 in 0eptno 20
SQL> Select * from emp 9here ename not in Dselect ename from emp 9here
0eptno:20E
062. (in0 the hi5hest Sal of the mp table.
SQL> Select !aBDsalE from emp ;
06/. (in0 the 0etails of hi5hest pai0 employee.
SQL> Select * from emp 9here sal : Dselect !aBDsalE from empE ;
062. (in0 the hi5hest pai0 employee of sales 0ept.
SQL> Select * from emp 9here sal : Dselect !aBDsalE from emp30ept 9here
0name:8S1LS8 an0 emp.0eptno : 0ept.0eptno E ;
064. List the most recently hire0 emp of 5ra0e / belon5s to the Loc
#K%#1+*
SQL> Select * from emp30ept 9here loc:8#K%#1+*8 an0
hire0ate : DSelect maBDhire0ateE from empE;
066. List the emps 9ho are senior to most recently hire0 emp 9ho is
9orFin5 un0er !5r G%&+
SQL> Select * from emp 9here hire0ate ? DSelect maBDhire0ateE from empE
an0
m5r:Dselect empno from emp 9here ename:8G%&+8E ;
06<. List the 0etails of emp belon5s to &e9 .orF 9ith the +ra0e / to 4
eBcept 7")S%D&$8 9hose sal > the hi5hest pai0 emp of #K%#1+*
in +roup 9here there is 7!1&1+)8 M 71&1L.S$8 not 9orFin5 for !5r
G%&+
SQL> Select * from emp30ept3sal5ra0e 9here loc:;&N .*)G; an0
+ra0e in D/3234E an0 job ?> ;")S%D&$; an0 Sal >
Dselect !aBDsalE from emp30ept 9here loc:;#K%#1+*; an0
'ob not in D;!1&1+);3;1&1L.S$;E an0 emp.0eptno : 0ept.0eptnoE
an0 !5r :
Dselect empno from emp 9here ename:;G%&+;E 1n0
emp.0eptno : 0ept.0eptno an0 sal bet9een losal an0 hisal ;
06>. Display the 0etails of most senior employee belon5s to 1=>1.
SQL> select * from emp 9here hire0ate : Dselect minDhire0ateE
from emp 9here hire0ate liFe ;I>1;E ;
06=. List the emps 9ho joine0 in >1 9ith job same as the most senior
person of year >1.
SQL> select * from emp 9here hire0ate liFe ;I>1; an0 job :
Dselect job from emp 9here hire0ate: Dselect minDhire0ateE from emp
9here hire0ate liFe ;I>1;EE ;
0<0. List the most senior emp 9orFin5 un0er G%&+ an0 +ra0e is more than
/.
SQL> select emp.* from emp3sal5ra0e 9here hire0ate :
Dselect maBDhire0ateE from emp 9here m5r:
Dselect empno from emp 9here ename : ;G%&+;EE an0
5ra0e > / an0 sal bet9een losal an0 hisal
0<1. (in0 the total sal 5iJen to the 7!+)8
SQL> select sumDsalE from emp 9here job : ;!1&1+);
0<2. (in0 the total annual sal to 0istribute job 9ise in the year >1
SQL> select 'ob3sumDsal*12E from emp +)*," -. '*- ;
0</. Display the total sal of emps belon5 to +ra0e /.
SQL> select 5ra0e3 sumDsalE from emp3sal5ra0e 9here
sal bet9een losal an0 hisal 5roup by 5ra0e ;
0<2. Display the aJ5 salaries of all #L)GS
SQL> select aJ5DsalE from emp 9here job : ;#L)G; ;
0<4. List the emps in 0ept 20 9hose sal is > the aJ5 sal of 0eptno 10 emps.
SQL> select * from emp 9here sal > Dselect aJ5DsalE from emp
9here 0eptno:10E an0 0eptno:20 ;
0<6. Display the number of emps for each job 5roup 0eptno 9ise
SQL> select 0name3job3countDempnoE from emp30ept
9here 0ept.0eptno : emp.0eptno
5roup by 0name3job ;
0<<. List the !5r no M no. of emps 9orFin5 for those !5rs in the asc !5rno.
SQL> select m5r3countD*E from emp 5roup by m5r or0er by m5r asc ;
0<>. List the 0ept 0etails 9here at least t9o emps are 9orFin5.
SQL> select 0name3 countDempnoE from emp30ept
9here 0ept.0eptno : emp.0eptno
5roup by 0name3job
haJin5 countDempnoE >: 2 ;
0<=. Display the 5ra0e3 number of emps3 maB sal of each 5ra0e.
SQL> select 5ra0e3 countDempnoE3maBDsalE from emp30ept3sal5ra0e
9here 0ept.0eptno : emp.0eptno an0
sal bet9een losal an0 hisal
5roup by 5ra0e ;
0>0. Display 0name3 5ra0e3 no of emps 9here atleast t9o emps are
7#L)GS8
SQL> select 0name35ra0e3 countDempnoE from emp30ept3sal5ra0e
9here 0ept.0eptno : emp.0eptno an0
sal bet9een losal an0 hisal
5roup by 0name35ra0e
haJin5 countD;#L)G;E >: 2 ;
0>1. List the 0etails of the 0ept 9here the maB no of emps are 9orFin5
SQL> select 0name3 countDempnoE from emp30ept
9here 0ept.0eptno : emp.0eptno 5roup by 0name
haJin5 countDempnoE : Dselect maBDcountDempnoEE from emp 5roup
by 0eptnoE ;
0>2. Display the emps 9hose !5r name is 'ones
SQL> Select * from emp 9here m5r : Dselect empno from emp 9here ename
: 7'*&S8E ;
0>/. List the emps 9hose salary is more than /000 after 5iJin5 20I
increment
SQL> Select !".*3S1LQDS1L*.2E incrsal from emp 9here S1LQDS1L*.2E >
/000 ;
0>2. List the emps 9ith their 0ept names.
SQL> Select emp.*3 0name from emp3 0ept 9here
emp.0eptno : 0ept.0eptno ;
0>4. List the emps 9ho are not 9orFin5 in sales 0ept
SQL> Select emp.*3 0name from emp3 0ept 9here
Dname ?> 7S1LS8 an0
emp.0eptno : 0ept.0eptno ;
0>6. List the emps name3 0ept3 sal M comm. (or those 9hose salary is
bet9een 2000 an0 4000 9hile loc is #K%#1+*
SQL> Select ename3 0name3 sal3 comm from emp3 0ept 9here
sal bet9een 2000 an0 4000 an0 loc : ;#K%#1+*; an0
emp.0eptno : 0ept.0eptno ;
0><. List the emps 9hose salary is > his !ana5er8s salary.
SQL> select 1.* from emp 13emp - 9here
1.m5r:-.empno an0 1.sal > -.sal ;
0>>. List the 5ra0e3 employee name for the 0eptno 10 or 0eptno /0 but
sal5ra0e is not 2 9hile they joine0 the company before 8/1@D#@>28
SQL> select 5ra0e3 ename3 hire0ate from emp3sal5ra0e 9here
0eptno in D103/0E an0 5ra0e ?> 2 an0 hire0ate
in Dselect hire0ate from emp 9here hire0ate
? ;/1@D#@>2;E an0 sal bet9een losal an0 hisal
0>=. List the name3 job3 0name3 loc for those 9ho are 9orFin5 as a 7!+)s8
SQL> select empno3ename3 job3 0name3 loc from emp30ept 9here
job : ;!1&1+); an0 emp.0eptno : 0ept.0eptno ;
0=0. List the emps 9hose m5r name is 7'*&S8 an0 also list their mana5er
name
SQL> select 1.*3 -.ename from emp 13 emp - 9here
1.m5r : -.empno an0 b.ename:;'*&S; ;
0=1. List the name an0 salary of (*)D if his salary is equal to hisal of his
+ra0e
SQL> select ename3 sal from emp 9here ename : ;(*)D;
an0 sal : Dselect hisal from emp3 sal5ra0e 9here ename : ;(*)D;
an0 sal bet9een losal an0 hisalE
0=2. List the name3 job3 0name3 !ana5er3 salary3 +ra0e 0ept 9ise.
SQL> Select ename3 job3 !5r3 sal3 0name3 5ra0e from
emp3 0ept3 sal5ra0e 9here emp.0eptno:0ept.0eptno
an0 sal bet9een losal an0 hisal an0 emp.0eptno in
Dselect 0istinct 0eptno from emp
5roup by emp.0eptnoE ;
0=/. List the emps name3 job3 salary3 5ra0e an0 0name eBcept 7#L)G8s
an0 sort on the basis of hi5hest salary.
SQL> select ename3 job3 sal3 5ra0e3 0name from emp3 0ept3 sal5ra0e
9here job ?> ;#L)G; an0 0ept.0eptno : emp.0eptno an0
sal bet9een losal an0 hisal or0er by sal 0esc ;
0=2. List the emps name3 job an0 !ana5er 9ho are 9ithout !ana5er.
SQL> select ename3 job3 m5r from emp 9here m5r is &,LL ;
0=4. List the names of emps 9ho are 5ettin5 the hi5hest salary 0ept 9ise.
SQL> Select ename3 job3 sal3 0name from emp3 0ept
9here emp.0eptno:0ept.0eptno an0 emp.0eptno in
Dselect 0istinct 0eptno from emp 5roup by emp.0eptnoE
an0 sal in Dselect maBDsalE from emp 5roup by 0eptnoE ;
0=6. List the emps 9hose salary is equal to aJera5e of maBimum an0
minimum.
SQL> TO RETRIEVE SAL WHICH IS = TO AV! SAL
Select ename3 job3 sal from emp 9here
sal : Dselect aJ5DsalE from empE ;
TO RETRIEVE SAL WHICH IS = TO AV! O" #A$ SAL A%& #I%
SAL
select * from emp 9here sal : Dselect DmaBDsalEQminDsalEEA2 from
empE ;
0=<. List the no of emps in each 0ept 9here the number is more than /.
SQL> Select 0eptno3 countD0eptnoE from emp
5roup by 0eptno haJin5 countD0eptnoE > / ;
0=>. List the names of 0epts. Nhere at least / emps are 9orFin5 in each
0ept.
SQL> Select 0name3 countDemp.0eptnoE from emp30ept
9here emp.0eptno : 0ept.0eptno
5roup by 0name haJin5 countDemp.0eptnoE > / ;
0==. List the mana5ers 9hose salary is more than his emps aJ5 sal.
SQL> Select 0istinct 1.* from emp 13 emp -
9here 1.empno : -.m5r an0
1.sal > Dselect aJ5D-.salE from emp 5roup by -.!5rE ;
100. List the name3 sal3 comm. (or those emps 9hose net pay is > or : any
other employee salary of the company.
SQL> Select 0istinct 1.ename3 1.sal3 1.comm from emp 13 emp - 9here
DD1.sal Q 1.sal*1.2E Q D1.sal*1.2E @D1.sal*1.6 Q >0EE >: Dselect aJ5DsalE
from empE ;
101. List the emps 9hose sal ? his !ana5er but more than other !ana5er
SQL> Select 0istinct 1.* from emp 13 emp -3 emp #
9here 1.sal ? -.sal an0 1.m5r : -.empno an0
1.sal > #.sal an0 1.m5r not in Dselect empno
from emp 9here empno ?> -.mpno an0 job : ;!1&1+);E ;
RRR
102. List the employee names an0 his annual salary 0ept 9ise.
SQL> Select ename3 0name3 sal*12 from emp3 0ept 9here
emp.0eptno in Dselect 0eptno from emp 5roup by 0eptnoE
an0 emp.0eptno : 0ept.0eptno ;
*)
Select 0eptno3 ename3 sal*12 from emp 5roup by 0eptno3ename3sal ;
10/. (in0 out least 4 earners of the company.
SQL> select * from emp 9here ro9num ? 6 or0er by sal asc
102. (in0 out the no of emps 9hose salary is > their !ana5er salary.
SQL> select countD1.empnoE from emp 13 emp - 9here
1.sal > -.sal an0 1.!5r : -.empno ;
104. List the !5rs 9ho are not 9orFin5 un0er 7")S%D&$8 but are 9orFin5
un0er other !ana5er.
SQL> select 1.&1!3 1.'*- from emp 13 emp - 9here
1.!5r : -.empno an0 -.ename ?> ;")S%D&$; 1&D 1.job :
;!1&1+); ;
106. List the recor0s from emp 9hose 0eptno is not in 0ept
SQL> K*N #1& %$ - "*SS%-LRRR
S1 )()&# G. T1L, N%$K*,$ 1 ")%!1). G. T1L,.U
select * from emp 9here 0eptno not in Dselect 0eptno from 0eptE
10<. List the name3 sal3 comm. 1n0 net pay is more than any other
employee.
SQL> select empno3 sal3 comm3 salQsal*1.2Q>00 &et from emp 9here
sal : Dselect maBDsalE from empE ;
10>. List the enames 9ho are retirin5 after 8/1@D#@>=8 the maB job perio0
is 20..
SQL> Select empno3 ename3 hire0ate3 sys0ate3
monthsCbet9eenDsys0ate3hire0ateEA12 Bp from emp
9here monthsCbet9eenDsys0ate3hire0ateEA12 > 20
or0er by hire0ate ;
1-*T Q,). %S N*)G%&+ -,$ -L*N Q,). %S &*$. %&
-L*N Q,). % +1T D1$DE %& "L1# *( S.SD1$.
select emp.*3 monthsCbet9eenD;/1@D#@>=;3hire0ateEA12
)etirementCDate
from emp 9here monthsCbet9eenD;/1@D#@>=;3hire0ateE A 12 > 20 ;
'on()s*be(+een,-./01 gi2ing o/3 gre4(er ()4n 055!
10=. List those emps 9hose sal is o00 Jalue.
SQL> Select * from emp 9here mo0Dsal32E ?> 0 ;
110. List the emps 9hose sal contain / 0i5its
SQL> Select * from emp 9here len5thDsalE : / ;
111. List the emps 9ho joine0 in the month of 7D#8
SQL> Select * from emp 9here hire0ate liFe 7ID#I8 ;
112. List the emps 9hose names contains 718
SQL> select ename from emp 9here ename liFe ;I1I; ;
11/. Lis( ()e e'3s +)ose 6e3(no is 424il4ble in )is s4l4ry!
SQL> select ename3 sal3 0eptno from emp
9here substrDsal3len5thDsalE@132E : 0eptno
or substrDsal3len5thDsalE@232E : 0eptno
or substrDsal3len5thDsalE@/32E : 0eptno ;
112. Lis( ()e e'3s +)ose firs( 1 7)4rs fro' )ire64(e = l4s( 1 7)4rs of
s4l4ry!
SQL> select ename3 sal3 hire0ate from emp
9here substrDhire0ate3132E : substrDsal3len5thDsalE@132E ;
114. Lis( ()e e'3s +)ose 058 of s4l is equ4l (o ye4r of 9oining
SQL> select ename3sal 3 sal * .13 hire0ate from emp
9here sal * .1 liFe substrDhire0ate3len5thDhire0ateE@132E ;
116. Lis( firs( :58 of 7)4rs of en4'e in lo+er 74se 4n6 re'4ining 4re
u33er 74se!
SQL> select ename3 lo9erDsubstrDename313len5thDenameEA2EE3
upperDsubstrDename3Dlen5thDenameEA2EQ13len5thDenameEEE from emp ;
11<. Lis( ()e 6n4'e +)ose no of e'3s is = (o no of 7)4rs in ()e
6n4'e!
SQL> select 0name3 len5thD0nameE Len from 0ept 9here 0eptno :
Dselect emp.0eptno from emp3 0ept 9here
emp.0eptno : 0ept.0eptno haJin5 #ountDempnoE in
Dselect len5thD0nameE from 0ept 5roup by 0nameE
5roup by emp.0eptnoE ;
$K%S Q,). N%LL N*)G 1($) !*D%(%&+ !" $1-L. 1$
")S$ $K) 1) &* S,#K D&1! NK) &*. *( !"S :
&* *( #K1)S %& $K D&1!. $* +$ $K *,$",$3 % K1D $*
#K1&+ *& S1LS D"$DD"$&* /0E !" $* S*! *$K)
D"$ V S* $K1$ $K &* *( #K1)S %& S1LS D"$D4E %S : &*
*( !"S *( $K1$ D"$. S1$ ")S&$ $K) 1) 6 !"SU
%( , D*&8$ #K1&+ $K 1-*T3 , +$ $K N)*&+ *A" (*)
$K S1!.
select 0name3 len5thD0nameE Len from 0ept 9here 0eptno :
Dselect emp.0eptno from emp3 0ept 9here
emp.0eptno : 0ept.0eptno haJin5 #ountDempnoE in
Dselect len5thD0nameE from 0ept 5roup by 0nameE
5roup by emp.0eptnoE ;
*A" O )eserch 3 > Nhich is a 9ron5 *A".
select 0name3 countDempnoE from emp3 0ept
9here emp.0eptno : 0ept.0eptno
5roup by 0name ;
*A" O Dname3 count of emps
11>. Lis( ()e e'3s ()ose +)o joine6 in 7o'34ny before 0:
()
of ()e
'on()!
SQL>
select * from emp 9here hire0ate in
Dselect hire0ate from emp 9here hire0ate > Dselect
hire0ate from emp 9here hire0ate liFe ;12@I;EE ;
&* )*NS SL#$D
11=. List the 0name3 no of chars of 9hich is no of emp8s in any other 0ept
SQL> RRR
select 0name3 len5thD0nameE Len from 0ept 9here 0eptno %&
Dselect emp.0eptno from emp3 0ept 9here emp.0eptno : 0ept.0eptno
haJin5 #ountDempnoE in Dselect countDempnoE from
emp 5roup by 0eptnoE 5roup by emp.0eptnoE ;
select 0name3 len5thD0nameE from 0ept ;
120. List the emps 9ho are 9orFin5 as mana5ers
SQL> select * from emp 9here empno in Dselect m5r from empE ;
121. List the name of 0ept 9here hi5hest no of emps are 9orFin5
SQL> select 0name3 emp.0eptno3 countDempnoE from emp3 0ept
9here emp.0eptno : 0ept.0eptno haJin5 countDempnoE :
Dselect maBDcountDempnoEE from emp 5roup by 0eptnoE
5roup by emp.0eptno3 0name ;
122. #ount the no of emps 9ho are 9orFin5 as 7!ana5ers8 Dusin5 set
optionE
SQL> Select * from emp 9here empno in Dselect empno from empE
,nion Dselect !5r from empE
IVI% ERROR : ;<uery blo7= )4s in7orre7( nu'ber of resul(
7olu'ns>
12/. List the emps 9ho joine0 in the company on the same 0ate
SQL> select * from emp 9here hire0ate in Dselect hire0ate from emp
haJin5 countDhire0ateE > 1 5roup by hire0ateE ;
122. List the 0etails of the emps 9hose 5ra0e is equal to one tenth of sales
0ept.
SQL> select * from emp3 sal5ra0e 9here 5ra0e : 0eptnoA10
an0 0eptno : Dselect 0eptno from 0ept 9here 0name:;S1LS;E
an0 sal bet9een losal an0 hisal ;
OR
select * from emp3 sal5ra0e 9here 5ra0e : 0eptnoA10
an0 sal bet9een losal an0 hisal ;
124. List the name of the 0ept 9here more than aJ5. no of emps are
9orFin5
SQL> select 0name3 0ept.0eptno3 countDempnoE from emp23 0ept
9here emp2.0eptno in Dselect 0eptno from emp 5roup by emp.0eptnoE
an0 emp2.0eptno : 0ept.0eptno 5roup by 0ept.0eptno3 0name
haJin5 countDempnoE >: Dselect countD0eptnoE from 0eptE ;
126. List the mana5ers name 9ho is haJin5 maB no of emps 9orFin5 un0er
him
SQL> select ename from emp 9here empno :
Dselect m5r from emp haJin5 countDempnoE :
Dselect maBDcountDempnoEE from emp 5roup by m5rE 5roup by m5rE ;
12<. List the ename an0 sal is increase0 by 14I an0 eBpresse0 as no of
0ollars.
SQL>
select ename3 Dsal Q sal * 0.14E3 0eco0eDsal3 sal Q sal * 0.143
Dsal Q sal * 0.14EA/2E &e9Sal from emp ;
12>. "ro0uce the output of !" table 7!"C1&DC'*-8 for ename an0 job
SQL> select ename WW ;C1&DC; WW job from emp ;
12=. "ro0uce the follo9in5 output from !"
!"L*.
@@@@@@@@@@@@@@@@
S!%$KDclerFE
1LL&DsalesmanE
SQL> select ename WW ;D; WW lo9erDjobE WW;E; !"L*. from emp ;
1/0. List the emps 9ith hire0ate in format 'une 231=>>.
SQL> select ename3 toCcharDhire0ate3;!onth DD3.....;E from emp ;
1/1. "rint a list of emp8s listin5 7just salary8 if salary is more than 14003 7*n
tar5et8 if salary is 1400 an0 7-elo9 14008 if salary is less than 1400.
SQL>
select ename3 sal3 0eco0eDsal3Dsal>1400E3 ;just salary;3
14003;*n tar5et;3Dsal?1400E3;-elo9 1400;3salE from emp ;
!%SS%&+ )%+K$ "1)&$KS%S.
1/2. Nrite a query to return the 0ay of the 9eeF for any 0ate entere0 in
format 7DD@!!@..8
SQL> select toCcharDhire0ate3 ;0ay;E from emp ;
*)
select toCcharDsys0ate3 ;0ay;E from 0ual
1//. Nrote a query to calculate the len5th of serJice of any employee 9ith
the company3 use D(%& to aJoi0 repetitiJe typin5 of functions
SQL> D(%& RRRR
1/2. +iJe a strin5 of format 7&&A&&83 Jerify that the first an0 last t9o
characters are numbers an0 that the mi00le character is 7A8. "rint the
eBpression 7.S8 if Jali03 7&*8 if not Jali0. ,se the follo9in5 Jalues to
test your solution.
712A/283 701Ala83 7==A=>8
SQL>
1/4. mps hire0 on or before 14
th
of any month are pai0 on the last (ri0ay
of that month. $hose hire0 after 14
th
are pai0 on the first (ri0ay of the
follo9in5 month. "rint a list of emps3 their hire0ate an0 the first pay
0ate. Sort on hire0ate.
SQL>
1/6. #ount the no of characters 9ithout consi0erin5 spaces for each name.
SQL> Select ename3 len5thDenameE from emp ;
1/<. (in0 out the emps 9ho are 5ettin5 0ecimal Jalue in their sal 9ithout
usin5 liFe operator.
SQL>
Select ename3 sal3 toCcharDsubstrDtoCcharDsalE3/31EE from emp ;
Select ename3 sal from emp 9here
toCcharDsubstrDtoCcharDsalE3@/31EE :;.;
1/>. List those emps 9hose salary contains first four 0i5it of their
0eptno. RRR
SQL> RRRRRRRRRR salary contains first four 0i5it of their 0eptno RRRRR
1/=. List those mana5ers 9ho are 5ettin5 less than his emps salary.
SQL> select 0istinct -.* from emp 13 emp - 9here
1.!5r:-.empno an0 1.sal > -.sal ;
120. "rint the 0etails of all the emps 9ho are sub@or0inates to -L1G.
SQL> select * from emp 9here m5r :
Dselect empno from emp 9here ename:;-L1G;E ;
121. List the emps 9ho are 9orFin5 as mana5ers usin5 co@relate0 sub@
query
SQL> Using Sub <uery :
Select * from emp 9here empno inDselect m5r from empE ;
Using Co-Rele4(e6 <uery :
Select 0istinct 1.* from emp 13 emp - 9here 1.empno :-.!5r ;
Using Co-Rele4(e6 Sub <uery :
Select 0istinct 1.* from emp 13 emp -
9here 1.empno inDselect -.m5r from empE
122. List the emps 9hose !5r name is 7'*&S8 an0 also 9ith his !ana5er
&ame.
SQL> select 0istinct 1.*3 -.ename from emp 13 emp - 9here 1.m5r :
Dselect empno from emp 9here ename:;'*&S;E an0
-.ename:;'*&S; ;
12/. Define a Jariable representin5 the eBpression use0 to calculate on
emps total annual remuneration. ,se the Jariable in a statement 9hich
fin0s all emps 9ho can earn /0000 a year or more.
SQL> select emp.*3 sal*12 1nnCSal from emp 9here sal*12 >: /0000 ;
122. (in0 out ho9 many mana5ers are there in the company.
SQL> Select countD*E from emp 9here empno inDselect m5r from empE ;
124. (in0 aJ5 sal an0 aJ5 total remuneration for each job type. )emember
salesman earn commission.
SQL> Select job3 countDempnoE3aJ5DsalE3 sumDsalE from emp 5roup by job ;
*)
Select job3 aJ5DsalE3 sumD0eco0eDcomm3null30EQsalE from emp 5roup
by job ;
*)
Select job3 aJ5D0eco0eDcomm3null30EQsalE3
sumD0eco0eDcomm3null30EQsalE from emp 5roup by job ;
126. #hecF 9hether all the emps numbers are in0ee0 unique.
SQL> RRR
12<. List the emps 9ho are 0ra9in5 less than 1000. sort the output by
salary.
SQL> Select * from emp 9here sal ? 1000 or0er by sal ;
12>. List the employee name3 job3 annual sal3 0eptno3 0ept name3 an0
5ra0e 9ho earn /6000 a year or 9ho are not #L)GS.
SQL> Select ename3 job3 sal*12 1nnSal3 emp.0eptno3 0name3 5ra0e
from emp3 0ept3 sal5ra0e 9here sal*12 > /6000 an0
job ?> ;#L)G; an0 sal bet9een losal an0 hisal an0
0ept.0eptno : emp.0eptno ;
12=. (in0 out the job that 9as fille0 in the first half of 1=>/ an0 same job
that 9as fille0 0urin5 the same perio0 of 1=>2.
SQL>
select job from emp 9here hire0ate in Dselect hire0ate from
emp 9here hire0ate > ;01@',L@>2;E an0 hire0ate in Dselect hire0ate
from emp 9here hire0ate > ;01@',L@>2;E
%O ROWS SELECTE&
140. (in0 out the emps 9ho joine0 in the company before their mana5ers.
SQL> select 1.empno3 1.hire0ate3 1.m5r from emp 13 emp -
9here 1.m5r : -.empno an0 1.hire0ate ? -.hire0ate ;
141. List all the emps by name an0 number alon5 9ith their mana5er8s
name an0 number also list G%&+ 9ho has no 7!ana5er8
SQL>
select 0istinct 1.empno3 1.ename3 1.m5r3 -.empno3 -.ename
from emp 13 emp - 9here 1.m5r : -.empno ;
K*N $* )$)%T G%&+8S %&(*
select * from emp 9here empno in
Dselect 0istinct 1.empno from
emp 13 emp - 9here 1.m5r : -.empnoE
union
Dselect -.empno from emp - 9here
ename:;G%&+;E ;
)))* O Query -locF has incorrect number of result columns.
142. (in0 all the emps 9ho earn minimum sal for each job 9ise in asc or0er
of sal
SQL> select * from emp 9here sal in Dselect minDsalE from emp
5roup by jobE or0er by sal ;
14/. (in0 out all the emps 9ho earn hi5hest sal in each job type. Sort in 0es
or0er of sal
SQL> select * from emp 9here sal in Dselect maBDsalE from emp
5roup by jobE or0er by sal 0esc ;
142. (in0 out the most recently hire0 emps in each 0ept or0er by hire0ate
SQL> select * from emp 9here hire0ate in Dselect maBDhire0ateE from emp
5roup by jobE or0er by hire0ate 0esc ;
144. List the emp name3 sal an0 0eptno for each emp 9ho earns a sal
5reater than the aJ5 for their 0ept or0er by 0eptno
SQL>
select ename3 sal3 0eptno from emp 9here sal in
Dselect sal from emp 9here sal > Dselect aJ5DsalE from emp
5roup by 0eptnoE 5roup by 0eptnoE or0er by 0eptno
&*$ 1 +)*," -. H")SS%*&
146. List the 0eptno 9here there are no emps
SQL> select 0eptno3 0name from 0ept 9here 0eptno not in
Dselect 0eptno from empE ;
14<. List the no of emps an0 aJ5 salary 9ithin each 0ept for each job.
SQL> select 0eptno3 job3 countDempnoE3 aJ5DsalE from emp 5roup by 0eptno3
job ;
14> (in0 the maB aJ5 salary 0ra9n for each job eBcept for 7")S%D&$8
SQL> select job3 maBDsalE3 aJ5DsalE from emp
9here job ?> ;")S%D&$; 5roup by job ;
14=. (in0 the name an0 job of the emps 9ho earn !aB salary an0
#ommission.
SQL> select ename3 job3 sal3 comm from emp
9here sal:Dselect maBDsalE from empE or
comm:Dselect maBDcommE from empE ;
160. List the name3 job an0 salary of the emps 9ho are not belon5in5 to the
0ept 10 but 9ho haJe the same job an0 salary as the emps of 0ept 10.
SQL> select ename3 job3 sal from emp 9here 0eptno ?> 10
an0 job in Dselect job from emp 9here 0eptno:10E
an0 sal in Dselect sal from emp 9here 0eptno:10E ;
&* S,#K )#*)DS3 S* +%T O
select ename3 job3 sal from emp 9here 0eptno ?> 10
an0 job in Dselect job from emp 9here 0eptno:10E
or sal in Dselect sal from emp 9here 0eptno:10E ;
161. List the 0eptno3 name3 job3 salary an0 salQcomm. *f the emps 9ho
earn the secon0 hi5hest earnin5s DsalQcommE
SQL> select ename3 job3 sal3 salQ0eco0eDcomm3null30E3 0eptno from
emp 9here empno : Dselect empno from emp 9here
salQ0eco0eDcomm3null30E :
Dselect maBDsalQ0eco0eDcomm3null30EE from emp 9here
salQ0eco0eDcomm3null30E in Dselect salQ0eco0eDcomm3null30E
from emp 9here salQ0eco0eDcomm3null30E ?
Dselect maBDsalQ0eco0eDcomm3null30EE from empEEEE ;
Select ename3 job3 sal3 salQ0eco0eDcomm3null30E3 0eptno from emp
9here
sal : Dselect maBDsalE from emp 9here sal in
Dselect sal from emp 9here sal ? Dselect maBDsalE from empEEE ;
Select ename3 job3 sal3 salQcomm3 0eptno from emp 9here
salQcomm : Dselect maBDsalQ0eco0eDcomm3null30EE from emp
9here salQ0eco0eDcomm3null30E in Dselect salQ0eco0eDcomm3null30E
from emp 9here salQ0eco0eDcomm3null30E ? Dselect
maBDsalQ0eco0eDcomm3null30EE from empEEE
&* )*NS SL#$D
162. List the 0eptno3 name3 job3 salary an0 salQcomm. *f the salesman
SQL> select ename3 job3 sal3 comm3 0eptno from emp 9here job :
;S1LS!1&;
16/. List the 0eptno an0 their aJ5 salaries for 0ept 9ith the aJ5 salary less
than the aJ5 for all 0epartments.
SQL> select 0eptno3 aJ5DsalE from emp haJin5 aJ5DsalE :
Dselect minDaJ5DsalEE from emp haJin5 aJ5DsalE in
Dselect aJ5DsalE from emp 5roup by 0eptnoE 5roup by 0eptnoE
5roup by 0eptno ;
162. List out the names an0 salaries of the emps alon5 9ith their mana5er
names an0 salaries for those emps 9ho earn more salary than their
mana5er.
SQL> select 1.ename3 1.sal3 -.ename3 -.sal from emp 13 emp -
9here 1.m5r : -.empno an0 1.sal > -.sal ;
164. List out the name3 job3 sal of the emps in the 0epartment 9ith the
hi5hest aJ5 sal.
SQL> select ename3 job3 sal from emp 9here sal >:
Dselect maBDaJ5DsalEE from emp 5roup by 0eptnoE ;
166. List the empno3 sal an0 comm. of emps
SQL> Select empno3 sal3 comm (rom emp ;
16<. List the 0etails of the emps in the asc or0er of salary.
SQL> Select * from emp or0er by sal ;
16>. List the 0ept in asc or0er of job an0 the 0esc or0er of emps print
empno3 ename
SQL> select empno3 ename3 job3 sal3 0eptno from emp or0er by job3 empno
0esc ;
16=. Display the unique 0ept of emps.
SQL> select * from emp 9here 0eptno : Dselect 0eptno from emp
haJin5 countD0eptnoE : 1 5roup by 0eptnoE;
$o 5et the ans9er enter a ne9 recor0 9ith 0eptno as 20.
1<0. Display the unique 0ept 9ith jobs.
SQL> select 0name3job from emp3 0ept 9here emp.0eptno : Dselect 0eptno
from emp
haJin5 countD0eptnoE : 1 5roup by 0eptnoE ;
1<1. Display the 0etails of -L1G.
SQL> Select * from emp 9here ename : 7-L1G8 ;
1<2. List all #L)Gs
SQL> Select * from emp 9here job : 7#L)G8 ;
1</. List all emps joine0 on 1 !ay >1.
SQL> Select * from emp 9here hire0ate : 801@!1.@>18 ;
1<2. List empno3 ename3sal3 0eptno of 0ept 10 emps in the asc or0er of sal.
SQL> Select empno3 ename3sal3 0eptno from emp 9here 0eptno : 10 or0er
by sal ;
1<4. List the emps 9hose salaries are less than /400.
SQL> Select * from emp 9here sal ? /400 ;
1<6. List the emps mpno3 name3sal for all emp joine0 before 1 apr >1
SQL>
Select mpno3 name3sal3hire0ate from emp 9here hire0ate in D
select hire0ate from emp 9here hire0ate > ;01@1")@>1;E ;
Select mpno3 name3sal3hire0ate from emp 9here hire0ate ? ;01@
1")@>1;
+iJin5 9ron5 output.
1<<. List the emps 9hose ann sal is ? 24000 in the asc or0er of salaries
SQL> Select * from emp 9here sal*12 ? 24000 or0er by sal ;
1<>. List the empno3 ename3 ann sal3 0aily sal of all salesman in the asc
ann sal.
SQL> Select empno3 ename3 sal*12 1nnSal3 salA/0 DailySal
from emp 9here job : 7S1LS!1&8 or0er by sal ;
1<=. List the empno3 ename3 hire0ate3 current 0ate M eBp in the asc or0er of
eBp.
SQL> Select empno3 ename3 hire0ate3 sys0ate3
monthsCbet9eenDsys0ate3hire0ateEA12 Bp from emp or0er by hire0ate
;
1>0. List the emps 9hose eBp is more than 10 years.
SQL> select * from emp 9here
monthsCbet9eenDsys0ate3hire0ateEA12 > 10 ;
1>1. List the empno3 ename3sal $1 /0I3 D1 20I3 K)1 40I3 5ross3 L%#3
"(3 net 0e0uctions3 net allo9ances an0 net sal in the asc or0er of net
sal.
SQL> select empno3 ename3sal3sal*./ $13sal*.2 D13sal*.4 K)13 >0 %$3
sal*.02 L%#3 sal*.12 "(3 Dsal*.16E@>0 &etDe0uctions3 sal*1.2
&et1llo9ances3
salQsal*1.2 +ross3 DsalQsal*1.2 @ Dsal*.16E@>0E &etSal from emp or0er
by sal ;
1>2. List the emps 9ho are 9orFin5 as !5rs.
SQL> Select * from emp 9here job : 7!1&1+)8 ;
1>/. List the emps 9ho are either #L)Gs or !1&1+)s.
SQL> Select * from emp 9here job : 7!1&1+)8 or job : 7#L)G8;
1>2. List the emps 9ho joine0 in the follo9in5 0ates O 1 may >13 1< noJ >13
/0 0ec >1.
SQL> select * from emp 9here hire0ate in
D;01@!1.@>1;3 ;1<@&*T@>1;3 ;/0CD#@>1;E ;
&* )*NS N%LL - SL#$D. $K) %S &* S,#K K%)D1$S
1>4. List the emps 9ho joine0 in year >1.
SQL> select * from emp 9here hire0ate liFe ;I>1; ;
1>6. List the emps 9hose ann sal ran5in5 from 2/000 to 20000.
SQL> Select * from emp 9here sal*12 bet9een 2/000 an0 20000 ;
1><. List the emps 9orFin5 un0er the !5rs </6=3 <>=03 <6423 <=00.
SQL> Select * from emp 9here !5r in D</6=3 <>=03 <6423 <=00E ;
&* )*NS N%LL - SL#$D. $K) %S &* S,#K !"&*s
1>>. List the emps 9ho joine0 in the secon0 half of >2.
SQL>
Select * from emp 9here hire0ate in Dselect hire0ate
from emp 9here hire0ate > ;/0@',&@>2;E ;
Select * from emp 9here hire0ate > ;/0@',&@>2;;
&* )*NS SL#$D
1>=. List all the 2 chars emps.
SQL> Select ename from emp haJin5
len5thDenameE : 2 5roup by ename ;
1=0. List the emps names startin5 9ith 7!8 9ith 4 chars
SQL> Select ename from emp 9here ename liFe 7!CCCC8 ;
&* )*NS N%LL - SL#$D. $K) %S &* S,#K !"s. +%T
*& !*) 7C8 %& &1!.
1=1. List the emps names en0 9ith 7K8 all to5ether 4 chars.
SQL> Select ename from emp 9here ename liFe ;CCCCK; ;
1=2. List the names start 9ith 7!8
SQL> Select ename from emp 9here ename liFe ;!I;
1=/. List the emps 9ho joine0 in the year >1
SQL> Select * from emp 9here hire0ate liFe 7I>18 ;
1=2. List the emps 9hose sal is en0in5 9ith 00.
SQL> Select * from emp 9here sal liFe 7I008 ;
1=4. List the emps 9ho joine0 in the month of jan
SQL> Select * from emp 9here hire0ate liFe 7I'1&I8 ;
1=6. List the emps 9ho joine0 in the month haJin5 a char 7a8
SQL> Select * from emp 9here hire0ate liFe 7I1I ;8
1=<. List the emps 9ho joine0 in the month haJin5 a secon0 char is 718
SQL> Select * from emp 9here hire0ate liFe 7I@C1I8 ;
1=>. List the emps 9hose salary is 2 0i5it no.
SQL> Select * from emp 9here sal liFe 7CCCC8 ;
1==. List the emps 9ho joine0 in >08s.
SQL> Select * from emp 9here hire0ate liFe 7I>C8 ;
200. List the emps 9ho are either clerFs 9ith an eBp more than >y
SQL> Select * from emp 9here job : 7#L)G8 or
!onthsCbet9eenDsys0ate3hire0ateEA12 > > ;
201. List the !5rs of 0ept 10 or 20.
SQL> select * from emp 9here 0eptno in D10320E an0 job : ;!1&1+); ;
202. List the emps joine0 in 'an 9ith salary ran5in5 from 1400 to 2000.
SQL> Select * from emp 9here hire0ate liFe 7I'1&I8 an0
Sal bet9een 1400 an0 2000 ;
20/. List the unique jobs of 0epts. 20 or /0 in 0es or0er
SQL> select 0istinct job from emp 9here 0eptno in D10320E or0er by job 0esc ;
202. List the emps alon5 9ith the eBp of those 9orFin5 un0er the m5r
9hose no is startin5 9ith < but shoul0 not haJe a 0i5it = joine0 before
1=>/.
SQL>
select emp.*3 monthsCbet9eenDsys0ate3hire0ateEA12 Bp
from emp 9here m5r liFe ;<I; an0 m5r not liFe ;I=I;
an0 hire0ate ? Dselect ;01@'an@>/; from 0ualE ;
#K#G *,$",$. D1$ %S +%T%&+ N)*&+ )S,L$.
204. List the emps 9ho are 9orFin5 as either m5r or analyst 9ith sal ran5in5
from 2000 an0 4000 an0 9ithout comm..
SQL> select * from emp 9here '*- %& D;!1&1+);3;1&1L.S$;E an0
sal bet9een 2000 an0 4000 an0 comm is null ;
206. List the empno3 ename3 sal3 job of emps 9ith the ann sal ? /2000 but
receiJin5 some comm.3 Nhich shoul0 not be > sal an0 0esi5nation
shoul0 be salesman 9orFin5 for 0ept /0.
SQL> select empno3 ename3 sal3 job from emp 9here
sal*12 ? /2000 an0 comm is not null an0
comm ? sal an0 job : ;S1LS!1&; an0 0eptno:/0 ;
20<. List the emps 9ho are 9orFin5 for 0ept 10 or 20 9ith 0esi5s as #L)G
or analyst 9ith a sal is either / or 2 0i5its 9ith an eBp > >y3 but not
belon5s to months of !1)3 1") M S" an0 9orFin5 for m5rs M no is
not en0in5 9ith >> or 46.
SQL> select * from emp 9here
0eptno in D10320E an0 job %& D;#L)G;3;1&1L.S$;E an0
len5thDsalE in D/32E an0 monthsCbet9eenDsys0ate3hire0ateEA12 > >
an0 hire0ate not in Dselect hire0ate from emp 9here hire0ate liFe
;I!1)I; or
hire0ate liFe ;I1")I; or hire0ate liFe ;IS"I;E an0 m5r is not null
an0
empno not in Dselect empno from emp 9here empno liFe ;I>>; or
empno liFe ;I46;E ;
20>. List the empno3 ename3 sal3 job3 0eptno M eBp of all emps belon5s to
0ept 10 or 20 9ith an eBp 6 to 10 yrs 9orFin5 un0er some m5r 9ithout
comm.3 9ith a job not en0in5 9ith 7!1&8 but sal > >40 or the emps
9orFin5 for 0ept /0 9ith a job haJin5 a char 718 irrespectiJe of the
position 9ith comm. > 200 9ith eBp >: <y an0 sal ? 2400 but not
belon5s to the month S" or &*T 9orFin5 un0er the !5r 9hose no is
not haJin5 0i5its either = or 0 in the asc 0ept M 0esc 0ept. SRRRU
SQL>
select empno3 ename3 sal3 job3 0eptno3
monthsCbet9eenDsys0ate3hire0ateEA12 Bp from emp 9here
D0eptno in D10320E an0 DmonthsCbet9eenDsys0ate3hire0ateEA12
bet9een 6 an0 10E
an0 m5r is not null an0 comm is null an0 job not liFe ;I!1&; an0 sal >
>40E
or
D0eptno : /0 an0 job liFe ;I1I; an0 comm > 200 an0
monthsCbet9eenDsys0ate3hire0ateEA12 >: < an0
sal ? 2400 an0 hire0ate not in Dselect hire0ate from emp 9here
hire0ate liFe ;IS"I; or hire0ate liFe ;I&*TI;E an0
m5r in Dselect empno from emp 9here empno not liFe ;I=I; or empno
liFe ;I0I;EE
or0er by 0eptno 0esc
&* )*NS SL#$D
20=. List the 0etails of emps 9orFin5 at #K%#1+*.
SQL> Select * from emp30ept 9here loc : 7#K%#1+*8 an0
Dept.0eptno : emp.0eptno ;
210. List the empno3 ename30eptno3 loc of all emps
SQL> Select empno3ename30ept.0eptno3loc from emp30ept 9here
Dept.0eptno : emp.0eptno ;
211. List the empno3 ename3 loc3 0name of all 0epts. 10 or 20.
SQL> Select empno3ename30name3loc from emp30ept 9here
Deptno in D10320E an0 Dept.0eptno : emp.0eptno ;
212. List the empno3 sal3 loc of all emps 9orFin5 at #K%#1+*3 D1LL1S
9ith an eBp> 6yrs.
SQL> Select empno3 sal3 loc from emp3 0ept
9here Loc in D;#K%#1+*;3;D1LL1S;E an0
!onthsCbet9eenDsys0ate3hire0ateEA12 > 6
an0 mp.0eptno : 0ept.0eptno ;
21/. List the emps alon5 9ith loc of those 9ho belon5s to Dallas3 ne9 yorF
9ith sal ran5in5 from 2000 to 4000 joine0 in >1.
SQL> Select ename3 loc from emp3 0ept
9here Loc in D;&N .*)G;3;D1LL1S;E an0
sal bet9een 2000 an0 4000 an0
hire0ate liFe ;I>1;
an0 mp.0eptno : 0ept.0eptno ;
212. List the empno3 ename3 sal3 5ra0e of all emps.
SQL> Select empno3 ename3 sal3 5ra0e from emp3 sal5ra0e
Nhere sal bet9een losal an0 hisal ;
214. List the 5ra0e 2M/ emp of #hica5o
SQL> Select emp.* from emp3sal5ra0e 9here
'ob : 7#K%#1+*8 an0 5ra0e in D23/E an0 sal bet9een losal an0
hisal ;
216. List the emps 9ith loc M 5ra0e of accountin5 0ept or the locs Dallas or
#hica5o 9ith the 5ra0es / to 4 M eBp > 6y.
SQL> Select ename3 loc3 5ra0e from emp3 0ept3 sal5ra0e
9here job : ;1##*,&$%&+; or Loc in D;chica5o;3;D1LL1S;E an0
5ra0e in D/3234E an0 monthsCbet9eenDsys0ate3hire0ateEA12 > 6
an0 mp.0eptno : 0ept.0eptno an0 sal bet9een losal an0 hisal ;
21<. List the 5ra0e / emps of research M operations 0epts. 'oine0 after
1=>< an0 9hose names shoul0 not be either miller or allen.
SQL> Select ename3 loc3 5ra0e3 0name3 job from emp3 0ept3 sal5ra0e
9here 5ra0e : / an0 0name in D;)S1)#K;3;*")1$%*&S;E
an0 hire0ate in Dselect hire0ate from emp 9here hire0ate >
;/1@D#@><;E an0 ename not in D;!%LL);3;1LL&;E an0
mp.0eptno : 0ept.0eptno an0 sal bet9een losal an0 hisal ;
&* S,#K )#*)DS3 S* +%T O
Select ename3 loc3 5ra0e3 0name3 job from
emp3 0ept3 sal5ra0e 9here 5ra0e : / an0
0name in D;)S1)#K;3;*")1$%*&S;3;S1LS;E an0
hire0ate in Dselect hire0ate from emp 9here hire0ate
? ;/1@D#@><;E an0 ename not in D;!%LL);3;1LL&;E an0
mp.0eptno : 0ept.0eptno an0 sal bet9een losal an0 hisal ;
21>. List the emps 9hose job is same as smith.
SQL> Select * from emp 9here job : Dselect job from emp 9here
ename:8S!%$K8E ;
21=. List the emps 9ho are senior to miller.
SQL> Select * from emp 9here hire0ate ?
Dselect hire0ate from emp 9here ename : ;!%LL);E ;
220. List the emps 9hose job is same as either allen or sal > allen
SQL> Select * from emp 9here ename ?> ;1LL&; 1&D
Djob : Dselect job from emp 9here ename : ;1LL&;E or
sal > Dselect sal from emp 9here ename : ;1LL&;EE ;
221. List the emps 9ho are senior to their o9n m5r.
SQL> Select 1.* from emp 13 emp - 9here
1.m5r : -.empno an0 1.hire0ate ?
Dselect -.hire0ate from emp 9here
empno : -.empnoE or0er by 1.hire0ate ;
222. List the emps 9hose sal > blaFe8s
SQL> Select 1.* from emp 13 emp - 9here 1.sal >
Dselect sal from emp 9here -.ename : ;-L1G;
an0 emp.empno:-.empnoE ;
22/. List the emps 9ho belon5s to 0ept 10 an0 sal > allen8s sal
SQL> Select 1.* from emp 13 emp - 9here
1.0eptno : 10 an0 1.sal >
Dselect sal from emp 9here -.ename :
;1LL&; an0 emp.empno:-.empnoE ;
*)
select * from emp 9here 0eptno: 10 an0
sal > Dselect sal from emp 9here ename:;1LL&;E ;
222. List the !5rs 9ho are senior to Fin5 M 9ho are junior to smith.
SQL> Select * from emp 9here job : ;!1&1+); an0
hire0ate ? Dselect hire0ate from emp 9here ename : ;G%&+;E an0
hire0ate > Dselect hire0ate from emp 9here ename : ;S!%$K;E ;
224. List the empno3 ename3 loc3 sal3 0name3loc of all the emps belon5 to
Fin58s 0ept
SQL> Select empno3 ename3 sal3 0name3 loc from emp3 0ept3 sal5ra0e
Nhere emp.0eptno : Dselect 0eptno from emp 9here ename:;G%&+;E
an0
emp.0eptno : 0ept.0eptno an0 sal bet9een losal an0 hisal
226. List the emps 9hose 5ra0e are > the 5ra0e of miller.
SQL> Select emp.*3 5ra0e from emp3 sal5ra0e Nhere 5ra0e >
Dselect 5ra0e from emp3 sal5ra0e Nhere ename : ;!%LL);
an0 sal bet9een losal an0 hisalE an0 sal bet9een losal an0 hisal
22<. List the emps 9ho are belon5s to 0allas or #hica5o 9ith the 5ra0e
same as a0ams or eBp more than smith.
SQL> Select * from emp3 0ept3 sal5ra0e Nhere loc in
D;D1LL1S;3;#K%#1+*;E an0 D5ra0e : Dselect 5ra0e
from emp3sal5ra0e 9here ename:;1D1!S; an0
sal bet9een losal an0 hisalE or
monthsCbet9eenDsys0ate3hire0ateEA12 >
Dselect monthsCbet9eenDsys0ate3hire0ateEA12 from
emp 9here ename : ;S!%$K;EE an0 ename not in D;1D1!S;3;S!%$K;E
an0 emp.0eptno : 0ept.0eptno an0 sal bet9een losal an0 hisal ;
22>. List the emps 9hose sal is same as for0 or blaFe.
SQL> Select * from emp 9here sal in Dselect sal from emp 9here ename
inD7(*)D838-L1G8EE ;
Select * from emp 9here sal in
Dselect sal from emp 9here ename : ;(*)D;E
,&%*&
Dselect sal from emp 9here ename : ;-L1G;E ;
+%T%&+ ))*) O 7Query blocF has incorrect no. of result columns.7
22=. List the emps 9hose sal is same as any one of the follo9in5O
aE Sal of any clerF of emp1 table.
bE 1ny emp of emp2 joine0 before >/.
cE $he total remuneration DSalQcomm.E of all sales person of sales
0ept belon5s to emp2 table.
0E 1ny 5ra0e 2 emps sal of emp2 table
eE 1ny emp sal of emp4 table
SQL> Select * from emp Nhere
sal in Dselect sal from emp1 9here job : ;#L)G;E or
sal in Dselect sal from emp2 9here hire0ate > ;01@'1&@>/; E or
sal in Dselect salQ0eco0eDcomm3null30E from
emp230ept 9here 0name:;S1LS; an0 emp2.0eptno:0ept.0eptnoE or
sal in Dselect sal from emp23sal5ra0e 9here 5ra0e:2E or
sal in Dselect sal from emp4E ;
Select * from emp Nhere sal in Dselect sal from emp1
9here job : ;#L)G;E
union Dselect sal from emp2 9here hire0ate > ;01@'1&@>/; E
union Dselect salQ0eco0eDcomm3null30E from emp230ept 9here
0name:;S1LS; an0 emp2.0eptno:0ept.0eptnoE
union Dselect sal from emp23sal5ra0e 9here 5ra0e:2E
union Dselect sal from emp4E ;
+%T%&+ ))*) O 7Query blocF has incorrect no. of result columns.7
2/0. List the hi5hest pai0 emp
SQL> Select * from emp 9here sal : Dselect maBDsalE from empE ;
2/1. List the 0etails of most recently hire0 emp of 0ept /0.
SQL> Select * from emp 9here hire0ate : Dselect maBDhire0ateE from emp
9here 0eptno : /0E an0 0eptno : /0;
2/2. List the hi5hest pai0 emp of #hica5o joine0 before the most recently
hire0 emp of 5ra0e 2.
SQL> select emp.*3loc from emp30ept3sal5ra0e 9here hire0ate ?
Dselect maBDhire0ateE from emp 9here hire0ate in
Dselect hire0ate from emp 9here hire0ate ?
Dselect maBDhire0ateE from emp3sal5ra0e30ept 9here
loc : ;#K%#1+*; an0 5ra0e:2 an0 sal bet9een losal an0 hisal
an0
emp.0eptno : 0ept.0eptnoEEE an0 loc : ;#K%#1+*; an0 5ra0e:2
an0
emp.0eptno : 0ept.0eptno an0 sal bet9een losal an0 hisal
*)
select emp.*3loc from emp30ept 9here hire0ate ?
Dselect maBDhire0ateE from emp 9here hire0ate in
Dselect hire0ate from emp 9here hire0ate ?
Dselect maBDhire0ateE from emp3sal5ra0e30ept 9here
loc : ;#K%#1+*; an0 5ra0e:2 an0 sal bet9een losal an0 hisal an0
emp.0eptno : 0ept.0eptnoEEE an0 loc : ;#K%#1+*; an0
emp.0eptno : 0ept.0eptno
S"&$ 20 !%&,$S *& $K 1-*T Q,).
2//. List the hi5hest pai0 emp 9orFin5 un0er Fin5.
SQL> Select * from emp 9here sal : Dselect maBDsalE from emp 9here
m5r:Dselect empno from emp 9here ename:;G%&+;EE ;
E%& O" ?OO@ <UERIES
2/2. Lis( ()e se7on6 )ig)es( 34i6 e'3
SQL> Select * from emp 9here sal : Dselect maBDsalE from emp 9here sal in
Dselect sal from emp 9here sal ? Dselect maBDsalE from empEEE ;
2/4. (in0 the ol0est 0ate bet9een current 0ate an0 701C'1&@>/8.
SQL> select 5reatestDsys0ate3;01@'1&@>/;E from 0ual ;

Das könnte Ihnen auch gefallen