Anda di halaman 1dari 15

SQL QUERIES 1) 2) 3) 4) 5)

Display the details of all employees SQL>Select * from emp; Display the depart information from department table SQL>select * from dept; Display the name and job for all the employees SQL>select ename,job from emp; 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, $%*(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 SQL>select emame from emp &here deptno)$0;

6) 7)
$0#

8)

Display the names of all the employees &ho are &or'in( as cler's and dra&in( a salary more than *000# SQL>select ename from emp &here job)+,L-./+ and sal>*000; Display the employee n"mber and name &ho are earnin( comm# SQL>select empno,ename from emp &here comm is not n"ll;

9)

10) 11)

Display the employee n"mber and name &ho do not earn any comm# SQL>select empno,ename from emp &here comm is n"ll; Display the names of employees &ho are &or'in( as cler's,salesman or analyst and dra&in( a salary more than *000# SQL>select ename from emp &here job)+,L-./+ 0. 102)+S3L-S435+ 0. 102)+353L6S7+ 35D S3L>*000;

12)

Display the names of the employees &ho are &or'in( in the company for the past 8 years; SQL>select ename from emp &here to9char(sysdate,+6666+ : to9char(hiredate,+6666+ >)8;

13)

Display the list of employees &ho have joined the company before *0:1;5:<0 or after *$:D-,:<0# a select ename from emp &here hiredate = +*0:1;5:$<<0+ or hiredate > +*$:D-,:<0+;

14)

Display c"rrent Date#

SQL>select sysdate from d"al;

15) 16) 17) 18)

Display the list of all "sers in yo"r database("se catalo( table # SQL>select "sername from all9"sers; Display the names of all tables from c"rrent "ser; SQL>select tname from tab; Display the name of the c"rrent "ser# SQL>sho& "ser Display the names of employees &or'in( in depart n"mber $0 or %0 or >0 or employees &or'in( as ,L-./S,S3L-S435 or 353L6S7# SQL>select ename from emp &here deptno in($0,%0,>0 or job in(+,L-./S+,+S3L-S435+,+353L6S7+ ;

19) 20)

Display the names of employees &hose name starts &ith alaphabet S# SQL>select ename from emp &here ename li'e +S?+; Display the -mployee names for employees &hose name ends &ith alaphabet S# SQL>select ename from emp &here ename li'e +?S+;

21)

Display the names of employees &hose names have second alphabet 3 in their names# SQL>select ename from emp &here ename li'e +93?+;

22)

select the names of the employee &hose names is e@actly five characters in len(th# SQL>select ename from emp &here len(th(ename )8;

23) 24)

Display the names of the employee &ho are not &or'in( as 4353A-.S# SQL>select ename from emp &here job not in(+4353A-.+ ; Display the names of the employee &ho are not &or'in( as S3L-S435 0. ,L-./ 0. 353L6S7# SQL>select ename from emp &here job not in(+S3L-S435+,+,L-./+,+353L6S7+ ;

25)

Display all ro&s from emp table#7he system sho"ld &ait after every screen f"ll of informaction# SQL>set pa"se on

26) 27) 28)

Display the total n"mber of employee &or'in( in the company# SQL>select co"nt(* from emp; Display the total salary bei(in( paid to all employees# SQL>select s"m(sal from emp; Display the ma@im"m salary from emp table#

SQL>select ma@(sal

from emp;

29) 30) 31) 32) 33) 34) 35)

Display the minim"m salary from emp table# SQL>select min(sal from emp; Display the avera(e salary from emp table# SQL>select av((sal from emp; Display the ma@im"m salary bein( paid to ,L-./# SQL>select ma@(sal from emp &here job)+,L-./+; Display the ma@im"m salary bein( paid to depart n"mber %0# SQL>select ma@(sal from emp &here deptno)%0; Display the minim"m salary bein( paid to any S3L-S435# SQL>select min(sal from emp &here job)+S3L-S435+; Display the avera(e salary dra&n by 4353A-.S# SQL>select av((sal from emp &here job)+4353A-.+; Display the total salary dra&n by 353L6S7 &or'in( in depart n"mber >0# SQL>select s"m(sal from emp &here job)+353L6S7+ and deptno)>0;

36) Display the names of the employee in order of salary i.e the name of the employee earning lowest salary sho"ld salary appear first#
SQL>select ename from emp order by sal;

