You are on page 1of 46

Solutions To Workshop Exercises

Chapter Chapter Chapter Chapter Chapter Chapter Chapter Chapter Chapter Chapter Chapter Chapter Chapter Chapter Chapter Chapter Chapter Chapter 1: SQL and Data...........................................................1 2: SQL: The Basics.......................................................3 3: The WHERE and ORDER BY Cla ses.............................! !: Character" # $%er" and &iscellane' s ( ncti'ns...........) *: Date and C'n+ersi'n ( ncti'ns..................................., ): -..re.ate ( ncti'ns" /RO01 BY and H-23#/.............12 4: E5 i6'ins...............................................................1* 7: S %5 eries............................................................28 ,: Set Operat'rs.........................................................23 18: C'$ple9 :'ins......................................................2) 11: 3nsert" 0pdate" and Delete.....................................38 12: Create" -lter" and Dr'p Ta%les................................33 13: 3nde9es" Se5 ences" and 2ie;s...............................3! 1!: The Data Dicti'nar<" Scriptin." and Rep'rtin............3) 1*: Sec rit<...............................................................37 1): Re. lar E9pressi'ns and Hierarchical Q eries............!8 14: E9pl'rin. Data Wareh' sin. (eat res......................!2 17: SQL Opti$i=ati'n..................................................!!

Chapter 1: SQL and Data


3n this chapter" <' learned a%' t data" h'; data is 'r.ani=ed in ta%les" and h'; the relati'nships a$'n. the ta%les are depicted in a sche$a dia.ra$. Based 'n <' r ne;l< ac5 ired >n';led.e" desi.n a sche$a dia.ra$ %ased 'n the ?icti'nal -C&E C'nstr cti'n C'$pan<. Dra; 'n <' r ';n ;'r> e9perience t' desi.n the ?'ll';in. c'$p'nents. 1. Dra; %'9es ?'r these three ta%les: E&1LOYEE" 1OS3T3O#" and DE1-RT&E#T. Solution: See the s'l ti'n ?'r E9ercise 3. 2. Create at least three c'l $ns ?'r each '? the ta%les and desi.nate a pri$ar< >e< ?'r each ta%le.
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e ( o) *+
,ersion -ate *!.!20(*

Solution: See the s'l ti'n ?'r E9ercise 3. 3. Create relati'nships a$'n. the ta%les that $a>e sense t' <' . -t least 'ne ta%le sh' ld ha+e a sel?@re?erencin. relati'nship. Hint: Be s re t' incl de the necessar< ?'rei.n >e< c'l $ns. Solution:

!. Thin> a%' t ;hich c'l $ns sh' ld n't all'; #0LL +al es. Solution: B< de?initi'n all the pri$ar< >e< c'l $ns d' n't all'; n ll +al es. 3n the DE1-RT&E#T ta%le the DE1-RT&E#TA#-&E c'l $n sh' ld pr'%a%l< n't all'; n ll +al es. 3n the E&1LOYEE ta%le the (3RSTA#-&E and L-STA#-&E c'l $ns are t;' $'re candidates ?'r #OT #0LL c'l $ns %eca se all e$pl'<ees sh' ld ha+e na$es. The ?'rei.n >e< c'l $ns DE1-RT&E#TA3D and 1OS3T3O#A3D $ st %e #OT #0LL as the relati'nships in the a%'+e dia.ra$ indicates. The dia.ra$ states that ?'r an indi+id al r'; in the E&1LOYEE ta%le al;a<s a r'; $ st e9ist in the 1OS3T3O# ta%le and the DE1-RT&E#T ta%le. The &-#-/ERA3D c'l $n 'n the 'ther hand $ st all'; n lls as indicated ;ith the 'pti'nal rec rsi+e relati'nship. 3? this ;as n't an 'pti'nal relati'nship" <' ;' ld n't %e a%le t' enter the president '? c'$pan< ?'r instance" %eca se it re5 ires an e9istin. entr< ?'r the
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e 2 o) *+
,ersion -ate *!.!20(*

1residentBs $ana.er. There?'re" the t'p '? the 'r. chart hierarch< Ce..." the presidentD has a n ll +al e in the &-#-/ERA3D c'l $n. 3t ;' ld %e ;ise t' n't all'; n ll +al es ?'r the DESCR31T3O# c'l $n '? the 1OS3T3O# ta%le as a descripti'n sh' ld al;a<s %e entered ;hen a p'siti'n is created.

Chapter 2: SQL: The Basics


1. 0se SQL De+el'per t' retrie+e all the ST-TE +al es ?r'$ the E31CODE ta%le" ;ith' t repeatin. the +al es. S'l ti'n: The 5 er< retrie+es 18 distinct state +al es.

2. Recall 'ne '? the state$ents <' sed in La% 2.1" sin. the SQL Hist'r< ta%. S'l ti'n: The SQL c'$$ands are sa+ed e+en a?ter <' e9it SQL De+el'per and <' se the SQL Hist'r< ta% t' retrie+e the state$ent. 3? the ta% is n't +isi%le" <' can clic> 'n 2ie;" then SQL Hist'r< 'r press (7.

3. What happens i? <' tr< t' l'.'n t' SQLF1l s ;ith the ppercase +ersi'n '? the pass;'rd learnG
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e / o) *+
,ersion -ate *!.!20(*

S'l ti'n: Y' ;ill see an err'r $essa.e indicatin. that the pass;'rd is in+alid. B< de?a lt" the Oracle data%ase 11. has case@sensiti+e pass;'rds.

!. E9ec te the ?'ll';in. state$ents in SQLF1l s and rec'rd <' r '%ser+ati'ns.


SET NULL 'NULL' SELECT DISTINCT cost FROM course

S'l ti'n: The SET #0LL SQLF1l s c'$$and s %stit tes the displa< '? an< n ll +al es ;ith an'ther +al e" s ch as the literal Hn llI in this e9a$ple. The c'l $nIs n ll +al e is n't chan.ed in the data%aseJ this c'$$and 'nl< chan.es the SQLF1l s displa< +al e.

P-

Chapter 3: The WHE E and ! DE Clauses

B"

1. Create a SQL state$ent that retrie+es data ?r'$ the CO0RSE ta%le ?'r c' rses that c'st 11,* and ;h'se descripti'ns start ;ith 3ntr'" s'rted %< their prere5 isites.
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e * o) *+
,ersion -ate *!.!20(*

S'l ti'n: SELECT FROM WHERE AND ORDER * course cost = 1195 description like 'Intro%' BY prerequisite;

2. Create an'ther SQL State$ent that retrie+es data ?r'$ the ST0DE#T ta%le ?'r st dents ;h'se last na$es %e.in ;ith -"B" 'r C" and ;h' ;'r> ?'r C'$petr'l Real Estate" s'rted %< their last na$es. S'l ti'n: SELECT * FROM student WHERE (last_name like 'A%' OR last_name like 'B%' OR last_name like 'C%') A ! em"l#$e% & 'C#m"et%#l Real Estate'' 3. Write a SQL state$ent that retrie+es all the descripti'ns ?r'$ the /R-DEATY1E ta%le" ?'r r';s that ;ere $'di?ied %< the ser &C-((REY. S'l ti'n: SELECT des(%i"ti#n FROM )%ade_t$"e WHERE m#di*ied_+$ & 'MCAFFRE,'' !. Sa+e all three SQL state$ents in a ?ile called W'r>sh'pACh3.s5l S'l ti'n:

Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e 0 o) *+
,ersion -ate *!.!20(*

Chapter #: Character$ %u&'er$ and (iscellaneous )unctions


1. Write the SELECT state$ent that ret rns the ?'ll';in. ' tp t O E_L- E ............................................. -nst%u(t#%/ R0 C1#2000000 31#ne/ 454.666.5454 -nst%u(t#%/ M0 F%ant7en00 31#ne/ 454.666.5454 -nst%u(t#%/ F0 Hanks00000 31#ne/ 454.666.5454 -nst%u(t#%/ C0 L#2%$00000 31#ne/ 454.666.5454 -nst%u(t#%/ A0 M#%%is0000 31#ne/ 454.666.5454 -nst%u(t#%/ 80 3e%te70000 31#ne/ 454.666.5454 -nst%u(t#%/ 0 S(1#%in000 31#ne/ 454.666.5454 -nst%u(t#%/ T0 Sm$t1e0000 31#ne/ 454.666.5454 -nst%u(t#%/ -0 Willi)0000 31#ne/ 454.666.5454
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e + o) *+
,ersion -ate *!.!20(*

-nst%u(t#%/ T0 W#9i(k0000 31#ne/ 454.666.5454 S'l ti'n: SELECT R3A!('-nst%u(t#%/ ' ::S;BSTR(*i%st_name<5<5) ::'0 ' ::last_name< 46<'0') :: '31#ne/ ' :: S;BSTR("1#ne<5<=) ::'.' ::S;BSTR("1#ne< ><=) ::'.' ::S;BSTR("1#ne< ?) O E_L- E FROM inst%u(t#% OR!ER B, last_name 2. Re;rite the ?'ll';in. 5 er< t' replace all 'cc rrences '? the strin. 0ni9 ;ith Lin 9. SELECT '- de@el#" s#*t2a%e #n t1e ;niA "lat*#%m' FROM dual S'l ti'n: SELECT RE3LACE('- de@el#" s#*t2a%e #n t1e ;niA "lat*#%m'< ';niA'< 'LinuA') FROM dual REPLACE('IDEVELOPSOFTWAREONTHEUNIXPLATFO ........................................ I develop softw !e o" t#e L$"%& pl tfo!' ( !ow sele)ted* 3. Deter$ine ;hich st dent d'es n't ha+e the ?irst letter '? her 'r his last na$e capitali=ed. Sh'; the ST0DE#TA3D and L-STA#-&E c'l $ns. S'l ti'n: SELECT student_id< last_name FROM student WHERE S;BSTR(last_name<5<5) & S;BSTR(LOWER(last_name)<5<5) STUDENT+ID LAST+NA,E .......... ............. -./ ""%"0$ to
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e 1 o) *+
,ersion -ate *!.!20(*

( !ow sele)ted* 3n Chapter 1)" KRe. lar E9pressi'ns and Hierarchical Q eries"L <' ;ill learn a%' t re. lar e9pressi'ns ;hich can acc'$plish the sa$e res lt. !. Chec> ;hether an< '? the ph'ne n $%ers in the 3#STR0CTOR ta%le ha+e %een entered in the CMMMDMMM@MMMM ?'r$at. S'l ti'n: SELECT "1#ne FROM inst%u(t#% WHERE TRA SLATE( "1#ne< 'B54=>6C?DE'< 'FFFFFFFFFF') & '(FFF)FFF.FFFF' "o !ows sele)ted Y' can 'pti'nall< incl de an e9tra space a?ter the B,B" ;hich ;ill i.n're an< e9tra spaces added t' the ph'ne n $%er. ('r e9a$ple" n $%ers in the ?'r$at C212D ***@1111 and C212D***@1111 ;' ld %e listed in the res lt set. 3n the case '? the 3#STR0CTOR ta%le" all the ph'ne n $%ers d' n't ?'ll'; this ?'r$atJ there?'re n' r';s are sh';n in the ' tp t. SELECT "1#ne< inst%u(t#%_id FROM inst%u(t#% WHERE TRA SLATE( "1#ne< 'B54=>6C?DE '< 'FFFFFFFFFF') & '(FFF)FFF.FFFF' "o !ows sele)ted Re?er t' Chapter 1)" KRe. lar E9pressi'ns and Hierarchical Q eries"L ?'r $'re ad+anced ?'r$at chec>in. sin. re. lar e9pressi'ns. *. E9plain the ? ncti'nalit< '? the ?'ll';in. 5 er<. SELECT se(ti#n_id< (a"a(it$< CASE WHE MO! ((a"a(it$< 4) GH B THE ELSE 'E@en (a"a(it$' E ! IOdd #% E@enI FROM se(ti#n WHERE se(ti#n_id - (5B5< 5>C< 5>?)

'Odd (a"a(it$'

S'l ti'n: The 5 er< sh';s ?'r secti'ns 181" 1!)" and 1!4 the SECT3O#A3D and C-1-C3TY in the ?irst t;' c'l $ns. The last c'l $n displa<s either the ;'rds BE+en capacit<B 'r BOdd capacit<B dependin. 'n the +al e '? the capacit<.
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e . o) *+
,ersion -ate *!.!20(*

SECTION+ID CAPACIT1 Odd o! Eve" 2222222222 2222222222 2222222222222 (.( (. Eve" ) p )$t3 (4/ -5 Odd ) p )$t3 (46 (5 Odd ) p )$t3 7 !ows sele)ted*