37) 38)

Display the names of the employee in descendin( order of salary# a select ename from emp order by sal desc; Display the names of the employee in order of employee name# a select ename from emp order by ename;

39) Display empno ename deptno sal sort the o!tp!t first "ase on name and within name "y deptno and with in deptno by sal#
SQL>select empno,ename,deptno,sal from emp order by

40)

Display the name of the employee alon( &ith their ann"al salary(sal*$% #7he name of the employee earnin( hi(hest ann"al salary sho"ld apper first# SQL>select ename,sal*$% from emp order by sal desc;

41) Display name salary hra pf da total salary for ea#h employee. $he o!tp!t sho!ld "e in the order of total salary hra 15% of salary da 10% of salary pf 5%

salary total salary will "e&salary'hra'da)(pf. )*+,sele#t ename sal sal-100.15 as hra sal-100.5 as pf sal-100.10 as da sal+salB$00*$8+salB$00*$0:salB$00*8 as total from emp; 42)
Display depart n"mbers and total n"mber of employees &or'in( in each department# SQL>select deptno,co"nt(deptno from emp (ro"p by deptno;

43)

Display the vario"s jobs and total n"mber of employees &ithin each job (ro"p# SQL>select job,co"nt(job from emp (ro"p by job;

44) 45) 46) 47) 48)

Display the depart n"mbers and total salary for each department# SQL>select deptno,s"m(sal from emp (ro"p by deptno;

Display the depart n"mbers and ma@ salary for each department# SQL>select deptno,ma@(sal from emp (ro"p by deptno; Display the vario"s jobs and total salary for each job SQL>select job,s"m(sal from emp (ro"p by job; Display the vario"s jobs and total salary for each job SQL>select job,min(sal from emp (ro"p by job; Display the depart n"mbers &ith more than three employees in each dept# SQL>select deptno,co"nt(deptno from emp (ro"p by deptno havin( co"nt(* >*;

49)

Display the vario"s jobs alon( &ith total salary for each of the jobs &here total salary is (reater than >0000# SQL>select job,s"m(sal from emp (ro"p by job havin( s"m(sal >>0000;

50)

Display the vario"s jobs alon( &ith total n"mber of employees in each job#7he o"tp"t sho"ld contain only those jobs &ith more than three employees# SQL>select job,co"nt(empno from emp (ro"p by job havin( co"nt(job >*

51) 52)

Display the name of the empployee &ho earns hi(hest salary# SQL>select ename from emp &here sal)(select ma@(sal from emp ; Display the employee n"mber and name for employee &or'in( as cler' and earnin( hi(hest salary amon( cler's# SQL>select empno,ename from emp &here &here job)+,L-./+ and sal)(select ma@(sal from emp &here job)+,L-./+ ;

53)

Display the names of salesman &ho earns a salary more than the hi(hest salary of any cler'# SQL>select ename,sal from emp &here job)+S3L-S435+ and sal>(select ma@(sal from emp &here job)+,L-./+ ;

54) Display the names of #ler/s who earn a salary more than the lowest salary of any salesman. )*+,sele#t ename from emp where 0o"123+4562 and sal,&sele#t min&sal) from emp where 0o"12)7+4)8792): Display the names of employees who earn a salary more than that of ;ones or that of salary grether than that of scott#
SQL>select ename,sal from emp &here sal> (select sal from emp &here ename)+105-S+ and sal> (select sal from emp &here ename)+S,077+ ;

55)

Display the names of the employees &ho earn hi(hest salary in their respective departments# SQL>select ename,sal,deptno from emp &here sal in(select ma@(sal from emp (ro"p by deptno ;

56)

Display the names of the employees &ho earn hi(hest salaries in their respective job (ro"ps# SQL>select ename,sal,job from emp &here sal in(select ma@(sal from emp (ro"p by job

57)

Display the employee names &ho are &or'in( in acco"ntin( department# SQL>select ename from emp &here deptno)(select deptno from dept &here dname)+3,,0;57C5A+

58)

Display the employee names &ho are &or'in( in ,hica(o# SQL>select ename from emp &here deptno)(select deptno from dept &here L0,)+,DC,3A0+

59)

Display the 1ob (ro"ps havin( total salary (reater than the ma@im"m salary for mana(ers# SQL>S-L-,7 102,S;4(S3L E.04 -4F A.0;F 26 102 D3GC5A S;4(S3L >(S-L-,7 43H(S3L E.04 -4F ID-.- 102)+4353A-.+ ;

60)

Display the names of employees from department n"mber $0 &ith salary (rether than that of any employee &or'in( in other department# SQL>select ename from emp &here deptno)$0 and sal>any(select sal from emp &here deptno not in $0 #

61)

Display the names of the employees from department n"mber $0 &ith salary (reater than that of all employee &or'in( in other departments# SQL>select ename from emp &here deptno)$0 and sal>all(select sal from emp &here deptno not in $0 #

62)

Display the names of the employees in ;ppercase# SQL>select "pper(ename from emp

63) 64) 65) 66) 67) 68)

Display the names of the employees in Lo&ecase# SQL>select lo&er(ename from emp Display the names of the employees in Fropercase# SQL>select initcap(ename from emp; Display the len(th of 6o"r name "sin( appropriate f"nction# SQL>select len(th(+name+ from d"al Display the len(th of all the employee names# SQL>select len(th(ename from emp; select name of the employee concatenate &ith employee n"mber# SQL>select enameJJempno from emp; ;ser appropriate f"nction and e@tract * characters startin( from % characters from the follo&in( strin( +0racle+# i#e the o"t p"t sho"ld be +ac+# SQL>select s"bstr(+oracle+,*,% from d"al

69)

Eind the Eirst occ"rance of character +a+ from the follo&in( strin( i#e +,omp"ter 4aintenance ,orporation+# SQL>S-L-,7 C5S7.(+,omp"ter 4aintenance ,orporation+,+a+,$ E.04 D;3L

70)

.eplace every occ"rance of alphabhet 3 &ith 2 in the strin( 3llens("se translate f"nction SQL>select translate(+3llens+,+3+,+2+ from d"al

71)

Display the informaction from emp table#Ihere job mana(er is fo"nd it sho"ld be displayed as boos(;se replace f"nction # SQL>select replace(102,+4353A-.+,+20SS+ E.04 -4F;

72)

Display empno,ename,deptno from emp table#Cnstead of display department n"mbers display the related department name(;se decode f"nction # SQL>select empno,ename,decode(deptno,$0,+3,,0;57C5A+,%0,+.-S-3.,D+,*0,+S3L-S+,>0,+0F.37C 05S+ from emp;

73) 74) 75) 76)

Display yo"r a(e in days# SQL>select to9date(sysdate :to9date(+$0:sep:KK+ from d"al Display yo"r a(e in months# SQL>select months9bet&een(sysdate,+$0:sep:KK+

from d"al

Display the c"rrent date as $8th 3"(est Eriday 5ineteen 5inety Saven# SQL>select to9char(sysdate,+ddth 4onth day year+ from d"al Display the follo&in( o"tp"t for each ro& from emp table#

scott has joined the company on &ednesday $*th 3"("st ninten nintey# SQL>select -534-JJ+ D3S 10C5-D 7D- ,04F356 05 +JJto9char(DC.-D37-,+day ddth 4onth year+ from -4F;

77) 78) 79) 80)

Eind the date for nearest sat"rday after c"rrent date# SQL>S-L-,7 5-H79D36(S6SD37-,+S37;.D36+ E.04 D;3L; Display c"rrent time# SQL>select to9char(sysdate,+hhL44Lss+