Chapter *: Date and Con+ersion )unctions


1. Displa< all the secti'ns ;here classes start at 18:38 -&. S'l ti'n: SELECT se(ti#n_id< TO_CHAR(sta%t_date_time< 'HH4>/M-') FROM se(ti#n WHERE TO_CHAR(sta%t_date_time< 'HH4>/M-') &'5B/=B' SECTION+ID TO+CH 2222222222 22222 85 (.97. :5 (.97. (.4 (.97. (.: (.97. ((/ (.97. (-- (.97. / !ows sele)ted* 2. Write a 5 er< that acc'$plishes the ?'ll';in. res lt. The ' tp t sh';s <' all the da<s '? the ;ee> ;here secti'ns ,," 7," and 18* start. #'te the 'rder '? the da<s. DA1 SECTION+ID 222 2222222222 ,o" :: T%e 8: Wed (.5 7 !ows sele)ted* S'l ti'n: SELECT TO_CHAR(sta%t_date_time< '!$') da$< se(ti#n_id FROM se(ti#n WHERE se(ti#n_id - (EE<DE<5B6)
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e 9 o) *+
,ersion -ate *!.!20(*

OR!ER B, TO_CHAR(sta%t_date_time< '!') The BD<B ?'r$at displa<s the da<s as &'n" T es" and s' ?'rth. The BDB ?'r$at listed in the ORDER BY cla se ;ill ret rn B1B ?'r S nda<" B2B ?'r &'nda<" B3B ?'r T esda<" etc. The BDB ?'r$at in the ORDER BY cla se is pre?era%le i? <' ;ant t' 'rder n't alpha%eticall< % t %< the da< '? the ;ee>. 3n this partic lar e9a$ple" the alpha%etical 'rderin. '? the da<s c'incides ;ith the se5 ence '? da<s '? the ;ee>" % t there $a< %e instances ;here this is n't the case. There?'re" the BDB ?'r$at is pre?era%le i? thatBs the desired 'rderin.. 3. Select the distinct c' rse c'sts ?'r all the c' rses. 3? a c' rse c'st is n>n';n" s %stit te a =er'. ('r$at the ' tp t ;ith a leadin. N si.n" and separate the th' sands ;ith a c'$$a. Displa< t;' di.its a?ter the deci$al p'int. The 5 er<Bs ' tp t sh' ld l''> li>e the ?'ll';in. res lt. COST 22222222222 ;.*.. ;(<.:5*.. ;(<(:5*.. ;(<5:5*.. 4 !ows sele)ted* S'l ti'n: SELECT !-ST- CT TO_CHAR( JL((#st< B)< 'KEE<EEB0EE') (#st FROM (#u%se OR!ER B, (#st 'r: SELECT !-ST- CT TO_CHAR(COALESCE((#st< B)< 'KEE<EEB0EE') (#st FROM (#u%se OR!ER B, (#st !. List all the r';s '? the /R-DEATY1E ta%le that ;ere created in the <ear 1,,7. S'l ti'n: SELECT * FROM )%ade_t$"e WHERE (%eated_date H&TO_!ATE('B5.LA .5EED'< '!!.MO .,,,,') A ! (%eated_date G TO_!ATE('B5.LA .5EEE'< '!!.MO .,,,,') *. What" i? an<thin." is ;r'n. ;ith the ?'ll';in. SQL state$entG SELECT zip + 100 FROM zipcode
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e (0 o) *+
,ersion -ate *!.!20(*

S'l ti'n: The 5 er< e9ec tes" % t d'esnBt $a>e sense %eca se <' d'nBt d' calc lati'ns 'n the E31 c'l $n" thatBs 'ne '? the reas'ns ;h< the E31 c'l $n in 2-RCH-R2 data t<pe ?'r$at and it st'res leadin. =er's. -dditi'nall<" an< calc lati'n sh' ld n't rel< 'n an i$plicit c'n+ersi'nJ it is %etter t' se the TOA#0&BER ? ncti'n. ). ('r the st dents enr'lled 'n :an ar< 38" 2884" displa< the c'l $ns ST0DE#TA3D and E#ROLLAD-TE. S'l ti'n: There are $an< p'ssi%le s'l ti'ns" here are 6 st a ?e; listed: SELECT student_id< en%#ll_date FROM en%#llment WHERE en%#ll_dateH & TO_!ATE('=B.LA .4BB?'< '!!.MO .,,,,') A ! en%#ll_date G TO_!ATE('=5.LA .4BB?'< '!!.MO .,,,,') 'r: SELECT student_id< en%#ll_date FROM en%#llment WHERE en%#ll_date H& !ATE '4BB?.B5.=B' A ! en%#ll_date G !ATE '4BB?.B5.=5' 'r: SELECT student_id< en%#ll_date FROM en%#llment WHERE TR; C(en%#ll_date) & TO_!ATE('=B.LA .4BB?'< '!!.MO . ,,,,') Res ltin. ' tp t: STUDENT+ID ENROLL+DA 2222222222 222222222 (.- 7.2=AN2.6 (.- 7.2=AN2.6 (.7 7.2=AN2.6 (.4 7.2=AN2.6 (.5 7.2=AN2.6 (./ 7.2=AN2.6 (./ 7.2=AN2.6 (.6 7.2=AN2.6 (.8 7.2=AN2.6 (.: 7.2=AN2.6 (.: 7.2=AN2.6 (( !ows sele)ted*

Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e (( o) *+
,ersion -ate *!.!20(*

Chapter ,: -..re.ate )unctions$ / !01 B" and H-23%/


1. List the 'rder in ;hich the WHERE" /RO01 BY" and H-23#/ cla ses are e9ec ted %< the data%ase in the ?'ll';in. SQL state$ent.
SELECT FROM WHERE 8RO;3 se(ti#n_id< CO; T(*)< *inal_)%ade en%#llment TR; C(en%#ll_date) H TO_!ATE('4M5CM4BB='< 'MMM!!M,,,,') B, se(ti#n_id< *inal_)%ade HAJ- 8 CO; T(*) H 6

S'l ti'n: (irst the WHERE cla se is e9ec ted" then the /RO01 BY" and lastl< the H-23#/ cla se is applied. 2. Displa< a c' nt '? all the di??erent c' rse c'sts in the CO0RSE ta%le. S'l ti'n: SELECT (#st< CO; T(*) FROM (#u%se 8RO;3 B, (#st COST COUNT(>? 222222222 222222222 (.:5 7 ((:5 -5 (5:5 ( ( 4 !ows sele)ted* #'te the #0LL +al e in the res lt set" and n'tice the di??erence i? <' ;rite the SQL state$ent sin. CO0#TCc'stDinstead. SELECT (#st< CO; T((#st) FROM (#u%se 8RO;3 B, (#st COST COUNT(COST? 222222222 22222222222 (.:5 7 ((:5 -5 (5:5 ( . 4 !ows sele)ted*
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e (2 o) *+
,ersion -ate *!.!20(*

3. Deter$ine the n $%er '? st dents li+in. in =ip c'de 1882*. S'l ti'n: SELECT CO; T(*) FROM student WHERE 7i" & '5BB46' COUNT(>? 222222222 7 ( !ow sele)ted* !. Sh'; all the di??erent c'$panies ?'r ;hich st dents ;'r>. Displa< 'nl< c'$panies in ;hich $'re than ?' r st dents are e$pl'<ed. S'l ti'n: SELECT em"l#$e%< CO; T(*) FROM student 8RO;3 B, em"l#$e% HAJ- 8 CO; T(*) H > E,PLO1ER COUNT(>? 22222222222222222222 222222222 A'e!*Le@ l S3ste's (. C! "e Co* / Ele)t!o"$) E"@$"ee!s (5 New 1o!A Pop 8 4 !ows sele)ted* *. List h'; $an< secti'ns each instr ct'r teaches. S'l ti'n: SELECT inst%u(t#%_id< CO; T(*) FROM se(ti#n 8RO;3 B, inst%u(t#%_id INSTRUCTOR+ID COUNT(>? 2222222222222 222222222 (.( : (.(. (.7 (. (.4 (. (.5 (. (./ (. (.6 (. (.8 :
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e (/ o) *+
,ersion -ate *!.!20(*

8 !ows sele)ted* ). What pr'%le$ d'es the ?'ll';in. state$ent s'l+eG SELECT FROM 8RO;3 HAJ- 8 CO; T(*)< sta%t_date_time< l#(ati#n se(ti#n B, sta%t_date_time< l#(ati#n CO; T(*) H 5

S'l ti'n: List the date" ti$e" and l'cati'n '? secti'ns that $eet at the sa$e ti$e" date" and l'cati'n. COUNT(>? START+DAT LOCATION 22222222 222222222 22222222 - .:2APR2.6 L-(4 - (/2APR2.6 L5.: - !ows sele)ted* 4. Deter$ine the hi.hest .rade achie+ed ?'r the $idter$ ;ithin each secti'n. S'l ti'n: SELECT se(ti#n_id< MAN(nume%i(_)%ade) FROM )%ade WHERE )%ade_t$"e_(#de & 'MT' 8RO;3 B, se(ti#n_id SECTION+ID ,AX(NU,ERIC+BRADE? 2222222222 222222222222222222 8. 6/ 8( 88 *** (54 :(5/ :: 5/ !ows sele)ted* 7. S pp'se <' ha+e a ta%le called C0STO&ERAORDER" ;hich c'ntains *",,3 r';s ;ith an 'rder t'tal '? N18",,3"333.,7 %ased 'n the 'rders ?r'$ !"*88 c st'$ers. /i+en this scenari'" h'; $an< r';CsD d'es the ?'ll';in. 5 er< ret rnG SELECT S;M(#%de%_am#unt) AS IO%de% T#talI FROM (ust#me%_#%de% S'l ti'n: -..re.ate ? ncti'ns al;a<s ret rn a sin.le r';. The res lt '? the S0& ? ncti'n ;ill ret rn 18",,3.333.,7.
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e (* o) *+
,ersion -ate *!.!20(*

Chapter 4: E5ui6oins
1. Select the c' rse descripti'n" secti'n n $%er" and l'cati'n ?'r secti'ns $eetin. in l'cati'n L211. S'l ti'n: SELECT des(%i"ti#n< se(ti#n_n#< l#(ati#n FROM (#u%se (< se(ti#n s WHERE (0(#u%se_n# & s0(#u%se_n# A ! l#(ati#n & 'L455' 0sin. the -#S3 :O3# s<nta9 and the O# cla se" it can als' %e ;ritten as: SELECT des(%i"ti#n< se(ti#n_n#< l#(ati#n FROM (#u%se ( LO- se(ti#n s O (0(#u%se_n# & s0(#u%se_n# WHERE l#(ati#n & 'L455' Or ;ith SELECT FROM ;S- 8 WHERE the 0S3#/ cla se: des(%i"ti#n< se(ti#n_n#< l#(ati#n (#u%se ( LO- se(ti#n s ((#u%se_n#) l#(ati#n & 'L455'

DESCRIPTION SECTION+NO LOCAT 222222222222222222222222222222222 2222222222 22222 P!oCe)t , " @e'e"t ( L-(( = v Develope! I 4 L-(( I"te!'ed$ te = v P!o@! ''$"@ - L-(( 7 !ows sele)ted* 2. Sh'; the c' rse descripti'n" secti'n n $%er" startin. date and ti$e '? the c' rses :'seph /er$an is ta>in.. S'l ti'n: SELECT des(%i"ti#n< se(ti#n_n#< sta%t_date_time FROM (#u%se (< se(ti#n s< en%#llment e< student st WHERE (0(#u%se_n# & s0(#u%se_n# A ! s0se(ti#n_id & e0se(ti#n_id A ! e0student_id & st0student_id A ! st0last_name & '8e%man' A ! *i%st_name & 'L#se"1'
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e (0 o) *+
,ersion -ate *!.!20(*

DESCRIPTION SECTION+NO START+DAT 2222222222222222222222222 2222222222 222222222 I"t!o to = v P!o@! ''$"@ - -42=UL2.6 ( !ow sele)ted* -s al;a<s <' can e9press this ;ith the -#S3 6'in s<nta9 as ?'ll';s ;hich $a< l''> li>e this: SELECT (0des(%i"ti#n< s0se(ti#n_n#< TO_CHAR(s0sta%t_date_time< '!!.MO .,,,, HH4>/M-/SS') FROM (#u%se ( LO- se(ti#n s O ((0(#u%se_n# & s0(#u%se_n#) LO- en%#llment e O (s0se(ti#n_id & e0se(ti#n_id) LO- student st O (e0student_id & st0student_id) WHERE st0last_name & '8e%man' A ! st0*i%st_name & 'L#se"1' Or <' can ;rite as ?'ll';s ;ith the 0S3#/ cla se. SELECT (0des(%i"ti#n< s0se(ti#n_n#< TO_CHAR(s0sta%t_date_time< '!!.MO .,,,, HH4>/M-/SS') FROM (#u%se ( LO- se(ti#n s ;S- 8 ((#u%se_n#) LO- en%#llment e ;S- 8 (se(ti#n_id) LO- student st ;S- 8 (student_id) WHERE st0last_name & '8e%man' A ! st0*i%st_name & 'L#se"1' 3. List the instr ct'r 3D" last na$e '? the instr ct'r" and secti'n 3D '? secti'ns ;here class participati'n c'ntri% tes t' 2* percent '? the t'tal .rade. Order the res lt %< the instr ct'rBs last na$e. S'l ti'n: SELECT i0inst%u(t#%_id< s0se(ti#n_id< last_name FROM inst%u(t#% i< se(ti#n s< )%ade_t$"e_2ei)1t 2 WHERE i0inst%u(t#%_id & s0inst%u(t#%_id A ! s0se(ti#n_id & 20se(ti#n_id A ! "e%(ent_#*_*inal_8RA!E & 46 A ! )%ade_t$"e_(#de & '3A' OR!ER B, last_name INSTRUCTOR+ID SECTION+ID LAST+NA,E 2222222222222 2222222222 2222222222
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e (+ o) *+
,ersion -ate *!.!20(*

(.6 (.( (.8 (.5 (.5 (.4 (./ (.(.: !ows sele)ted*

((5 (77 (55 (-: (44 8(76 (4: 88

F! "t0e" H "As Low!3 ,o!!$s ,o!!$s Pe!te0 S'3t#e WoC$)A WoC$)A

Or as an -#S3 6'in ;ith the 0S3#/ cla se: SELECT inst%u(t#%_id< se(ti#n_id< last_name FROM inst%u(t#% LO- se(ti#n ;S- 8 (inst%u(t#%_id) LO- )%ade_t$"e_2ei)1t ;S- 8 (se(ti#n_id) WHERE "e%(ent_#*_*inal_)%ade & 46 A ! )%ade_t$"e_(#de & '3A' OR!ER B, = !. Displa< the ?irst and last na$es '? st dents ;h' recei+ed ,, 'r $'re p'ints 'n the class pr'6ect. S'l ti'n: SELECT *i%st_name< last_name< nume%i(_)%ade FROM student s< en%#llment e< )%ade ) WHERE s0student_id & e0student_id A ! e0student_id & )0student_id A ! e0se(ti#n_id & )0se(ti#n_id A ! nume%i(_)%ade H& EE A ! )%ade_t$"e_(#de & '3L' FIRST+NA,E LAST+NA,E NU,ERIC+BRADE 2222222222 222222222222222 2222222222222 , 3 =odo$" :: =oel D!e"dle! :: - !ows sele)ted* Or as e9pressed ;ith an -#S3 6'in: SELECT *i%st_name< last_name< nume%i(_)%ade FROM student LO- en%#llment ;S- 8 (student_id) LO- )%ade ;S- 8 (student_id< se(ti#n_id)
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e (1 o) *+
,ersion -ate *!.!20(*

WHERE nume%i(_)%ade H& EE A ! )%ade_t$"e_(#de & '3L' *. Select the .rades ?'r 5 i==es '? st dents li+in. in =ip c'de 18,*). S'l ti'n: SELECT s0student_id< s0last_name< s0*i%st_name< )0nume%i(_)%ade< s07i" FROM student s< en%#llment e< )%ade ) WHERE s0student_id & e0student_id A ! e0student_id & )0student_id A ! e0se(ti#n_id & )0se(ti#n_id A ! )0)%ade_t$"e_(#de & 'OP' A ! s07i" & '5BE6C' STUDENT+ID LAST+NA,E FIRST+NA,E NU,ERIC+BRADE 2222222222 22222222222 22222222222 2222222222222 (:7 = 'e!")3 Al :( (:7 = 'e!")3 Al :. - !ows sele)ted* -lternati+el<" <' can als' 6'in the /R-DE ta%le directl< t' the ST0DE#T ta%le. C('r $'re in?'r$ati'n 'n s>ippin. this ta%le" see La% 4.2 and the para.raph titled OSP3113#/ THE 1R3&-RYQ(ORE3/# PEY 1-THO SELECT s0student_id< s0last_name< s0*i%st_name< )0nume%i(_)%ade< s07i" FROM student s< )%ade ) WHERE )0)%ade_t$"e_(#de & 'OP' A ! )0student_id & s0student_id A ! s07i" & '5BE6C' Or e9pressed in -#S3 6'in s<nta9 ;ith three ta%les: SELECT s0student_id< s0last_name< s0*i%st_name< )0nume%i(_)%ade< s07i" FROM student s LO- en%#llment e O (s0student_id & e0student_id) LO- )%ade ) O (e0student_id & )0student_id A ! e0se(ti#n_id & )0se(ti#n_id) WHERE )0)%ade_t$"e_(#de & 'OP' A ! s07i" & '5BE6C' ). List the c' rse n $%er" secti'n n $%er" and instr ct'r ?irst and last na$es '? classes ;ith c' rse n $%er 3*8 as a prere5 isite.
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

EIP 22222 (.:5/ (.:5/

Pa'e (. o) *+
,ersion -ate *!.!20(*

S'l ti'n: SELECT (0(#u%se_n#< se(ti#n_n#< *i%st_name< last_name FROM (#u%se (< se(ti#n s< inst%u(t#% i WHERE (0(#u%se_n# & s0(#u%se_n# A ! s0inst%u(t#%_id & i0inst%u(t#%_id A ! "%e%eQuisite & =6B COURSE+NO SECTION+NO FIRST+NA,E LAST+NA,E 222222222 2222222222 2222222222 22222222222 45. ( Fe!" "d H "As ( !ow sele)ted* The s'l ti'n can als' %e achie+ed sin. 'ne '? the -#S3 6'in s<nta9 +ariants: SELECT (0(#u%se_n#< se(ti#n_n#< *i%st_name< last_name FROM (#u%se ( LO- se(ti#n s O ((0(#u%se_n# & s0(#u%se_n#) LO- inst%u(t#% i O (s0inst%u(t#%_id & i0inst%u(t#%_id) WHERE "%e%eQuisite & =6B 4. What pr'%le$ d' the ?'ll';in. t;' SELECT state$ents s'l+eG SELECT stud0student_id< i0inst%u(t#%_id< stud07i"< i07i" FROM student stud< inst%u(t#% i WHERE stud07i" & i07i" SELECT stud0student_id< i0inst%u(t#%_id< stud07i"< i07i" FROM student stud< en%#llment e< se(ti#n se(< inst%u(t#% i WHERE stud0student_id & e0student_id A ! e0se(ti#n_id & se(0se(ti#n_id A ! se(0inst%u(t#%_id & i0inst%u(t#%_id A ! stud07i" & i07i" S'l ti'n: The t;' 5 eries identi?< st dents that li+e in the sa$e =ip c'de as instr ct'rs. The ?irst state$ent deter$ines th'se instr ct'rs ;h' li+e in the sa$e =ip c'de as st dents. 3t % ilds a Cartesian pr'd ct" %eca se there are
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e (9 o) *+
,ersion -ate *!.!20(*

$ ltiple 'cc rrences '? the sa$e =ip c'de in %'th the 3#STR0CTOR and ST0DE#T ta%les. The res lt l''>s li>e this: STUDENT+ID INSTRUCTOR+ID EIP EIP 2222222222 2222222222222 22222 22222 --7 (.- (..-5 (..-5 7:: (.- (..-5 (..-5 (/7 (.- (..-5 (..-5 --7 (.7 (..-5 (..-5 7:: (.7 (..-5 (..-5 (/7 (.7 (..-5 (..-5 --7 (./ (..-5 (..-5 7:: (./ (..-5 (..-5 (/7 (./ (..-5 (..-5 --7 (.8 (..-5 (..-5 7:: (.8 (..-5 (..-5 (/7 (.8 (..-5 (..-5 (- !ows sele)ted* The sec'nd state$ent sh';s the instr ct'rs ;h' li+e in the sa$e =ip c'de as the st dent the< teach. The res lt is as ?'ll';s: STUDENT+ID INSTRUCTOR+ID EIP EIP 2222222222 2222222222222 22222 22222 --7 (.7 (..-5 (..-5 (/7 (./ (..-5 (..-5 - !ows sele)ted*

Chapter 7: Su'5ueries
1. 0sin. a s %5 er< c'nstr ct" deter$ine ;hich secti'ns the st dent Henr< &asser is enr'lled in. S'l ti'n: SELECT se(ti#n_id FROM en%#llment WHERE student_id (SELECT student_id FROM student WHERE last_name & 'Masse%' A ! *i%st_name & 'Hen%$') "o !ows sele)ted
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e 20 o) *+
,ersion -ate *!.!20(*

Note: Henry Masser is not enrolled in any section at all. The query returns no rows. 2. What pr'%le$ d'es the ?'ll';in. SELECT state$ent s'l+eG SELECT 7i" FROM 7i"(#de 7 WHERE OT EN-STS (SELECT '*' FROM student WHERE 707i" & 7i") A ! OT EN-STS (SELECT '*' FROM inst%u(t#% WHERE 707i" & 7i") S'l ti'n: The 5 er< deter$ines the =ip c'des n't ?' nd in either the ST0DE#T ta%le 'r the 3#STR0CTOR ta%le. 3. Displa< the c' rse n $%er and descripti'n '? c' rses ;ith n' enr'll$ent. -ls' incl de c' rses ;hich ha+e n' secti'n assi.ned. S'l ti'n: SELECT (#u%se_n#< des(%i"ti#n FROM (#u%se ( WHERE OT EN-STS (SELECT ;LL FROM se(ti#n s WHERE (0(#u%se_n# & s0(#u%se_n#) OR (#u%se_n# (SELECT (#u%se_n# FROM se(ti#n s4 WHERE OT EN-STS (SELECT ;LL FROM en%#llment e WHERE s40se(ti#n_id & e0se(ti#n_id)) COURSE+NO DESCRIPTION 222222222 222222222222222222222222222222222 -5 I"t!o to P!o@! ''$"@ 8. P!o@! ''$"@ Te)#"$F%es *** 75. = v Develope! II 47. = v Develope! III (/ !ows sele)ted*

Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e 2( o) *+
,ersion -ate *!.!20(*

!. Can the -#Y and -LL 'perat'rs %e sed 'n the D-TE data t<peG Write a si$ple 5 er< t' pr'+e <' r ans;er. S'l ti'n: Yes" the -#Y and -LL 'perat'rs ;'r> 'n the D-TE data t<pe. There are $an< di??erent p'ssi%le sa$ple 5 eries. Here is 'ne ?'r each 'perat'r. The 5 eries pr'd ce the c'rrect res lt ;ith n' err'r. SELECT 'Hell#' FROM dual WHERE TO_!ATE('54.MAR.4BBE'< '!!.MO .,,,,') G A , (TO_!ATE('5=.MAR.4BBE'< '!!.MO .,,,,')< TO_!ATE('5>.MAR.4BBE'< '!!.MO .,,,,')) 'HELL 22222 Hello ( !ow sele)ted* SELECT 'Hell# a)ain' FROM dual WHERE TO_!ATE('54.MAR.4BEC'< '!!.MO .,,,,') HALL (SELECT (%eated_date FROM )%ade) 'HELLOABAIN 22222222222 Hello @ $" ( !ow sele)ted* *. 3? <' ha+e a ch'ice t' ;rite either a c'rrelated s %5 er< 'r a si$ple s %5 er<" ;hich 'ne ;' ld <' ch''seG Wh<G S'l ti'n: The c'rrelated s %5 er< sin. the #OT ER3STS 'perat'r tests ?'r #0LL +al es ;hich the #OT 3# 'perat'r d'es n't. -n'ther c'nsiderati'n is the n $%er '? rec'rds ret rned %< the ' ter 5 er< and the inner 5 er<. 3? the ' ter 5 er< ret rns a lar.e n $%er '? rec'rds" the c'rrelated s %5 er< $ st test ?'r each '? these ' ter r';s" ;hich is +er< ti$e@c'ns $in.. 3? the inner 5 er< ret rns 'nl< a +er< ?e; rec'rds" the si$ple s %5 er< is t<picall< %est. T' deter$ine the $'st e??icient state$ent" test a.ainst realistic data +'l $es and pr'perl< inde9ed ta%les. ('r $'re in?'r$ati'n a%' t this t'pic see Chapter 17" OSQL Opti$i=ati'n.O ). Deter$ine the t'p three =ip c'des ;here $'st '? the st dents li+e.
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e 22 o) *+
,ersion -ate *!.!20(*

S'l ti'n: SELECT s0*< ROW ;M %ankin) FROM (SELECT 7i"< CO; T(*) FROM student 8RO;3 B, 7i" OR!ER B, 4 !ESC) s WHERE ROW ;M G& = EIP COUNT(>? RANGINB 22222 222222222 222222222 .6.-4 : ( .6.(. / ((7/8 / 7 7 !ows sele)ted* #'te" i? <' e9ec te the inline +ie; 5 er<" <' n'tice that there are act all< three =ip c'des ;ith si9 st dents enr'lled each. Bel'; is a partial listin. '? the 5 er<. SELECT 7i"< CO; T(*) FROM student 8RO;3 B, 7i" OR!ER B, 4 !ESC EIP COUNT(>? 22222 222222222 .6.-4 : .6.(. / ((767 / ((7/8 / .6.45 *** .//.5 ( ./6:8 ( (45 !ows sele)ted* The =ip c'de 8482! has the lar.est n $%er '? st dents. Three =ip c'des ?'ll'; ;ith an e5 al n $%er '? enr'll$ents. B t 'nl< t;' are incl ded in the 5 er<" %eca se the ROW#0& pse d'c'l $n pic>s a $a9i$ $ '? three r';s. 3n Chapter 14" OE9pl'rin. Data Wareh' sin. (eat res"O <' ;ill learn $'re a%' t t'p@n 5 eries.

Chapter 8: Set !perators


Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e 2/ o) *+
,ersion -ate *!.!20(*

1. List all the =ip c'des in the E31CODE ta%le that are n't sed in the ST0DE#T 'r 3#STR0CTOR ta%les. Write t;' di??erent s'l ti'ns" sin. set 'perat'rs ?'r %'th. S'l ti'n: SELECT 7i" FROM 7i"(#de M- ;S SELECT 7i" FROM student M- ;S SELECT 7i" FROM inst%u(t#% 'r: SELECT 7i" FROM 7i"(#de M- ;S (SELECT 7i" FROM student ; -O SELECT 7i" FROM inst%u(t#%) EIP 22222 ..:(4 ./4.( *** 7.747747( 6: !ows sele)ted* 2. Write a SQL state$ent" sin. a set 'perat'r" t' sh'; ;hich st dents enr'lled in a secti'n 'n the sa$e da< the< re.istered. S'l ti'n: SELECT student_id< TR; C(%e)ist%ati#n_date) FROM student - TERSECT SELECT student_id< TR; C(en%#ll_date) FROM en%#llment "o !ows sele)ted

Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e 2* o) *+
,ersion -ate *!.!20(*

3. (ind the st dents ;h' are n't enr'lled in an< classes. Write three s'l ti'ns: a set 'perati'n" a s %5 er<" and a c'rrelated s %5 er<. S'l ti'n: SELECT student_id FROM student M- ;S SELECT student_id FROM en%#llment SELECT student_id FROM student WHERE student_id OT (SELECT student_id FROM en%#llment) SELECT student_id FROM student s WHERE OT EN-STS (SELECT 'A' FROM en%#llment e WHERE s0student_id & e0student_id) STUDENT+ID 2222222222 -84 -85 *** 7:6 7:: (.7 !ows sele)ted* !. Sh'; the st dents ;h' ha+e recei+ed .rades ?'r their class. Write ?' r s'l ti'ns: a set 'perati'n" a s %5 er<" a c'rrelated s %5 er<" and a 6'in. S'l ti'n: SELECT se(ti#n_id< student_id FROM en%#llment - TERSECT SELECT se(ti#n_id< student_id FROM )%ade SELECT se(ti#n_id< student_id FROM en%#llment
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e 20 o) *+
,ersion -ate *!.!20(*

WHERE (student_id< se(ti#n_id) (SELECT student_id< se(ti#n_id FROM )%ade) SELECT se(ti#n_id< student_id FROM en%#llment e WHERE EN-STS (SELECT 5 FROM )%ade ) WHERE e0se(ti#n_id & )0se(ti#n_id A ! e0student_id & )0student_id) SELECT !-ST- CT e0se(ti#n_id< e0student_id FROM en%#llment e< )%ade ) WHERE e0se(ti#n_id & )0se(ti#n_id A ! e0student_id & )0student_id SECTION+ID STUDENT+ID 2222222222 2222222222 8. (-8 8( (.7 *** (5/ -(4 (5/ -(5 --/ !ows sele)ted*

Chapter 19: Co&plex :oins


1. Write a 5 er< that sh';s all the instr ct'rs ;h' li+e in the sa$e =ip c'de. S'l ti'n: SELECT !-ST- CT i50*i%st_name< i50last_name< i507i" FROM inst%u(t#% i5< inst%u(t#% i4 WHERE i507i" & i407i" A ! i50inst%u(t#%_id GH i40inst%u(t#%_id OR!ER B, i507i" FIRST+NA,E LAST+NA,E EIP 2222222222 222222222222222 22222 R$)A C#ow (..(5 Fe!" "d H "As (..(5 A"$t ,o!!$s (..(5 C# !les Low!3 (..-5 N$" S)#o!$" (..-5
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e 2+ o) *+
,ersion -ate *!.!20(*

Todd To'

S'3t#e WoC$)A

(..-5 (..-5

6 !ows sele)ted* Or the 5 er< can als' %e ;ritten as an -#S3 6'in as ?'ll';s: SELECT !-ST- CT i50*i%st_name< i50last_name< i507i" FROM inst%u(t#% i5 LO- inst%u(t#% i4 O (i507i" & i407i") WHERE i50inst%u(t#%_id GH i40inst%u(t#%_id OR!ER B, i507i" #'te: Y' can als' $'+e the WHERE c'nditi'ns int' the O# cla se and it ;ill <ield the sa$e res lt as the< are all -#D c'nditi'ns that need t' %e $et ?'r the rec'rds t' %e ret rned in the res lt set. 2. -re an< '? the r''$s '+er%''>edG Deter$ine ;hether an< secti'ns $eet at the sa$e date" ti$e" and l'cati'n. S'l ti'n: SELECT !-ST- CT s0se(ti#n_id< TO_CHAR(s0sta%t_date_time< '!!.MO .,,,, HH4>/M-')< s0l#(ati#n FROM se(ti#n s< se(ti#n + WHERE s0l#(ati#n & +0l#(ati#n A ! s0sta%t_date_time & +0sta%t_date_time A ! s0se(ti#n_id GH +0se(ti#n_id OR!ER B, 4< = SECTION+ID TO+CHAR(S*START+D LOCAT 2222222222 22222222222222222 22222 (-8 .:2APR2-..6 .:97. L-(4 (7- .:2APR2-..6 .:97. L-(4 (.( (/2APR2-..6 .:97. L5.: (4. (/2APR2-..6 .:97. L5.: 4 !ows sele)ted* 3nstead '? sin. the pri$ar< >e< t' c'$pare i? this is the sa$e r'; 'r n't" <' c' ld se the ROW3D pse d'c'l $n instead Csee Chapter 13" K3nde9es" Se5 ences" and 2ie;sL ?'r $'re 'n ROW3DsD. The ROW3D can %e se? l i? n' pri$ar< >e< 'n ta%le e9ists. WHERE s0l#(ati#n & +0l#(ati#n A ! s0sta%t_date_time & +0sta%t_date_time A ! s0%#2id GH +0%#2id -lternati+el<" the 5 er< can %e ;ritten as ?'ll';s: SELECT se(ti#n_id< TO_CHAR(sta%t_date_time< '!!.MO .,,,, HH4>/M-')< l#(ati#n
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e 21 o) *+
,ersion -ate *!.!20(*

FROM se(ti#n WHERE (l#(ati#n< sta%t_date_time) (SELECT l#(ati#n< sta%t_date_time FROM se(ti#n 8RO;3 B, sta%t_date_time< l#(ati#n HAJ- 8 CO; T(*) H 5) 3. Deter$ine ;hether there is an< sched lin. c'n?lict %et;een instr ct'rs: -re an< instr ct'rs sched led t' teach 'ne 'r $'re secti'ns at the sa$e date and ti$eG Order the res lt %< the 3#STR0CTORA3D and the startin. date and ti$e '? the secti'ns. S'l ti'n: SELECT !-ST- CT s50inst%u(t#%_id< TO_CHAR(s50sta%t_date_time< '!!.MO .,,,, HH4>/M-')< s50se(ti#n_id FROM se(ti#n s5< se(ti#n s4 WHERE s50inst%u(t#%_id & s40inst%u(t#%_id A ! s50sta%t_date_time & s40sta%t_date_time A ! s50se(ti#n_id GH s40se(ti#n_id INSTRUCTOR+ID TO+CHAR(S(*START+ SECTION+ID 2222222222222 22222222222222222 2222222222 (.( (/2APR2-..6 .:97. (.( (.( (/2APR2-..6 .:97. (4. (.- .42,A12-..6 .:97. 88 (.- .42,A12-..6 .:97. (4: (.7 (42=UL2-..6 .:97. (.6 (.7 (42=UL2-..6 .:97. ((: (.7 (52,A12-..6 .:97. 8: (.7 (52,A12-..6 .:97. (5. (.7 -42=UL2-..6 .:97. 8( (.7 -42=UL2-..6 .:97. (-6 (.7 -42=UL2-..6 .:97. (4(.4 (-2=UN2-..6 .:97. :. (.4 (-2=UN2-..6 .:97. (5( (.5 .62,A12-..6 .:97. :6 (.5 .62,A12-..6 .:97. (-: (.6 .62,A12-..6 .:97. :: (.6 .62,A12-..6 .:97. ((5 (.6 -(2,A12-..6 .:97. (78 (.6 -(2,A12-..6 .:97. (54 (.8 .:2=UN2-..6 .:97. (.. (.8 .:2=UN2-..6 .:97. (7: -( !ows sele)ted* -lternati+el<" <' can ;rite the SQL state$ent as ?'ll';s:
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e 2. o) *+
,ersion -ate *!.!20(*

SELECT inst%u(t#%_id< sta%t_date_time< se(ti#n_id FROM se(ti#n WHERE (inst%u(t#%_id< sta%t_date_time) (SELECT inst%u(t#%_id< sta%t_date_time FROM se(ti#n 8RO;3 B, inst%u(t#%_id< sta%t_date_time HAJ- 8 CO; T(*) H 5) !. Sh'; the c' rse n $%er" descripti'n" c' rse c'st" and secti'n 3D ?'r c' rses that c'st 11,* 'r $'re. 3ncl de c' rses that ha+e n' c'rresp'ndin. secti'n. S'l ti'n: SELECT (0(#u%se_n#< des(%i"ti#n< se(ti#n_id< (#st FROM (#u%se ( LEFT O;TER LO- se(ti#n s O ((0(#u%se_n# & s0(#u%se_n#) WHERE (#st H& 55E6 OR!ER B, 5 COURSE+NO DESCRIPTION SECTION+ID COST 222222222 2222222222222222222222222 2222222222 222222222 (. Te)#"olo@3 Co")epts 8. ((:5 *** 8. P!o@! ''$"@ Te)#"$F%es (5:5 (.. H "ds2O" W$"dows (4( ((:5 *** (-4 Adv ")ed = v P!o@! ''$"@ (-/ ((:5 (-4 Adv ")ed = v P!o@! ''$"@ (-6 ((:5 *** 47. = v Develope! III ((:5 6( !ows sele)ted* #'te c' rses 78 and !38 d' n't ha+e a c'rresp'ndin. secti'n assi.ned. Or <' can ;rite the 5 er< sin. the traditi'nal s<nta9" ;ith the c'$$a %et;een the ta%les in the (RO& cla se. SELECT (0(#u%se_n#< des(%i"ti#n< se(ti#n_id< (#st FROM (#u%se (< se(ti#n s WHERE (0(#u%se_n# & s0(#u%se_n#(R) A ! (#st H& 55E6 OR!ER B, 5 *. Write a 5 er< that lists the secti'n n $%ers and st dents 3Ds '? st dents enr'lled in classes held in l'cati'n BL218B. 3ncl de secti'ns ?'r ;hich n' st dents are enr'lled.
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e 29 o) *+
,ersion -ate *!.!20(*

S'l ti'n: SELECT s0se(ti#n_id< e0se(ti#n_id< e0student_id FROM se(ti#n s LEFT O;TER LO- en%#llment e O s0se(ti#n_id & e0se(ti#n_id WHERE l#(ati#n & 'L45B' SECTION+ID SECTION+ID STUDENT+ID 2222222222 2222222222 2222222222 8( 8( (.7 8( 8( (.4 8( 8( -4. 84 84 (58 *** (-4 (-: *** (55 (55 -48 (55 (55 -4( (55 (55 (-6 7( !ows sele)ted* Y' can als' ;rite the 5 er< as ?'ll';s: SELECT s0se(ti#n_id< e0se(ti#n_id< e0student_id FROM se(ti#n s< en%#llment e WHERE l#(ati#n & 'L45B' A ! s0se(ti#n_id & e0se(ti#n_id(R)

Chapter 11: 3nsert$ 0pdate$ and Delete


1. Write and e9ec te t;' 3#SERT state$ents t' insert r';s in the E31CODE ta%le ?'r the ?'ll';in. t;' cities: #e;t'n" &- 821,, and Cle+eland" OH !3811. -?ter <' r 3#SERT state$ents are s ccess? l" $a>e the chan.es per$anent. S'l ti'n: - SERT - TO 7i"(#de ((it$< state< 7i"< (%eated_date< (%eated_+$< m#di*ied_date< m#di*ied_+$) JAL;ES (' e2t#n'< 'MA'< 'B45EE'< S,S!ATE< ;SER< S,S!ATE< ;SER) - SERT - TO 7i"(#de ((it$< state< 7i"< (%eated_date< (%eated_+$< m#di*ied_date< m#di*ied_+$) JAL;ES
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e /0 o) *+
,ersion -ate *!.!20(*

('Cle@eland'< 'OH'< '>=B55'< S,S!ATE< ;SER< S,S!ATE< ;SER) COMM-T 2. &a>e <' rsel? a st dent %< ;ritin. and e9ec tin. an 3#SERT state$ent t' insert a r'; int' the ST0DE#T ta%le ;ith data a%' t <' . 0se 'ne '? the =ip c'des <' inserted in e9ercise 1. 3nsert +al es int' the c'l $ns ST0DE#TA3D C se the +al e '? B,88BD" (3RSTA#-&E" L-STA#-&E" E31" RE/3STR-T3O#AD-TE C se a date that is ?i+e da<s a?ter t'da<D" CRE-TEDABY" CRE-TEDAD-TE" &OD3(3EDABY" and &OD3(3EDAD-TE. 3ss e a CO&&3T c'$$and ;hen <' are d'ne. S'l ti'n: - SERT - TO student (student_id< *i%st_name< last_name< 7i"< %e)ist%ati#n_date< (%eated_+$< (%eated_date< m#di*ied_+$< m#di*ied_date) JAL;ES (EBB< 'Sand$'< '!ella(#%te'< 'B45EE'< S,S!ATE R 6< ;SER< S,S!ATE< ;SER< S,S!ATE) COMM-T 3. Write an 01D-TE state$ent t' pdate the data a%' t <' in the ST0DE#T ta%le. 0pdate the c'l $ns S-L0T-T3O#" STREETA-DDRESS" 1HO#E" and E&1LOYER. Be s re t' als' pdate the &OD3(3EDAD-TE c'l $n and $a>e the chan.es per$anent. S'l ti'n: ;3!ATE student SET salutati#n & 'Ms0'< st%eet_add%ess & 'CB Winte% St0'< "1#ne & 'C5?.4=C.4?>C'< em"l#$e% & 'Ra$t#ne'< m#di*ied_+$ & ;SER< m#di*ied_date & S,S!ATE WHERE student_id & EBB COMM-T !. Delete the r'; <' created the ST0DE#T ta%le and the t;' r';s <' created in the E31CODE ta%le. Be s re t' iss e a CO&&3T c'$$and a?ter;ards. . Y' can per?'r$ this acti'n %< sin. a SQL c'$$and 'r SQL De+el'perIs Data ta%. S'l ti'n:
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e /( o) *+
,ersion -ate *!.!20(*

!ELETE FROM student WHERE student_id & EBB !ELETE FROM 7i"(#de WHERE 7i" - ('B45EE'< '>=B55') COMM-T *. Delete the =ip c'de 18,*! ?r'$ the E31CODE ta%le %< sin. SQL De+el'per. C'$$it <' r chan.e a?ter <' delete the r';. Descri%e the res lts '? <' r acti'ns. S'l ti'n: SQL De+el'per $ar>s the r'; ?'r deleti'n p'n pressin. the Delete ic'n. H';e+er" as s''n as <' c'$$it the chan.e" Oracle rec'.ni=es that dependent rec'rds e9ist and disall';s the deleti'n '? the r';. The Data Edit'r L'. ta% rep'rts the err'r as sh';n %el';.

2) %ou per)or&e3 the exercises in this chapter4 %ou "ill ha5e chan'e3 3ata in &ost o) the tables o) the S67-E86 sche&a# 2) %ou 'o back to the
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e /2 o) *+
,ersion -ate *!.!20(*

pre5ious chapters an3 reexecute those $ueries4 %ou &i'ht )in3 that the results are 3i))erent than the% "ere be)ore# 6here)ore4 i) %ou "ant to reloa3 the tables an3 3ata4 %ou can run the rebuil3Stu3ent#s$l script#

Chapter 12: Create$ -lter$ and Drop Ta'les


1. Create a ta%le called TE&1AST0DE#T ;ith the ?'ll';in. c'l $ns and c'nstraints: a c'l $n ST0D3D ?'r st dent 3D that is #OT #0LL and is the pri$ar< >e<" a c'l $n (3RSTA#-&E ?'r st dent ?irst na$eJ a c'l $n L-STA#-&E ?'r st dent last na$e" a c'l $n E31 that is a ?'rei.n >e< t' the E31 c'l $n in the E31CODE ta%le" a c'l $n RE/3STR-T3O#AD-TE that is #OT #0LL and has a CHECP c'nstraint t' restrict the re.istrati'n date t' dates a?ter :an ar< 1st" 2888. S'l ti'n: CREATE TABLE tem"_student (studid ;MBER(D) OT ;LL< *i%st_name JARCHAR4(46)< last_name JARCHAR4(46)< 7i" JARCHAR4(6)< %e)ist%ati#n_date !ATE OT ;LL< CO STRA- T tem"_student_"k 3R-MAR, SE,(studid)< CO STRA- T tem"_student_7i"(#de_*k FORE-8 SE,(7i") REFERE CES 7i"(#de(7i")< CO STRA- T tem"_student_%e)_date_(k CHECS(%e)ist%ati#n_date H TO_!ATE('B5.LA .4BBB'< '!!.MO .,,,,')) ) 2. Write an 3#SERT state$ent that +i'lates 'ne '? the c'nstraints ?'r the TE&1AST0DE#T ta%le <' created in e9ercise 1. Write an'ther 3#SERT state$ent that s cceeds ;hen e9ec ted" and c'$$it <' r ;'r>. S'l ti'n: - SERT - TO tem"_student (studid< *i%st_name< last_name< 7i"< %e)ist%ati#n_date) JAL;ES ( ;LL< 'AleA'< 'M#%%is#n'< 'EEEEE'< TO_!ATE('B5.!EC. 5EEE'< '!!.MO .,,,,')) - SERT - TO tem"_student * ERROR t l$"e (9
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e // o) *+
,ersion -ate *!.!20(*

ORA2.(4..9 ) ""ot $"se!t NULL $"to (HSTUDENTH*HTE,P+STUDENTH*HSTUDIDH? - SERT - TO tem"_student JAL;ES (5B5< 'AleA'< 'M#%%is#n'< 'B?C6C'< TO_!ATE('B5.!EC. 4BBB'< '!!.MO .,,,,')) ( !ow )!e ted* 3. -lter the TE&1AST0DE#T ta%le t' add t;' $'re c'l $ns called E&1LOYER and E&1LOYERAE31. The E&1LOYERAE31 c'l $n sh' ld ha+e a ?'rei.n >e< c'nstraint that re?erences the E31 c'l $n '? the E31CODE ta%le. 0pdate the E&1LOYER c'l $n" and alter the ta%le 'nce a.ain t' $a>e the E&1LOYER c'l $n #OT #0LL. Dr'p the TE&1AST0DE#T ta%le ;hen <' are d'ne ;ith the e9ercise. S'l ti'n: ALTER TABLE tem"_student A!! (em"l#$e% JARCHAR4(4B)< em"l#$e%_7i" JARCHAR4(6)< CO STRA- T tem"_student_*k FORE-8 SE,(em"l#$e%_7i") REFERE CES 7i"(#de(7i")) ;3!ATE tem"_student SET em"l#$e% & 'A M 3%#du(ti#ns' ALTER TABLE tem"_student MO!-F, (em"l#$e% OT ;LL) !RO3 TABLE tem"_student

Chapter 13: 3ndexes$ Se5uences$ and 2ie;s


1. Wh' can pdate the S-L-RY c'l $n thr' .h the &YAE&1LOYEE +ie;G Hint: The 0SER ? ncti'n ret rns the na$e '? the ser ;h' is c rrentl< l'..ed in. CREATE OR RE3LACE J-EW m$_em"l#$ee AS SELECT em"l#$ee_id< em"l#$ee_name< sala%$< mana)e% FROM em"l#$ee WHERE mana)e% & ;SER W-TH CHECS O3T-O CO STRA- T m$_em"l#$ee_(k_mana)e%

Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e /* o) *+
,ersion -ate *!.!20(*

S'l ti'n: Onl< $ana.ers can pdate their respecti+e e$pl'<eeBs salaries. The W3TH CHECP O1T3O# c'nstraint ens res that D&L state$ents satis?< the c'nditi'n in the WHERE cla se. This c'nditi'n en?'rces that 'nl< rec'rds are displa<ed" pdated" inserted" and deleted ;here the +al e in the &-#-/ER c'l $n is e5 al t' the ser c rrentl< l'..ed in. - SELECT state$ent a.ainst the &YAE&1LOYEE +ie; ?'r the ser ;ith the l'.in 3D '? :O#ES c' ld l''> li>e this: EM3LO,EE_-! ........... 56B 465 664 =6= 2. EM3LO,EE_ AME SALAR, MA A8 ......................... ......... ..... 8ates 55?D? LO ES S1e""a%d 555BC LO ES Ed2a%ds ?B=C LO ES 31il"#tts 55=?= LO ES

Which c'l $ns in a ta%le sh' ld <' c'nsider inde9in.G

S'l ti'n: C'l $ns ?re5 entl< sed in the WHERE cla se '? SQL state$ents are .''d candidates ?'r inde9es. Be s re t' c'nsider the selecti+it< '? the +al es '? the c'l $ns" that is" h'; $an< distinct +al es there are in the c'l $n. S'$eti$es it is se? l t' c'$%ine se+eral c'l $ns ;ith a l'; selecti+it< in a c'ncatenated inde9. &a>e s re <' pr'perl< access the inde9. Y' als' see $'re e9a$ples 'n inde9es and their i$pact in Chapter 17" OSQL Opti$i=ati'n.O 3n additi'n" c'nsider inde9in. ?'rei.n >e< c'l $ns" %eca se the< n't 'nl< are ?re5 entl< re?erenced in the WHERE cla se '? 6'ins" % t als' i$pr'+e the l'c>in. '? rec'rds 'n the child ta%le. 3. E9plain the p rp'se '? the Oracle SQL c'$$and %el';. ALTER - !EN (%se_(%se_*k_i REB;-L! S'l ti'n: This c'$$and re% ilds an e9istin. inde9 na$ed CRSEACRSEA(PA3 ;ith' t ha+in. t' dr'p the 'ld inde9 ?irst and then re@create it. !. -re #0LLs st'red in an inde9G E9plain.

S'l ti'n: #0LLs are n't st'red in an inde9. The e9cepti'n is i? it is a c'ncatenated inde9 and the leadin. c'l $n '? the inde9 d'es n't c'ntain a #0LL +al e. -n'ther e9cepti'n is a %it$apped inde9" ;hich st'res n ll +al es.

Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e /0 o) *+
,ersion -ate *!.!20(*

Chapter 1#: The Data Dictionar<$ Scriptin.$ and eportin.


1. Descri%e the res lt '? the ?'ll';in. 5 er<. SELECT ta+le_name< (#lumn_name< (#mments FROM use%_(#l_(#mments S'l ti'n: Write a 5 er< t' displa< all the c'l $n c'$$ents in the serBs sche$a. TADLE+NA,E COLU,N+NA,E CO,,ENTS 2222222222 222222222222222 22222222222222222222 COURSE COURSE+NO T#e %"$F%e ID fo! )o%!se* COURSE DESCRIPTION T#e f%ll " 'e fo! t# $s )o%!se* *** EIPCODE CREATED+DATE A%d$t )ol%'" 2 $"d$) tes d te of $"se!t* EIPCODE ,ODIFIED+D1 A%d$t )ol%'" 2 $"d$) tes w#o ' de l st % pd te* EIPCODE ,ODIFIED+DATE A%d$t )ol%'" 2 d te of l st %pd te* (-- !ows sele)ted* The res lt '? <' r 5 er< $a< +ar< ?r'$ the a%'+e res lt dependin. 'n the '%6ects <' ha+e created. The res lt sh';s a list '? c'l $n c'$$ents. 3t is se? l t' place c'$$ents 'n c'l $ns andQ'r ta%les descri%in. the in?'r$ati'n ?' nd ;ithin the c'l $n 'r ta%le. The ?'ll';in. c'$$and creates a c'l $n c'$$ent ?'r the 3#STR0CTORA3D c'l $n 'n the 3#STR0CTOR ta%le. COMME T O COL;M - STR;CTOR0- STR;CTOR_-! -S 'T1e uniQue -! *#% an inst%u(t#%0' Co''e"t )!e ted* - ta%le c'$$ent is st'red in the data dicti'nar< +ie; 0SERAT-BACO&&E#TS. The ne9t state$ent creates a ta%le c'$$ent ?'r the instr ct'r ta%le. COMME T O TABLE - STR;CTOR -S '3%#*ile in*#%mati#n *#% an inst%u(t#%0'
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e /+ o) *+
,ersion -ate *!.!20(*

Co''e"t )!e ted* 2. E9plain the di??erences %et;een the +ie;s 0SERA0SERS" -LLA0SERS" and DB-A0SERS. S'l ti'n: The 0SERA0SERS +ie; sh';s in?'r$ati'n a%' t the c rrentl< l'..ed in ser. Y' see se? l in?'r$ati'n s ch as the de?a lt ta%lespace na$e and the te$p'rar< ta%lespace na$e as ;ell as the date the ser ;as created. The -LLA0SERS +ie; sh';s a list '? all the sers in the s<ste$ and the date the ser ;as created. The DB-A0SERS +ie; displa<s all the sers in the s<ste$. The c'l $ns listed incl de the date the ser ;as created" the de?a lt and te$p'rar< ta%lespaces" and the encr<pted pass;'rd. 3. What are the nderl<in. data dicti'nar< +ie;s ?'r the p %lic s<n'n<$s T-BS and COLSG S'l ti'n: The p %lic s<n'n<$s are 0SERAT-BLES and 0SERAT-BACOL0&#S. The 5 eries t' deter$ine this s'l ti'n are: SELECT s$n#n$m_name< ta+le_name FROM all_s$n#n$ms WHERE s$n#n$m_name - ('TABS'< 'COLS') S1NON1,+NA,E TADLE+NA,E 222222222222222222222222222222 2222222222222222222222222222 TADS USER+TADLES COLS USER+TAD+COLU,NS - !ows sele)ted* Y' can als' 5 er< the D3CT +ie; ;ith this state$ent. SELECT * FROM di(t WHERE ta+le_name - ('TABS'< 'COLS') TADLE+NA,E CO,,ENTS 22222222222222222222222 222222222222222222222222222222 COLS S3"o"3' fo! USER+TAD+COLU,NS TADS S3"o"3' fo! USER+TADLES - !ows sele)ted* !. Write a d<na$ic SQL script t' dr'p all +ie;s in the ST0DE#T sche$a. 3? there are n' +ie;s" create s'$e t' test <' r script. S'l ti'n: T' create s'$e +ie;s iss e the ?'ll';in. state$ents. CREATE OR RE3LACE J-EW @ie25_@ AS
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e /1 o) *+
,ersion -ate *!.!20(*

SELECT * FROM student CREATE OR RE3LACE J-EW @ie24_@ AS SELECT * FROM (#u%se #'; create a ?ile ;ith the ?'ll';in. c'$$ands. Sa+e the ?ile. SET ECHO OFF REM File name/ d%#"_@ie20sQl REM 3u%"#se/ !%#" all t1e @ie2s in a use%'s s(1ema0 REM C%eated/ 5?.Ma%.4BBE AR REM Je%si#n/ 50B SET 3A8ES-PE B SET L- ES-PE DB SET FEE!BACS OFF SET TERM OFF S3OOL d%#"_@ie20#ut SELECT '!RO3 J-EW '::@ie2_name::' '' FROM use%_@ie2s' S3OOL OFF SET 3A8ES-PE 4> SET L- ES-PE DB SET FEE!BACS O SET TERM O SET ECHO O Td%#"_@ie20#ut Then e9ec te the ?ile ?r'$ the SQLF1l s pr'$pt ;ith the S c'$$and. SOLHTd%#"_@ie20sQl

Chapter 1*: Securit<


T' c'$plete the e9ercises %el';" create a ne; ser called SCHOOL ;ith the pass;'rd pr'.ra$" and .rant CO##ECT and RESO0RCE pri+ile.es t' it. Then l'. in as the ST0DE#T ser. .. C%eate SCHOOL use% CO S,STEMMmana)e% CREATE ;SER s(1##l -!E T-F-E! B, "%#)%am' 8RA T CO ECT< RESO;RCE TO s(1##l' 1. Create t;' r'les: RE/3STR-R and 3#STR0CTOR.
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e /. o) *+
,ersion -ate *!.!20(*

S'l ti'n: &a>e s re <' ha+e the CRE-TE ROLE s<ste$ pri+ile.e" 'ther;ise <' ;ill n't %e a%le t' create the r'les. CO studentMlea%n CREATE ROLE %e)ist%a%' CREATE ROLE inst%u(t#%' 2. Create a +ie; called C0RRE#TARE/S that re?lects all st dents ;h' re.istered 'n :an ar< 2*" 2884. /rant SELECT pri+ile.es 'n the ne; +ie; t' the RE/3STR-R r'le. S'l ti'n:
CREATE SELECT FROM WHERE A ! OR RE3LACE J-EW (u%%ent_%e)s AS *i%st_name< last_name student %e)ist%ati#n_date H& TO_!ATE('46.LA .4BB?'< '!!.MO .,,,,') %e)ist%ati#n_date G TO_!ATE('4C.LA .4BB?'< '!M.MO .,,,,')'

8RA T SELECT O

(u%%ent_%e)s TO %e)ist%a%'

3. Create a +ie; called r'ster that re?lects all st dents ta .ht %< the instr ct'r &aril<n (rant=en. /rant SELECT pri+ile.es 'n the ne; +ie; t' the 3#STR0CTOR r'le. S'l ti'n: CREATE OR RE3LACE J-EW %#ste% AS SELECT se0(#u%se_n# (#u%se< se0se(ti#n_id se(ti#n< s0*i%st_name *i%st< s0last_name last< e0student_id FROM student s< en%#llment e< se(ti#n se< inst%u(t#% i WHERE s0student_id & e0student_id A ! e0se(ti#n_id & se0se(ti#n_id A ! se0inst%u(t#%_id & i0inst%u(t#%_id A ! i0*i%st_name & 'Ma%il$n' A ! i0last_name & 'F%ant7en'' 8RA T SELECT O %#ste% TO inst%u(t#%'

!. /rant the RE/3STR-R and 3#STR0CTOR r'les t' the ne; ser called SCHOOL. S'l ti'n: 8RA T %e)ist%a%< inst%u(t#% TO s(1##l' *. L'. in as the ser SCHOOL and select ?r'$ the t;' pre+i' sl< created +ie;s.
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e /9 o) *+
,ersion -ate *!.!20(*

S'l ti'n: CO ECT s(1##lM"%#)%am SELECT * FROM student0(u%%ent_%e)s' SELECT * FROM student0%#ste%'

Chapter 1,: e.ular Expressions and Hierarchical Queries


1. #a$e 'ther hierarchical relati'nships <' are ?a$iliar ;ith. S'l ti'n: E9a$ples '? hierarchical relati'nships are a parts e9pl'si'n" als' re?erred t' as %ill '? $aterials" ;here <' sh'; all the parts that .' int' the asse$%l< '? a ?inal pr'd ct. -n'ther e9a$ple is the hierarch< '? an 'r.ani=ati'n" sh';in. all the e$pl'<ees and their respecti+e $ana.ers. - ?inancial pr'?it and l'ss state$ent rep'rt can %e an'ther e9a$ple '? a tree" ;here s $$ar< acc' nts are $ade p '? 'ther s $$ar< acc' nts that ?inall< res lt in p'stin.@le+el acc' nts. 2. Chan.e the prere5 isite '? c' rse n $%er 318 Operatin. S<ste$s" a r''t r'; in the hierarch<" ?r'$ a n ll +al e t' 1!* 3nternet 1r't'c'ls. Write the 5 er< t' detect the l''p in the hierarch<" sin. the CO##ECTABYA3SCYCLE pse d'c'l $n. S'l ti'n: With' t the chan.e" the hierarch< ?'r c' rse n $%er 318 l''>s li>e this: =5B O"e%atin) S$stems 5=B -nt%# t# ;niA 5=4 Basi(s #* ;niA Admin 5=> Ad@an(ed ;niA Admin 5=6 ;niA Ti"s and Te(1niQues ==B et2#%k Administ%ati#n 5>6 -nte%net 3%#t#(#ls

The 01D-TE state$ent ;ill create a l''p in the hierarch<. ;3!ATE (#u%se
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e *0 o) *+
,ersion -ate *!.!20(*

SET "%e%eQuisite & 5>6 WHERE (#u%se_n# & =5B Essentiall<" the CO##ECTABYA3SCYCLE ret rns the +al e '? 1 i? a r'; has a child ;hich is its ';n ancest'r. The ne9t 5 er< detects the l''p. SELECT * FROM (SELECT (#u%se_n#< "%e%eQuisite< S,S_CO ECT_B,_3ATH((#u%se_n#< 'M') AS I3at1I< LEJEL< CO ECT_B,_-SC,CLE AS ($(le FROM (#u%se CO ECT B, OC,CLE 3R-OR (#u%se_n# & "%e%eQuisite) WHERE ($(le & 5 COURSE+NO PREREIUISITE P t# LEVEL C1CLE 222222222 222222222222 222222222222222 22222 22222 (45 7(. J7(.J(45 ( 7(. (45 J(45J7(. ( - !ows sele)ted* 3? the l''p is % ried deeper in the hierarch<" <' r 5 er< ;ill ret rn $ ltiple r';s that all indicate the l''p %ein. ca sed. ('r si$ple chec>in. '? the hierarch<" it can %e help? l t' displa< the c' rse descripti'n s ch as SYSACO##ECTABYA1-THCdescripti'n" BFBD. Be s re t' reset the data %ac> t' its 'ri.inal state ;ith this state$ent. ;3!ATE (#u%se SET "%e%eQuisite & ;LL WHERE (#u%se_n# & =5B COMM-T 3. Wh< d'esnIt this 5 er< ret rn an< r';sG

SELECT * FROM inst%u(t#% WHERE RE8EN3_L-SE(inst%u(t#%_id< 'U/di)it/V') "o !ows sele)ted S'l ti'n: The RE/ER1RAL3PE ? ncti'n d'es n't rec'.ni=e this pattern as a character class T:di.it:U %eca se it is n't encl'sed ;ith an e9tra set '? s5 are %rac>ets ?'r the character class list.
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e *( o) *+
,ersion -ate *!.!20(*

!. -dd a S'cial Sec rit< n $%er c'l $n t' the ST0DE#T ta%le 'r create a separate ta%le ;ith this c'l $n. Write a c'l $n chec> c'nstraint that +eri?ies that the s'cial sec rit< n $%er is entered in the c'rrect MMM@MM@MMMM ?'r$at. S'l ti'n: ALTER TABLE student A!! (ssn JARCHAR4(55)) ALTER TABLE student A!! CO STRA- T stud_ssn_(k CHECS (RE8EN3_L-SE(ssn< 'W(UU/di)it/VVX=Y.UU/di)it/VVX4Y.UU/di)it/VVX>Y)K'))

Chapter 14: Explorin. Data Warehousin. )eatures


1. Write the 5 esti'n ?'r the ?'ll';in. 5 er< and ans;er. SELECT CO; T(!ECO!E(S-8 (t#tal_(a"a(it$.4B)< .5< 5< B< 5)) IG&4BI< CO; T(!ECO!E(S-8 (t#tal_(a"a(it$.45)< B< 5< .5< ;LL< !ECO!E(S-8 (t#tal_(a"a(it$.=B)< .5< 5))) I45.=BI< CO; T(!ECO!E(S-8 (t#tal_(a"a(it$.=B)< 5< 5)) I=5RI FROM (SELECT S;M((a"a(it$) t#tal_(a"a(it$< (#u%se_n# FROM SECT-O 8RO;3 B, CO;RSE_ O) KL-. -(27. 7(M 222222222 222222222 222222222 (. (/ ( !ow sele)ted* S'l ti'n: The 5 esti'n sh' ld %e si$ilar t' 'ne '? the ?'ll';in.: Deter$ine the t'tal capacit< ?'r each c' rse and 'rder the$ in three c'l $ns. List the n $%er '? c' rses ;ith a t'tal capacit< '? 28 'r less in 'ne c'l $n" the n $%er '? c' rses ;ith a t'tal capacit< %et;een 21 and 38 in an'ther" and lastl< sh'; the n $%er '? c' rses ;ith a capacit< '? '+er 31 in the third c'l $n. The res lt sh';s that there are t;' c' rses ;ith a t'tal capacit< '? 28 'r less" 18 c' rses ;ith a
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e *2 o) *+
,ersion -ate *!.!20(*

capacit< %et;een 21 and 38" and 1) c' rses ;ith a capacit< '+er 31 st dents. 2. 0sin. an anal<tical ? ncti'n" deter$ine the t'p three =ip c'des ;here $'st '? the st dents li+e. S'l ti'n: SELECT * FROM (SELECT 7i"< CO; T(*)< !E SE_RA S() OJER(OR!ER B, CO; T(7i") !ESC) AS %ank FROM student 8RO;3 B, 7i") WHERE %ank G&= EIP COUNT(>? RANG 22222 2222222222 2222222222 .6.-4 : ( .6.(. / ((767 / ((7/8 / .6.45 7 ((755 5 7 ((-.: 5 7 .6.46 5 7 ((765 5 7 ((765 7 (. !ows sele)ted* 3. E9plain the res lt '? the ?'ll';in. 5 er<.

SELECT 'O'::TO_CHAR(sta%t_date_time< 'O') Qt%< TO_CHAR(sta%t_date_time< '!,') da$< CO; T(*)< !E SE_RA S() OJER ( 3ART-T-O B, 'O'::TO_CHAR(sta%t_date_time< 'O') OR!ER B, CO; T(*) !ESC) %ank_Qt%< !E SE_RA S() OJER (OR!ER B, CO; T(*) !ESC) %ank_all FROM en%#llment e< se(ti#n s WHERE s0se(ti#n_id & e0se(ti#n_id 8RO;3 B, 'O'::TO_CHAR(sta%t_date_time< 'O')< TO_CHAR(sta%t_date_time< '!,') OR!ER B, 5< >
QT --Q2 Q2 Q2 DAY COUNT(*) RANK_QTR RANK_ALL --- ---------- -------- ---------MON 42 1 1 TUE 35 2 2 SAT 30 3 3
Web site http !!"""#oracles$lb%exa&ple#co&

Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009) Pa'e */ o) *+
,ersion -ate *!.!20(*

Q2 Q2 Q2 Q2 Q3 Q3

SUN WED FRI THU SAT TUE

29 15 13 13 29 20

4 5 6 6 1 2

4 6 7 7 4 5

9 rows selected

S'l ti'n: The 5 er< .enerates a listin. that sh';s the startin. 5 arter" da< '? the ;ee> '? an< secti'ns and ;ithin the respecti+e 5 arter the n $%er '? enr'll$ents. The R-#PAQTR c'l $n indicates ran>in. '? the enr'll$ent n $%er '? each 5 arter and the R-#PA-LL c'l $n sh';s the ran>in. ?'r all ti$e peri'ds.

Chapter 17: SQL !pti&i=ation


1. /i+en the ?'ll';in. e9ec ti'n plan" descri%e the steps and their 'rder '? e9ec ti'n. SELECT (0(#u%se_n#< (0des(%i"ti#n< i0inst%u(t#%_id FROM (#u%se (< se(ti#n s< inst%u(t#% i WHERE "%e%eQuisite & =B A ! (0(#u%se_n# & s0(#u%se_n# A ! s0inst%u(t#%_id & i0inst%u(t#%_id ....................................................... : -d : O"e%ati#n : ame : ....................................................... : B : SELECT STATEME T : : : 5 : ESTE! LOO3S : : : 4 : ESTE! LOO3S : : : = : TABLE ACCESS B, - !EN ROW-!: CO;RSE : : > : - !EN RA 8E SCA : CRSE_CRSE_FS_- : : 6 : TABLE ACCESS B, - !EN ROW-!: SECT-O : : C : - !EN RA 8E SCA : SECT_CRSE_FS_- : : ? : - !EN ; -O;E SCA : - ST_3S : ....................................................... S'l ti'n: This is a three@ta%le 6'in '? the CO0RSE" SECT3O#" and 3#STR0CTOR ta%les. The ?irst step e9ec ted in the e9ec ti'n plan is the access '? the inde9 CRSEACRSEA(PA3. This inde9 is %ased 'n the 1REREQ03S3TE c'l $n and" there?'re" retrie+es the ROW3Ds '? th'se
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e ** o) *+
,ersion -ate *!.!20(*

rec'rds that satis?< the c'nditi'n WHERE prere5 isite V 38. Then the r';s ;ith these ROW3Ds are retrie+ed ?r'$ the CO0RSE ta%le. The ne9t step is a nested l''p 6'in ;ith the SECT3O# ta%le. ('r each '? the retrie+ed CO0RSE r';s" the inde9 SECTACRSEA(PA3 is pr'%ed %ased 'n 6'in criteria '? s.c' rseAn' V c.c' rseAn'. Lastl<" this res lt is n'; sed ?'r an'ther nested l''p 6'in ;ith the 3#STR0CTOR inde9. #'te that the 5 er< 'nl< re5 ires the se '? the 3#STR0CTORA3D c'l $n" ;hich is als' the 'nl< c'l $n in the SELECT list. There?'re" 'nl< a l''> p '? the +al e in the inde9 is re5 ired" n't the 3#STR0CTOR ta%le. 2. Descri%e the steps '? the ?'ll';in. e9ec ti'n plan.

;3!ATE en%#llment e SET *inal_)%ade & (SELECT JL(AJ8(nume%i(_)%ade)<B) FROM )%ade WHERE e0student_id & student_id A ! e0se(ti#n_id & se(ti#n_id) WHERE student_id & 5BBB A ! se(ti#n_id & 4BBB . !ows %pd ted* .................................................... : -d : O"e%ati#n : ame : .................................................... : B : ;3!ATE STATEME T : : : 5 : ;3!ATE : E ROLLME T : : 4 : - !EN ; -O;E SCA : E R_3S : : = : SORT A88RE8ATE : : : > : TABLE ACCESS B, - !EN ROW-!: 8RA!E : : 6 : - !EN RA 8E SCA : 8R_3S : .................................................... S'l ti'n: This SQL 01D-TE state$ent is a c'rrelated s %5 er<. Y' can .enerate e9plain plans ?'r SQL state$ents 'ther than SELECT state$ents. The e9plain plan sh';s that the WHERE cla se '? the 01D-TE state$ent re?ers t' the pri$ar< >e< c'l $ns '? the E#ROLL&E#T and /R-DE ta%les t' identi?< the r';s and t' deter$ine the ?inal .rade +al es. The inner 5 er< tili=es the inde9 /RA1P" the pri$ar< >e< inde9" and accesses the /R-DE ta%le +ia the ROW3D. Beca se the s %5 er< speci?ies the -2/ ? ncti'n" the step SORT C-//RE/-TED is e9ec ted.
Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e *0 o) *+
,ersion -ate *!.!20(*

#'te" <' ;ill n't see a di??erence in the e9ec ti'n plan %et;een a c'rrelated 01D-TE state$ent and an 01D-TE state$ent ;ith a n'n@ c'rrelated s %5 er<. Y' need t' >eep in $ind" h';e+er" that the c'rrelated pdate ;ill repeatedl< e9ec te the inner 5 er< ?'r e+er< r'; retrie+ed %< the ' ter 5 er<. This is in c'ntrast t' the n'n@ c'rrelated s %5 er< that e9ec tes the inner 5 er< 'nl< 'nce. The c'rrelated s %5 er< c'$%ined ;ith an 01D-TE state$ent is a +er< ?ast ;a< t' pdate data ;ith' t ha+in. t' ;rite a pr'.ra$ t' c'$p te rec'rds ?'r each step. 3. The ?'ll';in. SQL state$ent has an err'r in the hint. C'rrect the state$ent s' Oracle can se the hint. SELECT M*R - !EN (student stu_"k) *M * FROM student s WHERE last_name & 'Smit1' S'l ti'n: The hint d'es n't speci?< the ta%le alias. When a ta%le alias is sed in the state$ent" the hint needs t' re?erence the alias 'ther;ise the hint is i.n'red. Y' c'rrect the state$ent as ?'ll';s. #'te that this is n't a +er< .''d inde9 ch'ice ?'r this 5 er< % t 6 st ill strates h'; <' can direct Oracle t' se a speci?ic inde9. SELECT M*R - !EN (s stu_"k) *M * FROM student s WHERE last_name & 'Smit1'

Solutions to Workshop Exercises - Oracle SQL by Example 4e; Alice Rischert; Prentice Hall (2009)
Web site http !!"""#oracles$lb%exa&ple#co&

Pa'e *+ o) *+
,ersion -ate *!.!20(*

You might also like