from d"al#

Display the date three months 2efore the c"rrent date# SQL>select add9months(sysdate,* from d"al; Display the common jobs from department n"mber $0 and %0# SQL>select job from emp &here deptno)$0 and job in(select job from emp &here deptno)%0 ;

81)

Display the jobs fo"nd in department $0 and %0 -liminate d"plicate jobs# SQL>select distinct(job from emp &here deptno)$0 or deptno)%0 (or SQL>select distinct(job from emp &here deptno in($0,%0 ;

82) 83)

Display the jobs &hich are "niM"e to department $0# SQL>select distinct(job from emp &here deptno)$0 Display the details of those &ho do not have any person &or'in( "nder them# SQL>select e#ename from emp,emp e &here emp#m(r)e#empno (ro"p by e#ename havin( co"nt(* )$;

84)

Display the details of those employees &ho are in sales department and (rade is *# SQL>select * from emp &here deptno)(select deptno from dept &here dname)+S3L-S+ and sal bet&een(select losal from sal(rade &here (rade)* and (select hisal from sal(rade &here (rade)* ;

85)

Display those &ho are not mana(ers and &ho are mana(ers any one# i display the mana(ers names SQL>select distinct(m#ename from emp e,emp m &here m#empno)e#m(r; ii display the &ho are not mana(ers SQL>select ename from emp &here ename not in(select distinct(m#ename from emp e,emp m &here m#empno)e#m(r ;

86) 87)

Display those employee &hose name contains not less than > characters# SQL>select ename from emp &here len(th(ename >>; Display those department &hose name start &ith !S! &hile the location name ends &ith !/!# SQL>select dname from dept &here dname li'e +S?+ and loc li'e +?/+;

88)

Display those employees &hose mana(er name is 105-S# SQL>select p#ename from emp e,emp p &here e#empno)p#m(r and e#ename)+105-S+;

89)

Display those employees &hose salary is more than *000 after (ivin( %0? increment# SQL>select ename,sal from emp &here (sal+sal*#% >*000;

90) 91)

Display all employees &hile their dept names; SQL>select ename,dname from emp,dept &here emp#deptno)dept#deptno Display ename &ho are &or'in( in sales dept# SQL>select ename from emp &here deptno)(select deptno from dept &here dname)+S3L-S+ ;

92)

Display employee name,deptname,salary and comm for those sal in bet&een %000 to 8000 &hile location is chica(o# SQL>select ename,dname,sal,comm from emp,dept &here sal bet&een %000 and 8000 and loc)+,DC,3A0+ and emp#deptno)dept#deptno;

93)Display 94)

those employees &hose salary (reter than his mana(er salary# SQL>select p#ename from emp e,emp p &here e#empno)p#m(r and p#sal>e#sal Display those employees &ho are &or'in( in the same dept &here his mana(er is &or'# SQL>select p#ename from emp e,emp p &here e#empno)p#m(r and p#deptno)e#deptno;

95)

Display those employees &ho are not &or'in( "nder any mana(er# SQL>select ename from emp &here m(r is n"ll

96) Display grade and employees name for the dept no 10 or 30 "!t grade is not 4 while 0oined the #ompany "efore 31(de#(82. )*+,sele#t ename grade from emp salgrade where sal "etween losal and hisal and deptno in($0,*0 and (rade=>> and hiredate=+*$:D-,:N%+; 97)
;pdate the salary of each employee by $0? increment &ho are not eli(ibl& for commission# SQL>"pdate emp set sal)sal+sal*$0B$00 &here comm is n"ll;

98) )4+43$ those employee who 0oined the #ompany "efore 31( de#(82 while their dept lo#ation is newyor/ or ,hica(o#
SQL>S-L-,7 -4F50,-534-,DC.-D37-,D534-,L0, E.04 -4F,D-F7 ID-.- (-4F#D-F750)D-F7#D-F750 35D DC.-D37- =+*$:D-,:N%+ 35D D-F7#L0, C5(+,DC,3A0+,+5-I 60./+ ;

99) D<)=+7> 48=+?>44 9784 ;?@ D4=75$849$ +?37$<?9 A?5 7++ BC? 754 B?56<9D 7) 4353A-.O
SQL>select ename,102,D534-,L0,37C05 from emp,D-F7 &here m(r is not n"ll;

100)

DCSFL36 7D0S- -4FL06--S ID0S- 4353A-. 534- CS 105-SO :: P35D 3LS0 DCSFL36 7D-C. 4353A-. 534-QO SQL> S-L-,7 F#-534- E.04 -4F -, -4F F ID-.- -#-4F50)F#4A. 35D -#-534-)+105-S+;

101)

Display name and salary of ford if his salary is eM"al to hisal of his (rade a select ename,sal,(rade from emp,sal(rade &here sal bet&een losal and hisal and ename )+E0.D+ 35D DCS3L)S3L;

102)

Display employee name,job,depart name ,mana(er name,his (rade and ma'e o"t an "nder department &iseO SQL>S-L-,7 -#-534-,-#102,D534-,-4F#-534-,A.3D- E.04 -4F,-4F -,S3LA.3D-,D-F7 ID-.- -4F#S3L 2-7I--5 L0S3L 35D DCS3L 35D -4F#-4F50)-#4A. 35D -4F#D-F750)D-F7#D-F750 0.D-. 26 D534-

103) +ist o!t all employees name 0o" salary grade and depart name for eEery one in the #ompany e@cept +,L-./+#Sort on salary display the hi(hest
salaryO SQL>S-L-,7 -534-,102,D534-,S3L,A.3D- E.04 -4F,S3LA.3D-,D-F7 ID-.S3L 2-7I--5 L0S3L 35D DCS3L 35D -4F#D-F750)D-F7#D-F750 35D 102 507 C5(+,L-./+ 0.D-. 26 S3L 3S,;

104)

Display the employee name,job and his mana(er#Display also employee &ho are &itho"t mana(erO SQL>select e#ename,e#job,e4F#ename 3S 4ana(er from emp,emp e &here emp#empno(+ )e#m(r

105)

Eind o"t the top 8 earners of companyO SQL>S-L-,7 DCS7C5,7 S3L E.04 -4F - ID-.- 8>)(S-L-,7 ,0;57(DCS7C5,7 S3L E.04 -4F 3 ID-.- 3#S3L>)-#S3L 0.D-. 26 S3L D-S,;

106) 107)

Display name of those employee &ho are (ettin( the hi(hest salaryO SQL>select ename from emp &here sal)(select ma@(sal from emp ; Display those employee &hose salary is eM"al to avera(e of ma@im"m and minim"mO SQL>select ename from emp &here sal)(select ma@(sal +min(sal B% from emp ;

108)

Select co"nt of employee in each department &here co"nt (reater than *O SQL>select co"nt(* from emp (ro"p by deptno havin( co"nt(deptno >*

109)

Display dname &here at least * are &or'in( and display only department nameO SQL>select distinct d#dname from dept d,emp e &here d#deptno)e#deptno and *>any (select co"nt(deptno from emp (ro"p by deptno

110)

Display name of those mana(ers name &hose salary is more than avera(e salary of his companyO SQL>S-L-,7 -#-534-,-4F#-534- E.04 -4F,-4F ID-.- -4F#-4F50)-#4A. 35D -#S3L>(S-L-,7 3GA(S3L E.04 -4F ;

111)Display

those mana(ers name &hose salary is more than avera(e salary of his employeeO SQL>S-L-,7 DCS7C5,7 -4F#-534- E.04 -4F,-4F - ID-.-#S3L =(S-L-,7 3GA(-4F#S3L E.04 -4F ID-.- -4F#-4F50)-#4A. A.0;F 26 -4F#-534- 35D -4F#-4F50)-#4A.;

112)

Display employee name,sal,comm and net &hose net pay is (reter than or eM"al to any the companyO SQL>select ename,sal,comm,sal+nvl(comm,0 as &here sal+nvl(comm,0 >any (select

pay for those employee other employee salary of 5etFay from emp sal from emp

113)

Display all employees names &ith total sal of company &ith each employee nameO SQL>S-L-,7 -534-,(S-L-,7 S;4(S3L E.04 -4F E.04 -4F;

114)

Eind o"t last 8(least earners of the company#O SQL>S-L-,7 DCS7C5,7 S3L E.04 -4F - ID-.8>)(S-L-,7 ,0;57(DCS7C5,7 S3L E.04 -4F 3 ID-.3#S3L=)-#S3L 0.D-. 26 S3L D-S,;

115)

Eind o"t the n"mber of employees &hose salary is (reater than their mana(er salaryO SQL>S-L-,7 -#-534- E.04 -4F ,-4F - ID-.- -4F#-4F50)-#4A. 35D -4F#S3L=-#S3L;

116) 117) 118)

Display those department &here no employee &or'in(O SQL>select dname from emp,dept &here emp#deptno not in(emp#deptno Display those employee &hose salary is 0DD val"eO SQL>select * from emp &here sal=0; Display those employee &hose salary contains alleast * di(itsO SQL>select * from emp &here len(th(sal >)*;

119) 120)

Display those employee &ho joined in the company in the month of DecO SQL>select ename from emp &here to9char(hiredate,+405+ )+D-,+; Display those employees &hose name contains !3!O SQL>select ename from emp &here instr(ename,+3+ >0; or SQL>select ename from emp &here ename li'e(+?3?+ ;

121) 122)

Display those employee &hose deptno is available in salaryO SQL>select emp#ename from emp, emp e &here emp#sal)e#deptno; Display those employee &hose first % characters from hiredate :last % characters of salaryO SQL>select ename,S;2S7.(hiredate,$,% JJ-534-JJs"bstr(sal,:%,% from emp

123)

Display those employee &hose $0? of salary is eM"al to the year of joinin(O SQL>select ename from emp &here to9char(hiredate,+66+ )sal*0#$;

124)

Display those employee &ho are &or'in( in sales or researchO SQL>S-L-,7 -534- E.04 -4F ID-.- D-F750 C5(S-L-,7 D-F750 E.04 D-F7 ID-.D534- C5(+S3L-S+,+.-S-3.,D+ ;

125)

Display the (rade of jonesO SQL>S-L-,7 -534-,A.3D- E.04 -4F,S3LA.3DID-.- S3L 2-7I--5 L0S3L 35D DCS3L 35D -name)+105-S+;

126) 127) 128)

Display those employees &ho joined the company before $8 of the monthO a select ename from emp &here to9char(hiredate,+DD+ =$8; Display those employee &ho has joined before $8th of the month# a select ename from emp &here to9char(hiredate,+DD+ =$8; Delete those records &here no of employees in a partic"lar department is less than *# SQL>delete from emp &here deptno)(select deptno from emp (ro"p by deptno havin( co"nt(deptno =* ;

129)

Display the name of the department &here no employee &or'in(# SQL> S-L-,7 -#-534-,-#102,4#-534-,4#102 E.04 -4F -,-4F 4 ID-.- -#4A.)4#-4F50

130)

Display those employees &ho are &or'in( as mana(er# SQL>S-L-,7 4#-534- 4353A-. E.04 -4F 4 ,-4F ID-.- -#4A.)4#-4F50 A.0;F 26 4#-534-

131)

Display those employees &hose (rade is eM"al to any n"mber of sal b"t not eM"al to first n"mber of salO SQL> S-L-,7 -534-,A.3D- E.04 -4F,S3LA.3DID-.- A.3D- 507 C5(S-L-,7 S;2S7.(S3L,0,$ E.04 -4F

132)

Frint the details of all the employees &ho are S"b:ordinate to 2L3/-O SQL>select emp#ename from emp, emp e &here emp#m(r)e#empno and e#ename)+2L3/-+;

133)

Display employee name and his salary &hose salary is (reater than hi(hest avera(e of department n"mberO SQL>S-L-,7 S3L E.04 -4F ID-.- S3L>(S-L-,7 43H(3GA(S3L E.04 -4F A.0;F 26 D-F750 ;

134)

Display the $0th record of emp table(&itho"t "sin( ro&id SQL>S-L-,7 * E.04 -4F ID-.- .0I5;4=$$ 4C5;S S-L-,7 * E.04 -4F ID-.- .0I5;4=$0

135)

Display the half of the ename+s in "pper case and remainin( lo&ercaseO SQL>S-L-,7 S;2S7.(L0I-.(-534- ,$,* JJS;2S7.(;FF-.(-534- ,*,L-5A7D(-534E.04 -4F;

136)

Display the $0th record of emp table &itho"t "sin( (ro"p by and ro&idO SQL>S-L-,7 * E.04 -4F ID-.- .0I5;4=$$ 4C5;S S-L-,7 * E.04 -4F ID-.- .0I5;4=$0 Delete the $0th record of emp table# SQL>D-L-7- E.04 -4F ID-.- -4F50)(S-L-,7 -4F50 E.04 -4F ID-.- .0I5;4=$$ 4C5;S S-L-,7 -4F50 E.04 -4F ID-.- .0I5;4=$0

137) 138) 139) 140)

,reate a copy of emp table; SQL>create table ne&9table as select * from emp &here $)%; Select ename if ename e@ists more than once# SQL>select ename from emp e (ro"p by ename havin( co"nt(* >$; Display all enames in reverse orderO(S4C7DLD7C4S # SQL>S-L-,7 .-G-.S-(-534- E.04 -4F; Display those employee &hose joinin( of month and (rade is eM"al# SQL>S-L-,7 -534- E.04 -4F ID-.- S3L 2-7I--5 (S-L-,7 L0S3L E.04 S3LA.3D- ID-.A.3D-)709,D3.(DC.-D37-,+44+ 35D (S-L-,7 DCS3L E.04 S3LA.3D- ID-.A.3D-)709,D3.(DC.-D37-,+44+ ;

141) 142)

Display those employee &hose joinin( D37- is available in deptno# SQL>S-L-,7 -534- E.04 -4F ID-.- 709,D3.(DC.-D37-,+DD+ )D-F750 Display those employees name as follo&s 3 3LL-5 2 2L3/-

SQL> S-L-,7 S;2S7.(-534-,$,$ ,-534- E.04 -4F;

143)

List o"t the employees ename,sal,FE(%0? 0E S3L SQL>S-L-,7 -534-,S3L,S3L*#% 3S FE E.04 -4F;

from emp;

144) 145) 146) 147) 148) 149)

,reate table emp &ith only one col"mn empno; SQL>,reate table emp as select empno from emp &here $)%; 3dd this col"mn to emp table ename vrachar%(%0 # SQL>alter table emp add(ename varchar%(%0 ; 0ops C for(ot (ive the primary 'ey constraint# SQL>alter table emp add primary 'ey(empno ; 3dd in no&#

5o& increase the len(th of ename col"mn to *0 characters# SQL>alter table emp modify(ename varchar%(*0 ; 3dd salary col"mn to emp table# SQL>alter table emp add(sal n"mber($0

C &ant to (ive a validation sayin( that salary cannot be (reater $0,000 (note (ive a name to this constraint SQL>alter table emp add constraint ch'900$ chec'(sal=)$0000

150)

Eor the time bein( C have decided that C &ill not impose this validation#4y boss has a(reed to pay more than $0,000# SQL>a(ain alter the table or drop constraint &ith alter table emp drop constraint ch'900$ (or Disable the constraint by "sin( alter table emp modify constraint ch'900$ disable;

151)

4y boss has chan(ed his mind# 5o& he doesn+t &ant to pay more than $0,000#so revo'e that salary constraint# SQL>alter table emp modify constraint ch'900$ enable;

152) 153)

3dd col"mn called as m(r to yo"r emp table; SQL>alter table emp add(m(r n"mber(8 ; 0hR 7his col"mn sho"ld be related to empno# Aive a command to add this constraint# SQL>3L7-. 732L- -4F 3DD ,05S7.3C57 4A.9D-F7 E0.-CA5 /-6(4A. .-E-.-5,-S -4F(-4F50

154)

3dd deptno col"mn to yo"r emp table; SQL>alter table emp add(deptno n"mber(8 ;

155) $his deptno #ol!mn sho!ld "e related to deptno #ol!mn of dept ta"le: )*+,alter ta"le emp add #onstraint deptF001 foreign /ey&deptno)

referen#e dept&deptno)
Pdeptno sho"ld be primary 'eyQ

156)

Aive the command to add the constraint# SQL>alter table =table9name add constraint =constraint9name> =constraint type>

157)

,reate table called as ne&emp# ;sin( sin(le command create this table as &ell as (et data into this table("se create table as ; SQL>create table ne&emp as select * from emp; SQL>,reate table called as ne&emp# 7his table sho"ld contain only empno,ename,dname# SQL>create table ne&emp as select empno,ename,dname from emp,dept &here $)%;

158)

Delete the ro&s of employees &ho are &or'in( in the company for more than % years# SQL>delete from emp &here (sysdate:hiredate B*S8>%;

159)

Frovide a commission($0? ,omm 0f Sal to employees &ho are not earnin( any commission# SQL>select sal*0#$ from emp &here comm is n"ll

160)

Cf any employee has commission his commission sho"ld be incremented by $0? of his salary# SQL>"pdate emp set comm)sal*#$ &here comm is not n"ll;

161)

Display employee name and department name for each employee# SQL>select empno,dname from emp,dept &here emp#deptno)dept#deptno

162)Display

employee n"mber,name and location of the department in &hich he is &or'in(# SQL>select empno,ename,loc,dname from emp,dept &here emp#deptno)dept#deptno;

163)

Display ename,dname even if there are no employees &or'in( in a partic"lar department("se o"ter join # SQL>select ename,dname from emp,dept &here emp#deptno)dept#deptno(+

164) 165)

Display employee name and his mana(er name# SQL>select p#ename,e#ename from emp e,emp p &here e#empno)p#m(r; Display the department name and total n"mber of employees in each department# SQL>select dname,co"nt(ename from emp,dept &here emp#deptno)dept#deptno (ro"p by dname;

166)Display

the department name alon( &ith total salary in each department# SQL>select dname,s"m(sal from emp,dept &here emp#deptno)dept#deptno (ro"p by dname;

167) 168)

Display itemname and total sales amo"nt for each item# SQL>select itemname,s"m(amo"nt from item (ro"p by itemname; Irite a Q"ery 7o Delete 7he .epeted .o&s from emp table; SQL>Delete from emp &here ro&id not in(select min(ro&id from emp (ro"p by ename

169)

70 DCSFL36 8 70 K .0IS E.04 3 732LSQL>select ename from emp &here ro&id in(select ro&id from emp &here ro&n"m=)K min"s select ro&id from empi &here ro&n"m=8

170)

DCSFL36

70F 5 .0IS E.04 732L-O

SQL>S-L-,7 * E.04 (S-L-,7 * E.04 -4F 0.D-. 26 -534- D-S, ID-.- .0I5;4 =$0;

171)

DCSFL36 70F * S3L3.C-S E.04 -4F; SQL>S-L-,7 S3L E.04 ( S-L-,7 * E.04 -4F 0.D-. ID-.- .0I5;4 =>

26 S3L D-S,

172)

DCSFL36 <th E.04 7D- -4F 732L-O SQL>S-L-,7 -534- E.04 -4F ID-.- .0ICD)(S-L-,7 .0ICD E.04 -4F ID-.- .0I5;4=)$0 4C5;S S-L-,7 .0ICD E.04 -4F ID-.- .0I5;4 =$0 select second ma@ salary from emp; select ma@(sal fromemp &here sal=(select ma@(sal from emp ;

((((((......((((((

Anda mungkin juga menyukai