Sei sulla pagina 1di 59

Page |1

Additional Lab manual to MOC 2778


Contents
Contents...........................................................................................................................................1
Lab Module1: Using SQL Server Management Studio and SQLCM !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!2 Lab Module2: Quer"ing and #iltering ata !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!$ ata !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1)

Lab Module%: &rou'ing and Summari(ing Lab Module ) *oining

ata +rom Multi'le ,ables!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!2-

Lab Module.: /or0ing 1it2 Sub3ueries!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!%Lab Module$: Modi+"ing ata in ,ables!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!%7

Lab Solutions.................................................................................................................................44
Lab2 Solutions!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! )) Lab % Solutions!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ). Lab ) Solutions!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! )8 Lab. Solutions!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! .% Lab $ Solutions!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! .$

Cornerstone Lab Manual 4ev! A 52--6 Cornerstone! All 4ig2ts 4eserved

Page |2

Lab Module1: Using SQL Server Management Studio and SQLCMD


Objectives Exercise 1: Start using Mi7roso+t SQL Server 2--8 8oo0s Online9 7onsidered as t2e 'rimar" sour7e o+ in+ormation about Mi7roso+t SQL Server 2--8! Exercise 2: /rite a +e1 3ueries to retrieve metadata! Alt2oug2 1e 2ave not "et learned 2o1 to 1rite 3ueries9 t2e s7o'e o+ t2is e:er7ise is to be +amiliar 1it2 t2e tools t2at 7ome along 1it2 SQL Server and ta0e a loo0 at 2o1 to retrieve metadata! Exercise 3: Ma0e use o+ t2e SQLCM ;stimated time to 7om'lete t2is lab Exercise 1 To start using SQL Server 2008 Books Online &o to t2e Microsoft SQL Server 2008 at t2e /indo1s start menu 72oose Documentation and Tutorials and t2en 7li70 SQL Server Books Online C2oose t2e Contents tab at t2e bottom le+t side! ;:'and t2e to' node9 SQL Server 2008 Books Online. Start at t2e to' o+ t2e Contents ,ree9 belo1 t2e se7ond level node Database n!ine "ou 1ill +ind Tec"nical #eference e:'and t2is and t2en e:'and Transact$SQL #eference. ,2is is a 7olle7tion o+ to'i7s 12ere "ou 1ill s'end a lot o+ time! ,a0e a s1i+t s7roll<do1n and reali(e t2at ,<SQL 7ontains a vast amount o+ statements9 o'erators9 0e"1ords and +un7tions! &o to t2e %nde& tab in t2e lo1er le+t! =ere "ou 2ave an inde:ed list o+ 12at "ou 7an en7ounter 12en 1riting 3ueries9 12atever "ou 7an imagine> it is t2ere9 as long it 2as to do 1it2 SQL Server! 2- minutes utilit"!

/rite sel in t2e Look for te:tbo: and noti7e t2at a bun72 o+ to'i7s a''ears t2at ad2eres to t2e S;L;C, statement! Cli70 on S L CT

Page |% %'TO Statement about 18 ro1s do1n9 it 1ill t2en a''ear a list at t2e bottom t2at gives "ou a 7ou'le suggestions! Cli70 on S L CT &am(les )Transact$SQL* +or a 7olle7tion o+ e:am'les S;L;C, statement! /e 1ill ta0e a loo0 on t2at in t2e ne:t module! /rite select statement in t2e Loo0 +or te:tbo: and "ou 1ill re7eive an even broader guide to 12at "ou 7an do in a sele7t statement! Lo7ate t2e Sear72 tab in t2e to'9 rig2t area and 7li70 it! /rite t2e 1ord Quer+ in t2e dro' do1n list bo: and 7li70 t2e button Sear72 to t2e rig2t! A+ter t2e sear72 is 7om'leted "ou re7eive .-- suggestions +or t2e sear72 o+ Quer+! ,2is 7an be tedious to go t2roug29 1e t2ere+ore 7om'lete t2e sear72 string in t2e same manner as man" ot2er sear72 engine 1or0: Ma0e sure "ou 1rite ,uer+ near o(timi-e. "ou t2en +ilter out ?ust about %. 2its +or relevant in+ormation! Add not &ml to t2e sear72 string so it loo0s li0e ,uer+ near o(timi-e not &ml! @ou are t2en do1n on about 16 2its! ,2is s2o1s "ou 2o1 'o1er+ul t2e sear72 me72anism is in SQL Server 8oo0s Online are! ,2is is ver" use+ul 12en s'e7i+i7 in+ormation is o+ interest! Cli70 t2e to'i7 about Quer+ Tunin! #ecommendations and let it load! #ind t2e S+nc /it" table of contents button at t2e toolbar! Cli70 t2at and "ou 1ill +ind t2at ot2er related in+ormation a''ears on t2at se7tion o+ t2e ,able O+ Content! Exercise 2 Start SQL Server Management Studio and 7onne7t t2e Ob?e7t ;:'lorer to t2e database engine using integrated! O'en a 3uer" 1indo19 t2en to t2e ne1 3uer" 1indo1 1rite:

USE master GO SELECT * FROM sys.databases


;:e7ute t2e 3uer" b" 2itting t2e #. 0e"!

,2is 3uer" re3uests in+ormation about t2e databases in t2e s"stem b" retrieving all 7olumns +rom t2e s"s!databases 7atalog vie1! ,a0e a loo0 at t2e result and s7roll to t2e rig2t! ,r" to +ind out 12at databases t2at 2ave t2e recover!"#o$el"$esc 7olumn set to #ULL!

Page |)

Additional in+ormation about t2e s!s $atabases 7atalog vie1 is to be +ound in Mi7roso+t SQL Server 2--. 8oo0s Online! *ust 1rite t2e name o+ t2is vie1 in t2e look %or te:tbo: at t2e &n$ex tab and "ou 1ill +ind it! ,o use a d"nami7 management vie19 1rite t2e +ollo1ing:

SELECT * FROM sys.dm_tran_locks WHERE reso rce_database_!d " #$_%#&'


,2e vie1 s"s!dmAtranAlo70s gat2ers metadata about 7urrent transa7tions in t2e 7urrent database regarding re7ourse lo70ing! ,2e s"stem +un7tion 8AB CD returns t2e identi+i7ation number o+ t2e 7urrent database! Additional in+ormation about d"nami7 management +un7tions is to be +ound in Mi7roso+t SQL Server 2--. 8oo0s Online! ,2e to'i7s regarding Catalog Eie1s and "nami7 Management Eie1s and #un7tions are "our time 1ell invested in9 i+ "ou 72oose to read t2ose! @ou 7an 7lose Management Studio i+ "ou 1is29 1it2out saving t2e 3uer" 1indo1 as a +ile!

Exercice 3 SQL'() utilit! Start a ne1 7ommand 'rom't 1indo1! /rite s3l7md <F to revie1 t2e s1it72es available +or t2e utilit"! @ou 1ill in a moment ma0e use o+ t2e <i and <o s1it72es! #irst "ou 1ill 1or0 intera7tive 1it2 t2e utilit"! C2ange t2e statement to read ?ust s*lc#$ and +it to Enter ke! &% ,ro#,te$ -1./0 t+en !ou #anage$ to connect to local+ost0 $e%ault instance 1it+ 2in$o1s &ntegrate$ securit! 3evie1 t+e ,ro$uct $ocu#entation %or #ore in%or#ation i% necessar! At t+e ,ro#,t 1rite t+e %ollo1ing 4a%ter eac+ line +it t+e Enter ke!5: 1G US; Adventure/or0s2--8 2G S;L;C, name #4OM Produ7tion!Produ7tCategor" %G &O ,2e ans1er s2ould loo0 li0e t2e +ollo1ing: C(an)ed database conte*t to +,d-ent reWorks.//0+. name 11111111111111111111111111111111111111111111111111 ,ccessor!es

Page |. $!kes Clot(!n) Com2onents &3 ro4s a55ected' ;:it t2e SQLCM utilit" b" 1riting *uit and 2itting t2e ;nter 0e"! ;:it t2e 7ommand 'rom't! Start a ne1 3uer" 1indo1 in Management Studio b" 7li70ing t2e button 'e/ Quer+ at t2e to' le+t! /rite t2e +ollo1ing:

USE ,d-ent reWorks.//06 GO SELECT 7ame8 Gro 27ame FROM H manReso rces.#e2artment6
Save t2e +ile to C:Htem'H,2eQuer"!s3l! Start a ne1 7ommand 1indo1 and 1rite t2e +ollo1ing: s3l7md!e:e <i 7:Htem'H,2eQuer"!s3l <o 7:Htem'H,2e4esult!t:t 8ro1se to t2e +older 7:Htem'Hand revie1 t2e te:t+ile! En$ o% lab

Page |$

Lab Module2: Querying and Filtering Data


Objectives ,2e labs in +irst 'art o+ t2is module aim to 'ra7ti7e 12at "ou 2ave learned t2eoreti7all" in 7onte:t o+ retrieving data in various 1a"s! Bn all e:er7ises "ou 1ill 1or0 1it2 t2e Adventure/or0s2--8 sam'le database! @ou 1ill use t2e Produ7tion s72ema +or retrieval o+ data +rom several tables! Suggestions on solutions +or all e:er7ises 1ill be +ound at t2e ver" end o+ t2is 7om'endium! ;stimated time to 7om'lete t2is lab 6ote A series o+ dots CIIIID in result sets indi7ates t2at ro1s 2ave been e:7luded +or 'resentational reasons! Exercise 1 /rite a 3uer" t2at retrieves t2e 7olumns Produ7tB 9 Jame9 Color and ListPri7e +rom t2e Produ7tion!Produ7t table9 1it2 no +ilter! @our result set s2ould loo0 somet2ing li0e t2e +ollo1ing!
9rod ct%# 11111111111 : . < 3 <:= >>? >>= >>@ >>0 >>> 7ame 111111111111111111111111111111 ,d; stable Race $ear!n) $all $$ $all $ear!n) Headset $all $ear!n)s $lade ML $ottom $racket HL $ottom $racket Road1@?/ $lack8 33 Road1@?/ $lack8 30 Road1@?/ $lack8 ?. Color 111111111111111 7ULL 7ULL 7ULL 7ULL 7ULL 7ULL 7ULL $lack $lack $lack L!st9r!ce 11111111111 /8// /8// /8// /8// /8// :/:8.3 :.:83> ?<>8>> ?<>8>> ?<>8>>

). min!

&?/3 ro4&s' a55ected'

Page |7 Exercise 2 Continue to 1or0 1it2 t2e 'revious 3uer" and e:7lude t2ose ro1s t2at are - +or t2e 7olumn ListPri7e! @our result set s2ould loo0 somet2ing li0e t2e +ollo1ing!

9rod ct%# 11111111111 ?:3 ?:? ?:= ?:@ ?:0 >>@ >>0 >>>

7ame 111111111111111111111111111111 LL Mo nta!n Seat ,ssembly ML Mo nta!n Seat ,ssembly HL Mo nta!n Seat ,ssembly LL Road Seat ,ssembly ML Road Seat ,ssembly

Color 111111111111111 7ULL 7ULL 7ULL 7ULL 7ULL $lack $lack $lack

L!st9r!ce 1111111111 :<<8<3 :3@8:3 :>=8>. :<<8<3 :3@8:3 ?<>8>> ?<>8>> ?<>8>>

Road1@?/ $lack8 33 Road1@?/ $lack8 30 Road1@?/ $lack8 ?.

&</3 ro4&s' a55ected'

Exercise 3 Use t2e same 3uer"9 but t2is time "ou ?ust 1ant to see t2e ro1s t2at are JULL +or t2e Color 7olumn! @our result set s2ould loo0 somet2ing li0e t2e +ollo1ing!
9rod ct%# 11111111111 : . < 3 <:= >3@ >>3 >>? >>= 7ame 111111111111111111111111111111 ,d; stable Race $ear!n) $all $$ $all $ear!n) Headset $all $ear!n)s $lade Color 111111111111111 7ULL 7ULL 7ULL 7ULL 7ULL 7ULL 7ULL 7ULL 7ULL L!st9r!ce 11111111111 /8// /8// /8// /8// /8// >:8?@ ?<8>> :/:8.3 :.:83>

HL LL ML HL To r!n) Handlebars $ottom $racket $ottom $racket $ottom $racket

&.30 ro4&s' a55ected'

Exercise 7 Use t2e same 3uer"9 but t2is time "ou ?ust 1ant to see t2e ro1s t2at are not JULL +or t2e Color 7olumn! @our result set s2ould loo0 somet2ing li0e t2e +ollo1ing!
9rod ct%# 11111111111 <:@ <:0 <:> <./ >>. >>< >>@ >>0 7ame 111111111111111111111111111111 LL Crankarm ML Crankarm HL Crankarm C(a!nr!n) $olts Color 111111111111111 $lack $lack $lack S!l-er $lack $lack $lack $lack L!st9r!ce 11111111111 /8// /8// /8// /8// ?<>8>> ?<>8>> ?<>8>> ?<>8>>

Mo nta!n1?// $lack8 30 Mo nta!n1?// $lack8 ?. Road1@?/ $lack8 33 Road1@?/ $lack8 30

Page |8
>>> Road1@?/ $lack8 ?. $lack ?<>8>>

&.?= ro4&s' a55ected'

Exercise 8 Jo19 7ombine t1o sear72 arguments in t2e 3uer" "ou 2ave been 1or0ing 1it2! @ou ?ust 1ant to see t2e ro1s t2at are not JULL +or t2e 7olumn Color9 and t2e 7olumn ListPri7e 2as a value greater t2an (ero! @our result set s2ould loo0 somet2ing li0e t2e +ollo1ing!
9rod ct%# 11111111111 =0/ @/= @/@ @/0 >>< >>@ >>0 >>> 7ame 111111111111111111111111111111 HL Road Frame 1 $lack8 ?0 HL Road Frame 1 Red8 ?0 S2ort1:// Helmet8 Red S2ort1:// Helmet8 $lack Color 111111111111111 $lack Red Red $lack $lack $lack $lack $lack L!st9r!ce 1111111111 :3<:8?/ :3<:8?/ <38>> <38>> ?<>8>> ?<>8>> ?<>8>> ?<>8>>

Mo nta!n1?// $lack8 ?. Road1@?/ $lack8 33 Road1@?/ $lack8 30 Road1@?/ $lack8 ?.

&.3? ro4&s' a55ected'

Exercise 9 Jo1 1e 1ant a re'ort t2at 7on7atenates t2e 7olumns Jame and Color +rom t2e Produ7tion!Produ7t table! @our result set s2ould loo0 somet2ing li0e t2e +ollo1ing! Ma0e sure "ou e:7lude ro1s t2at are JULL +or t2e 7olumn Color! Also noti7e t2e 7olumn name!
7ame ,nd Color 111111111111111111111111111111111111111111 LL Crankarm A $lack ML Crankarm A $lack HL Crankarm A $lack C(a!nr!n) $olts A S!l-er C(a!nr!n) 7 t A S!l-er C(a!nr!n) A $lack Mo nta!n1?// $lack8 33 A $lack Mo nta!n1?// $lack8 30 A $lack Mo nta!n1?// $lack8 ?. A $lack Road1@?/ $lack8 33 A $lack Road1@?/ $lack8 30 A $lack Road1@?/ $lack8 ?. A $lack &.?= ro4&s' a55ected'

Page |6

Exercise : Customi(e t2e 'revious 3uer" so t2e ans1er loo0s li0e t2e +ollo1ing!
7ame ,nd Color 11111111111111111111111111111111111111111111111111 7,MEA LL Crankarm 11 COLORA $lack 7,MEA ML Crankarm 11 COLORA $lack 7,MEA HL Crankarm 11 COLORA $lack 7,MEA C(a!nr!n) $olts 11 COLORA S!l-er 7,MEA C(a!nr!n) 7 t 11 COLORA S!l-er 7,MEA C(a!nr!n) 11 COLORA $lack

7,MEA 7,MEA 7,MEA 7,MEA 7,MEA Mo nta!n1?// $lack8 Mo nta!n1?// $lack8 Road1@?/ $lack8 33 Road1@?/ $lack8 30 Road1@?/ $lack8 ?. 30 ?. 11 11 11 11 COLORA $lack 11 COLORA $lack COLORA $lack COLORA $lack COLORA $lack

&.?= ro4&s' a55ected'

Exercise 8 Jo1 1e 1ould li0e to see t2e 7olumns Produ7tB and Jame +rom t2e Produ7tion!Produ7t table +iltered b" Produ7tB +rom )-- to .--! /rite a 3uer" t2at ma0es "our result set loo0 somet2ing li0e t2e +ollo1ing! ,r" to ma0e "our /=;4; 7lause as sim'le and readable as 'ossible!
9rod ct%# 11111111111 3// 3/: 3/. 3/< 3/3 3/? 3/= 3>3 3>? 3>= 3>@ 7ame 11111111111111111111111111111 LL H b HL H b Beyed Was(er E*ternal Lock Was(er < E*ternal Lock Was(er 3 E*ternal Lock Was(er > E*ternal Lock Was(er ? 9a!nt 9a!nt 9a!nt 9!nc( 1 S!l-er 1 $l e 1 Cello4 $olt

&>0 ro4&s' a55ected'

Exercise ; /e 1ould li0e to see t2e 7olumns Produ7tB 9 Jame and 7olor +rom t2e Produ7tion!Produ7t table restri7ted to t2e 7olors bla70 and blue! /rite a 3uer" t2at ma0es "our result set loo0 somet2ing li0e t2e +ollo1ing! ,r" to ma0e "our /=;4; 7lause as sim'le and readable as 'ossible!

P a g e | 1-

9rod ct%# 11111111111 <:@ <:0 <:> <.. =0/ @/0 @:: @..

7ame 11111111111111111111111111111111111 LL Crankarm ML Crankarm HL Crankarm C(a!nr!n) HL Road Frame 1 $lack8 ?0 S2ort1:// Helmet8 $lack S2ort1:// Helmet8 $l e LL Road Frame 1 $lack8 ?0 Mo nta!n1?// $lack8 30 Mo nta!n1?// $lack8 ?. Road1@?/ $lack8 33 Road1@?/ $lack8 30 Road1@?/ $lack8 ?.

color 11111111 $lack $lack $lack $lack $lack $lack $l e $lack $lack $lack $lack $lack $lack

>>. >>< >>@ >>0 >>>

&::> ro4&s' a55ected'

Exercise 10 2il$car$s ,2is e:er7ise and t2e ne:t t2ree +ollo1ing 1ill ma0e use o+ 1ild7ards in ,ransa7t<SQL! ,o begin 1it29 1e 1ould li0e a re'ort on 'rodu7ts t2at begins 1it2 t2e letter S! /rite a 3uer" t2at retrieves t2e 7olumns Jame and ListPri7e +rom t2e Produ7tion!Produ7t table! @our result set s2ould loo0 somet2ing li0e t2e +ollo1ing! Order t2e result set b" t2e Jame 7olumn!
7ame 11111111111111111111111111111111111111111111111111 Seat L ) Seat 9ost Seat Stays Seat T be S(ort1Slee-e Class!c Dersey8 L S(ort1Slee-e Class!c Dersey8 M L!st9r!ce 11111111111 /8// /8// /8// /8// ?<8>> ?<8>> <38>> <38>> /8// /8//

S2ort1:// Helmet8 $l e S2ort1:// Helmet8 Red Steerer Stem &:3 ro4&s' a55ected'

Exercise 11 Jo1 1e 1ould li0e a re'ort on 'rodu7ts t2at begins 1it2 t2e letters S or A! /rite a 3uer" t2at retrieves t2e 7olumns Jame and ListPri7e +rom t2e Produ7tion!Produ7t table! @our result set s2ould loo0 somet2ing li0e t2e +ollo1ing! Order t2e result set b" t2e Jame 7olumn!

P a g e | 11
7ame 11111111111111111111111111111111111111111111111111 ,d; stable Race ,ll19 r2ose $!ke Stand ,WC Lo)o Ca2 Seat L ) Seat 9ost L!st9r!ce 1111111111 /8// :?>8// 08>> /8// /8// <38>> /8// /8//

S2ort1:// Helmet8 Red Steerer Stem &:@ ro4&s' a55ected'

Exercise 12 Ad?ust "our 3uer" so "ou retrieve ro1s t2at 2ave a Jame t2at begins 1it2 t2e letters SPO9 but is t2en not +ollo1ed b" t2e letter K! A+ter t2is (ero or more letters 7an e:ists! Order t2e result set b" t2e 'ame 7olumn!
7ame 11111111111111111111111111111111111111111111111111 S2ort1:// Helmet8 $lack S2ort1:// Helmet8 $l e S2ort1:// Helmet8 Red &< ro4&s' a55ected' L!st9r!ce 11111111111 <38>> <38>> <38>>

Exercise 13 /rite a 3uer" t2at retrieves uni,ue 7olors +rom t2e table Produ7tion!Produ7t! /e do not 1ant to see all t2e ro1s9 ?ust 12at 7olors t2at e:ist in t2e 7olumn Color! @our result set s2ould loo0 somet2ing li0e t2e +ollo1ing!
Color 111111111111111 7ULL $lack $l e Grey M lt! Red S!l-er S!l-erE$lack W(!te Cello4 &:/ ro4&s' a55ected'

Exercise 17 /rite a 3uer" t2at retrieves t2e uni3ue 7ombination o+ 7olumns Produ7tSub7ategor"B and Color +rom t2e Produ7tion!Produ7t table! #ormat and sort so t2e result set a77ordingl" to t2e +ollo1ing! /e do not 1ant an" ro1s t2at are JULL!in an" o+ t2e t1o 7olumns in t2e result!

P a g e | 12
9rod ctS bcate)ory%# 11111111111111111111 : : . . . < <: <: <: <. <? Color 111111111111111 S!l-er $lack Cello4 Red $lack Cello4 Red $l e $lack S!l-er Grey

&<3 ro4&s' a55ected'

Exercise 18 Somet2ing is L1rongM 1it2 t2e /=;4; 7lause in t2e +ollo1ing 3uer"! /e do not 1ant an" 4ed or 8la70 'rodu7ts +rom an" SubCategor" t2an t2ose 1it2 t2e value o+ 1 in 7olumn Produ7tSubCategor"B 9 unless t2e" 7ost bet1een 1--- and 2---! 6ote: ,2e L;#,CD +un7tion 1ill be 7overed in a +ort27oming module! SELECT 9rod ctS bCate)ory%# 8 LEFT&F7ameG8<?' ,S F7ameG 8 Color8 L!st9r!ce FROM 9rod ct!on.9rod ct WHERE Color %7 &+Red+8+$lack+' OR L!st9r!ce $ETWEE7 :/// ,7# ./// ,7# 9rod ctS bCate)ory%# " : OR#ER $C 9rod ct%# /rite t2e 3uer" in t2e editor and e:e7ute it! ,a0e a loo0 at t2e result set and t2en ad?ust t2e 3uer" so it delivers t2e +ollo1ing result set! Ti,: O'erator 're7eden7e is o+ten a sour7e o+ 7on+usion!

9rod ctS bCate)ory%# 11111111111111111111 :3 :3

7ame 11111111111111111111111111111111111 HL Road Frame 1 $lack8 ?0 HL Road Frame 1 Red8 ?0

Color 111111111111111 $lack Red

L!st9r!ce 111111111 :3<:8?/ :3<:8?/

P a g e | 1%
:3 :3 :3 :3 :3 :. :. :. . . : : : : : &=< ro4&s' a55ected' HL HL HL HL HL HL HL HL Road Frame 1 Red8 =. Road Frame 1 Red8 33 Road Frame 1 Red8 30 Road Frame 1 Red8 ?. Road Frame 1 Red8 ?= Mo nta!n Frame 1 S!l-er8 3. Mo nta!n Frame 1 S!l-er8 33 Mo nta!n Frame 1 S!l-er8 30 ...... Road1<?/1W Cello48 33 Road1<?/1W Cello48 30 Mo nta!n1?// $lack8 3/ Mo nta!n1?// $lack8 3. Mo nta!n1?// $lack8 33 Mo nta!n1?// $lack8 30 Mo nta!n1?// $lack8 ?. Red Red Red Red Red S!l-er S!l-er S!l-er Cello4 Cello4 $lack $lack $lack $lack $lack :3<:8?/ :3<:8?/ :3<:8?/ :3<:8?/ :3<:8?/ :<=38?/ :<=38?/ :<=38?/ :@//8>> :@//8>> ?<>8>> ?<>8>> ?<>8>> ?<>8>> ?<>8>>

Exercise 19

Use the Production.Product table to return product name, color and list price for each product. For the color column, where there is NULL, replace it with the strin Unknown. 7ame 111111111111111111111111111111 ,d; stable Race $ear!n) $all $$ $all $ear!n) Headset $all $ear!n)s ..... HL $ottom $racket Road1@?/ $lack8 33 Road1@?/ $lack8 30 Road1@?/ $lack8 ?. &?/3 ro4&s' a55ected' Color 111111111111111 Unkno4n Unkno4n Unkno4n Unkno4n Unkno4n $lack $lack $lack L!st9r!ce 1111111111 /8// /8// /8// /8// :.:83> ?<>8>> ?<>8>> ?<>8>>

P a g e | 1)

Lab Module3: Grouping and Summari ing Data


Objectives !o ma"e use of #arious functions for manipulation and calculations on data. $n these e%ercises we will &uer' some tables in the Production schema in the (d#enture)or"s*++, sample database. !he last two e%ercises are more ad#anced. Estimated time to complete this lab Solutions .ou will find solution su estion at the end of this lab manual. Exercise 1 /ow man' products can 'ou find in the Production.Product table0 .our result set should loo" li"e the followin .
11111111111 ?/3 &: ro4&s' a55ected'

-+ minutes

Exercise 2 )rite a &uer' that retrie#es the number of products in the Production.Product table that are included in a subcate or'. !he rows that ha#e NULL in column ProductSubcate or'$1 are considered to not be a part of an' subcate or'. HasS bCate)ory%# 1111111111111111 .>? &: ro4&s' a55ected' Notice that the result has a column name. (lso ta"e a loo" at the messa e 'ou recei#e from S2L Ser#er Exercise 3 /ow man' Products reside in each SubCate or'0 !he answer to this is retrie#able if 'ou write a &uer' that use the C3UN! a re ate function combined with a 453UP 6. clause. !he column ProductSubcate or'$1 is a candidate for buildin roups of rows when &uer'in the Production.Product table. .our result set should loo" somethin li"e the result below. Notice the column alias for the second column.

P a g e | 1.

9rod ctS bcate)ory%# 11111111111111111111 7ULL : . < 3 <3 <? <= <@ &<0 ro4&s' a55ected' Exercise 4

Co nted9rod cts 111111111111111 ./> <. 3< .. 0 : : . ::

!r' to write two different &ueries to find out how man' products that do not ha#e a product subcate or'. 3ne &uer' without the )/757 clause and one &uer' usin a )/757 clause. !he rows that ha#e NULL in column ProductSubcate or'$1 are considered to not be a part of an' subcate or'. Tip8 $n the first we can use a difference between e%pressions to calculate the fi ure. 11111111111 ./> &: ro4&s' a55ected' !he other wa' usin a )/757 clause. 3bser#e the column alias. 7oS bCat 11111111111 ./> &: ro4&s' a55ected' Exercise 5 ( report is needed, the summar' of products in stoc". )rite a &uer' a ainst another table this time, the Production.Product$n#entor' table. 9rod ct%# 11111111111 : . < 3 <:= >>@ >>0 >>> T(eS m 11111111111 :/0? ::/> :<?. :<.. :<=: :?< :?? :>3

&3<. ro4&s' a55ected'

P a g e | 1$
Exercise 6 Continue to write on the &uer' in pre#ious e%ercise. (dd a )/757 clause that e%tracts the rows that ha#e the column Location$1 set to 4+ and limit the result to include 9ust summari:ed &uantities less then 1++. !ip8 now is the time to see the /(;$N4 clause in action. 9rod ct%# 11111111111 3>. 3>< 3>3 3>? 3>= 0:3 >3< T(eS m 11111111111 := .0 3 .? 33 >= 00

&@ ro4&s' a55ected' Exercise 7 $n this &uer' we also want to see what shelf the product is to be deli#ered from. (dd code to the pre#ious &uer'. S(el5 1111111111 $ $ $ $ $ 7E, 7E, 9rod ct%# 11111111111 3>. 3>< 3>3 3>? 3>= 0:3 >3< T(eS m 11111111111 := .0 3 .? 33 >= 00

&@ ro4&s' a55ected' Exercise )e would li"e to see the a#era e &uantit' for products where column Location$1 has the #alue of 1+. !he table Production.Product$n#entor' has the answer. T(e,-) 11111111111 .>? &: ro4&s' a55ected' Exercise ! !o continue to write on the pre#ious &uer', we would li"e to see the result b' shelf e%cludin rows that has the #alue of N<( in the column Shelf. )e also want to see a total a#era e based on shelf onl' and also for all products => rand total?@.

P a g e | 17
9rod ct%# S(el5 11111111111 1111111111 7ULL 7ULL 3@= , 3@@ , 3@0 , 7ULL , <:= $ <>0 $ 0:< E >3= E >3@ E 7ULL E <.@ F 7ULL F 30. L 30< L 303 L 30? L 30= L 30@ L 300 L 7ULL L &3@ ro4&s' a55ected' Exercise 1" )e want to "now number of members =rows@ and a#era e list price in the Production.Product table. !his should be rouped independentl' o#er the Color and the Class column. )e are not interested in an' rows where Color nor Class are null =)/757 Class $S N3! NULL (N1 Color $S N3! NULL@. /int8 thin" 453UP$N4 S7!S. Color 111111111111111 7ULL 7ULL 7ULL $lack $l e Red S!l-er S!l-erE$lack Cello4 Class 11111 H L M 7ULL 7ULL 7ULL 7ULL 7ULL 7ULL T(eCo nt 11111111111 @: @= ?. @. .. <@ </ = <. ,-)9r!ce 111111111111111111111 :>.?8:3= 3=:8.0:: 0/>8=@/? >:@8@./> :/0:8<@:< :3<080>30 :./.8/=? =:8:> :/@.8..> T(e,-) 11111111111 <.0 3/3 <?< =.. 3?> <00 3/3 .?: .30 .33 .?? 33< 33< :@= 3?> :>= :@= 3?@ <.3 </? .>>

&> ro4&s' a55ected' Exercise 11 )e now want to e%amine the function 453UP$N4. !he followin &uer' enerates the result below the &uer' itself. !a"e a loo" and complete the &uer' so it results to the second result set. SELECT 9rod ctS bcate)ory%# 8 COU7T&7ame' as Co nted FROM 9rod ct!on.9rod ct

P a g e | 18
GROU9 $C ROLLU9 &9rod ctS bcate)ory%#' 9rod ctS bcate)ory%# 11111111111111111111 7ULL : . < 3 <. << <3 <? <= <@ 7ULL &<> ro4&s' a55ected' 6elow 'ou find the second result set, continue to write a complete &uer' so it i#es the followin answer. !he added column is for clarit' re ardin NULL. 9rod ctS bcate)ory%# 11111111111111111111 7ULL 7ULL : . < <3 <? <= <@ &<> ro4&s' a55ected' Co nted 11111111111 ./> ?/3 <. 3< .. : : . :: %sGrandTotal 11111111111 / : / / / / / / / Co nted 11111111111 ./> <. 3< .. 0 : < : : . :: ?/3

#an$ %unctions
Exercise 12 Use the ProductCate or' table in the Production schema to return each product name. )e also want them numbered, in alphabetical order8 Use the 53)ANUB675 function. #!ctOrder 11111111111111111111 : . < 3 &3 ro4&s' a55ected' Exercise 13 Use the 5(NC function to create the followin result set. .ou are supposed to ran" products b' their &uantit' in stoc" on different locations. 7ame 111111111111111 ,ccessor!es $!kes Clot(!n) Com2onents

P a g e | 16

Tip8 Choose Location$1 for the partitionin , on 2uantit' in descendin order. 3rder the result b' Location$1, 5(NC. (lso limit the result set to onl' show Location$1Ds *, E and 4.
7ame 11111111111111111111 Metal S(eet < Metal 9late : Metal S(eet ? Metal $ar . Metal 9late . Metal 9late < Metal S(eet @ Metal $ar : Metal S(eet = Metal S(eet . Metal S(eet 3 Metal S(eet : 9a!nt 1 S!l-er 9a!nt 1 $l e 9a!nt 1 Red 9a!nt 1 Cello4 9a!nt 1 $lack 9a!nt 1 $l e 9a!nt 1 Cello4 9a!nt 1 Red 9a!nt 1 $lack 9a!nt 1 S!l-er &.. ro4&s' a55ected' Locat!on%# 1111111111 . . . . . . . . . . . . < < < < < 3 3 3 3 3 H ant!ty 11111111 =>: =0> =0> 3>@ 33: 3.@ 3.@ 3/< <<: <.: <.: <:. 3> 3> 3: </ :@ <? .? .3 :3 :. R,7B 11111111111111111111 : . . 3 ? = = 0 > :/ :/ :. : : < 3 ? : . < 3 ?

P a g e | 2-

Lab Module ! "oining Data #rom Multiple $ables


Objectives &ab 'ns(ers Estimated time to complete the lab !he labs in this module are di#ided into two parts. !he first part is aimed for practicin 9oins, 9ust 9oin. !he second part is incorporatin 9oin and a re ations. Su estions for solutions to the e%ercises can be found at the end of this lab manual. F+ minutes.

<art one0 solel! =O&6 exercises


Exercise 1 )rite a &uer' that lists the countr' and pro#ince names stored in (d#enture)or"s*++, sample database. $n the Person schema 'ou will find the Countr'5e ion and StatePro#ince tables. Goin them and produce a result set similar to the followin . Notice that there is no particular sort order in the result set. Co ntry 111111111111111111111111111111 Canada Un!ted States Un!ted States Un!ted States ,mer!can Samoa France France France France France France &:0: ro4&s' a55ected' Exercise 2 Continue to wor" with the pre#ious &uer' and add a filter to onl' list the countries 4erman' and Canada. (lso notice the sort order and column headin s of the result set. .our result set should loo" similar to the followin . Co ntry 111111111111111111111111111111 Canada Canada Canada Canada Canada Canada Germany Germany 9ro-!nce 111111111111111111111111 ,lberta $r!t!s( Col mb!a $r ns4!ck Labrador Man!toba 7e45o ndland $randenb r) Hamb r) 9ro-!nce 111111111111111111111111 ,lberta ,laska ,labama ,rkansas ,mer!can Samoa $el5ord &Terr!to!re de' Essonne Ha ts de Se!ne Se!ne Sa!nt #en!s Ial de Marne Ial d+O!se

P a g e | 21
Germany Germany Germany Germany &./ ro4&s' a55ected Exercise 3 )e want information about orders. From the Sales.Sales3rder/eader table we want the Sales3rder$1, 3rder1ate and SalesPerson$1 colums. From the Sales.SalesPerson table we want the 6usiness7ntit'$1 =which identifies the sales person@, 6onus and the Sales.!1 =how much this person sold for 'et this 'ear@ columns. =(s an aside, note that 9oinin Sales3rder/eader to SalesPerson will restrict the result to nonH$nternet orders =order processed on the $nternet has 1 in the 3nline3rderFla , and has NULL for the SalesPerson$1 column.@ Note that the time portion below has been remo#ed from the 3rder1ate column for presentation purposes. Hessen 7ordr(e!n1West5alen Saarland Sa*ony

SalesOrder%# Order#ate 111111111111 11111111111 111111111111111111111 3<=?> .//:1/@1/: 3<==/ .//:1/@1/: 3<==: .//:1/@1/: 3<==. .//:1/@1/: 3<==< .//:1/@1/: 3<==3 .//:1/@1/: ..... @:>3> .//31/=1/: @:>?/ .//31/=1/: @:>?: .//31/=1/: @:>?. .//31/=1/: &<0/= ro4&s' a55ected'

Sales9erson%# $ s!nessEnt!ty%# $on s SalesCT# 1111111111111 1111111111111111 1111111111 .@> .@> .0. .0. .@= .0/ .@@ .@> .@> .@? .@> .@> .0. .0. .@= .0/ .@@ .@> .@> .@? =@//8// =@//8// ?///8// ?///8// .///8// ?///8// .?//8// =@//8// =@//8// 3://8// .0::/:.8@:?: .0::/:.8@:?: <:0><?=8.3=? <:0><?=8.3=? ?.//3@?8.<:< /8// <0?@:=<8=<<. .0::/:.8@:?: .0::/:.8@:?: 3??@/3?8/3?>

Exercise 4 Use abo#e &uer', add Gob!itle and remo#e the SalesPerson$1 and the 6usiness7ntit'$1 columns. .ou need to 9oin to the /uman5esources.7mplo'ee table. SalesOrder%# 111111111111 3<=?> 3<==/ 3<==: 3<==. ...... @:>3@ @:>30 @:>3> @:>?/ @:>?: @:>?. Order#ate 11111111111 .//:1/@1/: .//:1/@1/: .//:1/@1/: .//:1/@1/: .//31/=1/: .//31/=1/: .//31/=1/: .//31/=1/: .//31/=1/: .//31/=1/: Dobt!tle 11111111111111111111 Sales Re2resentat!-e Sales Re2resentat!-e Sales Re2resentat!-e Sales Re2resentat!-e Sales Sales Sales Sales Sales Sales Re2resentat!-e Re2resentat!-e Re2resentat!-e Re2resentat!-e Re2resentat!-e Re2resentat!-e $on s 11111111 =@//.// =@//.// ?///.// ?///.// .?//.// =@//.// .?//.// =@//.// =@//.// 3://.// SalesCT# 11111111111111111 .0::/:.8@:?: .0::/:.8@:?: <:0><?=8.3=? <:0><?=8.3=? <0?@:=<8=<<. .0::/:.8@:?: <0?@:=<8=<<. .0::/:.8@:?: .0::/:.8@:?: 3??@/3?8/3?>

P a g e | 22
&<0/= ro4&s' a55ected' Exercise 5 Now use abo#e &uer' and 9oin to the Person.Person table. (dd the FirstName and LastName column and remo#e the Gob!itle, and Sales.!1 columns. $f 'ou stud' the forei n "e' relationships between the tables, 'ou mi ht notice that there is no direct forei n "e' relationship between the 7mplo'ee and the Person table. 6ut there is an indirect relationship throu h the 6usiness7ntit' table, since shince this is oneHtoHone relationship to both Person and 7mplo'ee, we actuall' donIt need this table in our &uer'.

SalesOrder%# 111111111111 3<=?> 3<==/ 3<==: 3<==. 3<==< ......... @:>3= @:>3@ @:>30 @:>3> @:>?/ @:>?: @:>?.

Order#ate 11111111111 .//:1/@1/: .//:1/@1/: .//:1/@1/: .//:1/@1/: .//:1/@1/: .//31/=1/: .//31/=1/: .//31/=1/: .//31/=1/: .//31/=1/: .//31/=1/: .//31/=1/:

F!rst7ame 1111111111 Ts-! Ts-! DosJ DosJ L!nda DosJ D!ll!an Ts-! D!ll!an Ts-! Ts-! M!c(ael

Last7ame 111111111111111 Re!ter Re!ter Sara!-a Sara!-a M!tc(ell Sara!-a Carson Re!ter Carson Re!ter Re!ter $lyt(e

$on s 11111111 =@//.// =@//.// ?///.// ?///.// .///.// ?///.// .?//.// =@//.// .?//.// =@//.// =@//.// 3://.//

&<0/= ro4&s' a55ected'

Exercise 6 Since we donIt return an' columns from the 7mplo'ee table, and the relationships for the 6usiness7ntit'$1 column are oneHtoHone, we actuall' donIt need the 7mplo'ee table in the &uer'. 5eHwrite abo#e &uer' to that 'ou remo#e the 7mplo'ee table from the &uer', and ma"e sure that the result is the same as from abo#e &uer'. Exercise 7 Now we donIt want the 6onus column an'more. ( ain, we can remo#e table from the &uer', because we ha#e oneHtoHone relationships. 5emo#e the 6onus column and the references to the SalesPerson table. SalesOrder%# 111111111111 3<=?> 3<==/ 3<==: 3<==. Order#ate 11111111111 .//:1/@1/: .//:1/@1/: .//:1/@1/: .//:1/@1/: F!rst7ame 1111111111 Ts-! Ts-! DosJ DosJ Last7ame 111111111111111 Re!ter Re!ter Sara!-a Sara!-a

P a g e | 2%
...... @:>30 @:>3> @:>?/ @:>?: @:>?.

.//31/=1/: .//31/=1/: .//31/=1/: .//31/=1/: .//31/=1/:

Ts-! D!ll!an Ts-! Ts-! M!c(ael

Re!ter Carson Re!ter Re!ter $lyt(e

&<0/= ro4&s' a55ected' Exercise )e also want to see order details information. Use abo#e &uer' and 9oin it to the Sales.Sales3rder1etail table, from which 'ou return the Product$1 and 3rder2t' column. (lso, concatenate the FirstName and LastName columns into one column named SalesPerson. 3rder the rows b' the 3rder1ate, Sales3rder$1 columns.

SalesOrder%# 111111111111 3<=?> 3<=?> 3<=?> 3<=?> 3<=?> 3<=?> ...... @:>?. @:>?. @:>?. @:>?. @:>?. @:>?. @:>?. @:>?.

Order#ate 11111111111 .//:1/@1/: .//:1/@1/: .//:1/@1/: .//:1/@1/: .//:1/@1/: .//:1/@1/: .//31/=1/: .//31/=1/: .//31/=1/: .//31/=1/: .//31/=1/: .//31/=1/: .//31/=1/: .//31/=1/:

Sales9erson 11111111111111111 Ts-! Re!ter Ts-! Re!ter Ts-! Re!ter Ts-! Re!ter Ts-! Re!ter Ts-! Re!ter M!c(ael M!c(ael M!c(ael M!c(ael M!c(ael M!c(ael M!c(ael M!c(ael $lyt(e $lyt(e $lyt(e $lyt(e $lyt(e $lyt(e $lyt(e $lyt(e

9rod ct%# 11111111111 @@= @@@ @@0 @@: @@. @@< >:/ >.3 >.= >./ @3< @3. >>3 >0?

OrderHty 11111111 : < : : : . 3 3 ? . : 3 < <

&=/>:> ro4&s' a55ected'

Exercise !

Now we want the name of the product instead of the Product$1 column. .ou can et this b' 9oinin to the Production.Product table.
Order#ate 11111111111 .//:1/@1/: .//:1/@1/: .//:1/@1/: .//:1/@1/: .//:1/@1/: .//31/=1/: .//31/=1/: .//31/=1/: .//31/=1/: .//31/=1/: Sales9erson 1111111111111111 Ts-! Re!ter Ts-! Re!ter Ts-! Re!ter Ts-! Re!ter Ts-! Re!ter M!c(ael M!c(ael M!c(ael M!c(ael M!c(ael $lyt(e $lyt(e $lyt(e $lyt(e $lyt(e 9rod ct7ame 1111111111111111111111111111111 Mo nta!n1:// $lack8 3. Mo nta!n1:// $lack8 33 Mo nta!n1:// $lack8 30 Mo nta!n1:// S!l-er8 <0 Mo nta!n1:// S!l-er8 3. LL LL HL HL LL Mo nta!n Frame Mo nta!n Frame Mo nta!n Frame Mo nta!n Frame $ottom $racket 1 1 1 1 $lack8 30 S!l-er8 ?. $lack8 3. S!l-er8 3= OrderHty 11111111 : < : : : ? . : 3 <

SalesOrder%# 111111111111 3<=?> 3<=?> 3<=?> 3<=?> 3<=?> ...... @:>?. @:>?. @:>?. @:>?. @:>?.

P a g e | 2)
@:>?. .//31/=1/: M!c(ael $lyt(e Mo nta!n1?// S!l-er8 3. <

&=/>:> ro4&s' a55ected'

Exercise 1" Now 'ou want to use abo#e and limit so 'ou8 1. 3nl' see order with order #alue o#er 1++ +++ =Sub!otal column in order header table@ *. 3nl' see orders with order date of 'ear *++4. .ou can either limit this usin the 1(!7P(5! function to return the 'ear, or 'ou can use a ran e in the )/757 clause, as described in http8<<www."aras:i.com<S2LSer#er<infoAdatetime.asp, the >Searchin for datetime #alues? section.

SalesOrder%# 111111111111 =::03 =::03 =::03 =::03 =::03 =::03 =::03 ...... @:03@ @:03@ @:03@ @:03@ @:03@ @:03@

Order#ate 11111111111 .//31/:1/: .//31/:1/: .//31/:1/: .//31/:1/: .//31/:1/: .//31/:1/: .//31/:1/: .//31/=1/: .//31/=1/: .//31/=1/: .//31/=1/: .//31/=1/: .//31/=1/:

Sales9erson 1111111111111111 S( %to S( %to S( %to S( %to S( %to S( %to S( %to Dae Dae Dae Dae Dae Dae 9ak 9ak 9ak 9ak 9ak 9ak

9rod ct7ame 1111111111111111111111111111111 S(ort1Slee-e Class!c Dersey8 S To r!n)1./// $l e8 3= To r!n)1</// $l e8 ?/ LL To r!n) Frame 1 $l e8 ?3 To r!n)1</// Cello48 ?3 To r!n)1./// $l e8 =/ To r!n)1:/// $l e8 =/ To HL HL To To HL r!n)1./// $l e8 =/ To r!n) Frame 1 $l e8 =/ To r!n) Handlebars r!n)1./// $l e8 3= r!n)1:/// Cello48 ?/ To r!n) Frame 1 Cello48 =/

OrderHty 11111111 ? < :: : ? 3 0 = = < 3 . 3

&=>? ro4&s' a55ected'

Exercise 11 )e want to see information about countries and pro#inces. Goin the Countr'5e ion and the StatePro#ince tables =both in the Person schema@. Note that we want to "eep the countries for which there are no pro#incesJ Sort the result on countr' name and pro#ince name. Co ntry7ame 1111111111111111111111111 ,5)(an!stan ,lban!a ,l)er!a ,mer!can Samoa ,ndorra ,n)ola ...... ,rmen!a ,r ba , stral!a 9ro-!nce7ame 11111111111111111111 7ULL 7ULL 7ULL ,mer!can Samoa 7ULL 7ULL 7ULL 7ULL 7e4 So t( Wales

P a g e | 2.
, stral!a , stral!a , stral!a , stral!a ...... I!r)!n %slands8 $r!t!s( I!r)!n %slands8 U.S. Wall!s and F t na Cemen Kamb!a K!mbab4e &3/@ ro4&s' a55ected' H eensland So t( , stral!a Tasman!a I!ctor!a 7ULL I!r)!n %slands 7ULL 7ULL 7ULL 7ULL

Exercise 12 )rite a &uer' that retrie#es customers that ha#e not 'et placed an order. !his can be done usin an outer 9oin because the customer e%ists in the Sales.Customer table but not in the Sales.Sales3rder/eader table. .our result set should loo" similar to the followin . C stomer%# 11111111111 : . < 3 ? = ...... =>@ =>0 =>> @// @/: SalesOrder%# 111111111111 7ULL 7ULL 7ULL 7ULL 7ULL 7ULL 7ULL 7ULL 7ULL 7ULL 7ULL

&@/: ro4&s' a55ected' Exercise 13 Usin a full 9oin we can retrie#e a result set listin products that ha#e no product model name and product model names that is not assi ned to a specific product. )rite a &uer' that deli#ers the followin result set b' usin the tables Production.Product and Production.ProductBodel 9rod ct7ame 111111111111111111111111111111 ,d; stable Race $ear!n) $all $$ $all $ear!n) Headset $all $ear!n)s $lade LL Crankarm 9rod ctModel7ame 111111111111111111111 7ULL 7ULL 7ULL 7ULL 7ULL 7ULL

P a g e | 2$
ML Crankarm 7ULL 7ULL 7ULL 7ULL 7ULL 7ULL 7ULL 7ULL &.:0 ro4&s' a55ected' 7ULL ML Road SeatESaddle : Road1<?/ HL Mo nta!n SeatESaddle : HL Road SeatESaddle : Mo nta!n13// LL Mo nta!n SeatESaddle : Road1??/ LL Road SeatESaddle .

<art t1o0 join an$ aggregations toget+er


Exercise 1 Step one) the join* (s a first step to the final solution, write a &uer' that retrie#es the emplo'ees wor"in as salesmen. !he table Sales.SalesPerson contains salesmenK we will later need the column 6usiness7ntit'$1 for further 9oinin to the 3rder/eader table. !herefore we use this table as a startin point. 3ther tables in this first part areK Person.Person and /uman5esources.7mplo'ee. 6usiness7ntit'$1 in Sales.SalesPerson will match the 6usiness7ntit'$1 in /uman5esources.7mplo'ee for a 9oin between the two tables. (lso, incorporate the table Person.Person for names and the result set should loo" li"e the followin . Use the L7F! function to format the Fullname column. Sales9erson%# 1111111111111 .00 .0< .@0 .@> .@@ .>/ .0/ .0@ .0: F llname 111111111111111111111111111111 Syed ,bbas #a-!d Cam2bell Garrett Iar)as Ts-! Re!ter D!ll!an Carson Lynn Tso5l!as 9amela ,nsman1Wol5e Tete Mensa1,nnan S( %to

&:@ ro4&s' a55ected' Step t(o) addin+ the a++re+ates* Goin in the table Sales.Sales3rder/eader and use the SUB and C3UN! functions for a re atin the #alues in L!otal SumM and LNo. of 3rdersM. .our result set should loo" li"e the followin .

$ s!nessEnt!ty%# F llname Total S m 7o. o5 Orders 1111111111111111 111111111111111111111111111111 111111111111111111111 1111111111111 .@> Ts-! Re!ter 0@:30=?8>=>? 3.>

P a g e | 27
.00 .@= .@3 .@0 Rac(el IaldeL L!nda M!tc(ell Ste2(en D!an) Garrett Iar)as ..3:./38/3.3 :.=3@@.>80.0 :<=>=.38=3>3 3<0/@:38.??3 :</ 3:0 30 .<3

.0@ .0: .0/ .@@ .03 ,my ,lberts S( %to 9amela ,nsman1Wol5e D!ll!an Carson Tete Mensa1,nnan 0>:>0<8=3<? @>.>:=38>==? 3/.>><08<?=: :.:?@>.@8:>:. .0:3>?080>3. <> .3. >? 3@< :3/

&:@ ro4&s' a55ected'

Exercise 2 )e will now write a &uer' that report sales b' 'ear and re ion. Use the tables Sales.Sales3rder/eader and Sales.Sales!erritor' for a list of #alues a re ated and then rouped b' 'ear and re ion. !he column Sub!otal in Sales.Sales3rder/eader shall be a re ated. !he 1(!7P(5! function will be used for presentation and roupin . Use the L7F! function to format the 5e ion column. .our result set should loo" li"e the followin . Cear 11111111111 .//: .//. .//< .//3 .//: .//. .//< .//3 .//: .//. .//< .//3 Re)!on 1111111111111111111111111 Un!ted B!n)dom Un!ted B!n)dom Un!ted B!n)dom Un!ted B!n)dom So t(4est So t(4est Canada Canada , stral!a , stral!a , stral!a , stral!a S mTotal 11111111111111111 .>:?>/8?:>3 :=/>@@08/<>. <>?:/..8/0=0 .@?/@.>8.@:= .>=:=//83=/. >//..>@83?.: @3/0:=38=//. <??<?0=8.0@> :</>/3@8:>@0 .:?3.03800<? 3::?/3<8=:@: <3=//>>8=>?=

&3/ ro4&s' a55ected' Exercise 3 )e want to see which emplo'ee ha#e been wor"in in more than one department. Use the tables Person. Person, /uman5esources.7mplo'ee and /uman5esources.7mplo'ee1epartment/istor'. !he C3UN! function in companion with 453UP 6. and /(;$N4 is used to find out the answer to our &uestion. .our result set should loo" li"e the followin . $ s!nessEnt!ty%# 1111111111111111 3 := ..3 Last7ame 1111111111 Walters $radley Ion) #2t. Co nt 11111111111 . . .

P a g e | 28
.<3 .?/ 7orman Word . <

&? ro4&s' a55ected'

,ombinin+ #esult Sets


Exercise 4 )e want to produce a tin' sales report. )hat we aim is to see the minimum, ma%imum and a#era e #alues from the column Sub!otal in the table Sales.Sales3rder/eader. Use UN$3N to combine the three result sets 'ou recei#e b' usin B$N, B(N and (;4 a re ate functions in separate &ueries.

)rite a &uer' that result in the followin . T!ny Sales Re2ort 11111111111111111111111111111111111111111 ,-era)e Order Ial eA 3/3=.>? Ma*!m n Order Ial eA ..3<?=.30 M!n!m m Order Ial eA :.<@ &< ro4&s' a55ected' Exercise 5 $n the pre#ious section 'ou wrote an outer 9oin to force all customers that not 'et ha#e placed an order =Customer$1 e%ists in the Sales.Customers table but not in the 3rders table@. !he followin &uer' is probabl' what 'ou wrote.

SELECT C.C stomer%#8 SOH.SalesOrder%# FROM Sales.C stomer ,S C LEFT OUTER DO%7 Sales.SalesOrderHeader ,S SOH O7 C.C stomer%# " SOH.C stomer%# WHERE SOH.SalesOrder%# %S 7ULL OR#ER $C C stomer%#
(nother wa' to achie#e the same result is b' usin the 7NC7P! operator. )rite a &uer' that combines two result sets and produce an e&ui#alent answer, without the column showin a lot of NULL, as the 3U!75 G3$N. $t should loo" li"e the followin . C stomer%# 11111111111 : . < 3 ? = ... =>0 =>> @// @/:

P a g e | 26
&@/: ro4&s' a55ected'

#estrictin+ the number o- ro(s returned -rom a .uer/


Exercise 6 )rite a &uer' that shows the ten most e%pensi#e products. !he table to use is Production.Product. Use the !3P clause and our result set should loo" somethin li"e the result below.

ListPrice name HHHHHHHHHHHHHHHHHHHHH HHHHHHHHHHHHHHHHHHHHHHHH EOF,,*F 5oadH1O+ 5ed, -* EOF,,*F 5oadH1O+ 5ed, 44 EOF,,*F 5oadH1O+ 5ed, 4, EOF,,*F 5oadH1O+ 5ed, O* EOF,,*F 5oadH1O+ 5ed, OEEPP,PP BountainH1++ Sil#er, E, EEPP,PP BountainH1++ Sil#er, 4* EEPP,PP BountainH1++ Sil#er, 44 EEPP,PP BountainH1++ Sil#er, 4, EEF4,PP BountainH1++ 6lac", E, =1+ row=s@ affected@ Exercise 7 )rite a &uer' that shows price and name of the 1 Q products that has the hi hest number of da's to manufacture. !he table to use is Production.Product. .our result set should loo" somethin li"e the result below. ListPrice name HHHHHHHHHHHHHHHHHHHHH HHHHHHHHHHHHHHHHHHHHHHHHH EOF,,*F 5oadH1O+ 5ed, -* EOF,,*F 5oadH1O+ 5ed, 44 EOF,,*F 5oadH1O+ 5ed, 4, EOF,,*F 5oadH1O+ 5ed, O* EOF,,*F 5oadH1O+ 5ed, O14OF,PP 5oadH4O+ 5ed, O, =- row=s@ affected@

P a g e | %-

Lab Module%: &or'ing (it) Sub*ueries


Objectives First 'ou will write a couple of simple sub&ueries. .ou will then mo#e on and increase comple%it' in the use of sub&ueries. Correlated sub&ueries are the ne%t topic where the 7N$S!S "e'word has its place. !he labs for this module are closed with an e%ercise on Common !able 7%pressions. Estimated time to complete the lab Exercise 1 )rite a &uer' that filters data and return the column >Name? from table Production.Product. !he filterin of rows is achie#ed b' a )/757 clause that compares a sin le #alue from a sub&uer'. !he inner sub&uer' shall return a specific ProductSubcate or'$1 that the outer &uer' uses as a filter of products to include in the report. !he inner &uer' will use its own )/757 clause to deli#er its #alue, the ProductSubcate or'$1, b' retrie#in it where the column >Name? in table Production.ProductSubcate or' ha#e the #alue of RSaddlesI. !he result set should loo" li"e the followin . 7ame 11111111111111111111111111111 LL Mo nta!n SeatESaddle ML Mo nta!n SeatESaddle HL Mo nta!n SeatESaddle LL Road SeatESaddle ML Road SeatESaddle HL Road SeatESaddle LL To r!n) SeatESaddle ML To r!n) SeatESaddle HL To r!n) SeatESaddle &> ro4&s' a55ected' Exercise 2 $n this e%ercise 'ou can chan e the pre#ious &uer' to deli#er the followin result set. !he )/757 clause in the sub&uer' will now use the wildcard strin R6oQI for a comparison. !he result set should loo" li"e the followin . 7ame 1111111111111111111111111111 Water $ottle 1 </ oL. Mo nta!n $ottle Ca)e Road $ottle Ca)e LL $ottom $racket ML $ottom $racket HL $ottom $racket P+ minutes.

P a g e | %1

&= ro4&s' a55ected' Exercise 3 )rite a &uer' that return all products that has the same price as the cheapest =lowest ListPrice@ !ourin 6i"e =ProductSubcate or'$1 S E@. Use the B$N=@ a re ate function in the sub&uer' to return the lowest ListPrice to the outer &uer'. !he result set should loo" li"e the followin . 7ame 11111111111111111111111111 To r!n)1</// $l e8 ?3 To r!n)1</// $l e8 ?0 To r!n)1</// $l e8 =. MMM To r!n)1</// Cello48 =. To r!n)1</// $l e8 33 To r!n)1</// $l e8 ?/ &:/ ro4&s' a55ected' Exercise 4 0art 11 ( list of countries =table8 Person.Countr'5e ion, column8 Name@ that hosts less then ten instances of StatePro#ince in table Person.StatePro#ince is what 'our boss wants. )rite a &uer' that satisfies 'our boss. Tip8 a sub&uer' usin /(;$N4 clause and a re ate function C3UN!=@ can do the 9ob. !he result set should loo" li"e the followin . 7ame 111111111111111111111111111 ,mer!can Samoa , stral!a Germany M!crones!a Un!ted B!n)dom Mars(all %slands 7ort(ern Mar!ana %slands 9ala I!r)!n %slands8 U.S. &> ro4&s' a55ected' 0art 21 5ewrite the &uer' as a G3$N, the same result set should be retrie#ed. /ere /(;$N4 and C3UN!=@ are as useful as in the pre#ious &uer'. Exercise 5 $n this &uer' we will put a sub&uer' in an e%pression in the S7L7C! list. )e would li"e to see a report on how the corporate salesmen are doin in their histor' of business =althou h some parameters in this e%ercise mi ht be missin for a correct result@.

P a g e | %2

( re ate the a#era e from column Sub!otal in table Sales.Sales3rder/eader as a sub&uer' =pa' attention to the NULL #alues in column SalesPerson$1@, then substract the rouped a#era es b' SalesPerson$1 in the outer &uer'. (lso, in the outer &uer' NULL #alues can cause confusin results. 5emember that NULL in the SalesPerson$1 column means $nternet sales, and those sales are of no interest for this &uer'. !he result set should loo" li"e the followin . Sales9erson%# 1111111111111 .03 .0: .@0 .0= .0> .0< Sales#!55 111111111111111111111 .0@:8@>3 1@/.:8>@? @/..8:=03 MMM 1?@:@83:@< 0?/<8:<03 :=?>8/?30

&:@ ro4&s' a55ected' Exercise 6 $n this e%ercise we will build the final &uer' in three steps. !he final result will show which bic'cles that costs 4++ to ,++ less than the a#era e bi"e. !he final &uer' will ma"e use of a deri#ed table to i#e us the answer. Step 11 Find out the a#era e ListPrice #alue in table Production.Product. 5estrict the rows 'ou wor" on to #alues 1, * and E in the column ProductSubcate or'$1. !he intermediate result set should be8

111111111111111111111 :?0=8@<@ &: ro4&s' a55ected'


Step 21 $ncorporate the entire pre#ious &uer' as a part of an e%pression in the new outer S7L7C! list 'ou will write. !a"e column ListPrice from table Production.Product and subtract the pre#ious &uer' as the e%pression, i#e the new column the column alias R1iffI. Ne ati#e #alues indicate a cheaper bi"e. !he intermediate result set should loo" somethin li"e the followin .
7ame 11111111111111111111111111111111111111111111111111 Road1:?/ Red8 =. Road1:?/ Red8 33 Road1:?/ Red8 30 Road1:?/ Red8 ?. Road1:?/ Red8 ?= Road13?/ Red8 ?0 Road13?/ Red8 =/ #!55 1111111111 :>>:8?<< :>>:8?<< :>>:8?<< :>>:8?<< :>>:8?<< 1:.08@3@ 1:.08@3@

P a g e | %%
Road13?/ Red8 33 Road13?/ Red8 30 1:.08@3@ 1:.08@3@ 1:/3=8@3@ 1:/3=8@3@ 1:/3=8@3@ 1:/3=8@3@ 1:/3=8@3@

Mo nta!n1?// $lack8 30 Mo nta!n1?// $lack8 ?. Road1@?/ $lack8 33 Road1@?/ $lack8 30 Road1@?/ $lack8 ?. &>@ ro4&s' a55ected'

Step 31 Now, the final &uer'. )rap parentheses around the pre#ious &uer' and turn it into a deri#ed table, i#e it the alias RNI. 2uer' the deri#ed table for all its columns and use 67!)77N to e%tract bic'cles that are in the inter#al 4++ to ,++, remember, ne ati#e #alues indicates cheaper. 4i#e the 67!)77N a reflection of how it wor"s, from the smallest to the lar est #alue. !he final result set should loo" li"e the followin .
7ame 11111111111111111111111111111111111111111111111111 Mo nta!n1<// $lack8 <0 Mo nta!n1<// $lack8 3/ Mo nta!n1<// $lack8 33 Mo nta!n1<// $lack8 30 Road1??/1W Cello48 <0 Road1??/1W Cello48 3/ Road1??/1W Cello48 3. Road1??/1W Cello48 33 Road1??/1W Cello48 30 &> ro4&s' a55ected' #!55 11111111111 1?/=8@3@ 1?/=8@3@ 1?/=8@3@ 1?/=8@3@ 13==8.3@ 13==8.3@ 13==8.3@ 13==8.3@ 13==8.3@

.ou mi ht also want to tr' writin this usin a Common !able 7%pression =C!7, usin )$!/@, instead of a deri#ed table. Exercise 7 $n this e%ercise we will e%amine the correlated sub&uer'. First 'ou use a 9oin and then a correlated sub&uer' to do the same wor". (s a final part of this e%ercise 'ou will compare e%ecution plans between the two &ueries. 5emember if not 35175 6. is present in the &uer'K an' arbitrar' order will be chosen b' S2L Ser#er. 0art 11 !he followin &uer' reports the salesmen that had more than O+++ in bonus. )rite and e%ecute the &uer', sa#e it for further use. SELECT 9.F!rst7ame N + + N 9.Last7ame FROM Sales.Sales9erson S9 DO%7 H manReso rces.Em2loyee E O7 E.$ s!nessEnt!ty%# " S9.$ s!nessEnt!ty%# DO%7 9erson.9erson ,S 9 O7 E.$ s!nessEnt!ty%# " 9.$ s!nessEnt!ty%# WHERE $on s O ?///

P a g e | %)
!he result8
111111111111111111111 Ts-! Re!ter Dae 9ak Lynn Tso5l!as &< ro4&s' a55ected'

0art 21 5ewrite the &uer' and use a correlated sub&uer' instead. )hen 'ou are finished, sa#e the &uer' for part E of this e%ercise. 0art 31 Compare the both &ueries e%ecution plans. )hat differs0 !o do this, clic" the button >$nclude (ctual 7%ecution Plan?, and then the >7%ecute? button. )hen the &ueries are e%ecuted, 'ou will see an e%tra tab in the result window. Exercise $n this e%ercise 'ou will write a correlated sub&uer' usin 7N$S!S as the first e%ercise. !hen, as the second, the 9oin e&ui#alent &uer'. )e would li"e a report on the salesmen that are not assi ned to a store to co#er. !he table Sales.SalesPerson can be used as the outer &uer' table, the table Sales.Store is a candidate for the inner &uer'. $n the table Sales.Store 'ou ha#e a column b' the name SalesPerson$1, a sales person encountered in the Sales.SalesPerson but not in the Sales.Store table =6usiness7ntit'$1 column@ is of interest in this report. !he result should be8 Sales9erson%# 1111111111111 .=0 .03 .0@ .00 &3 ro4&s' a55ected' 0art 11 )rite the correlated sub&uer' usin 7N$S!S. 0art 21 )rite the G3$N e&ui#alent &uer' Exercise ! .ou will write a report on countin products and their belon in s in product cate ories and product subcate ories. (s a tool 'ou will use the Common !able 7%pression. !he e%ercise is di#ided into two parts before the final &uer' is complete. 0art 11 First 'ou write a &uer' that ather the result set followin . !able Production.Product and the a re ate function C3UN!=@ will be used. 9rod ctS bcate)ory%#

P a g e | %.
11111111111111111111 7ULL : . < <3 <? <= <@ &<0 ro4&s' a55ected' 0art 21 !urn the pre#ious &uer' into a C!7 b' wrappin parentheses around it, name it >!empSet? and specif' the column names as the s'nta% of C!7 specifies. !he C!7 columns should ha#e the names >ProdSub$1? and >CountedProds?. Create the C!7 and issue a S7L7C! T F53B !empSet to chec" functionalit'. $t should loo" li"e 9rodS b%# Co nted9rods 11111111111 111111111111 7ULL .:: : <. . 3< MMM <? : <= . <@ :: &<0 ro4&s' a55ected' !hen comment out the S7L7C! T F53B !empSet test&uer'. Now 9oin the table Production.ProductSubcate or' and the C!7 b' appropriate columns and use SUB=@ a re ate function to summari:e the C!7 column >CountedProds?. Ba"e the Goin an outer 9oin to catch the C!7 column ProdSub$1s #alue of NULL. !he final result set should loo" somethin li"e the followin . 9rod ctCate)ory%# 11111111111111111 7ULL : . < 3 S bCat 11111111111 / < :3 0 :. S m9rods 11111111111 .:: >@ :<3 <? .> 11111111111 ./> <. 3< .. MMM : : . ::

Warn!n)A 7 ll -al e !s el!m!nated by an a))re)ate or ot(er SET o2erat!on. &? ro4&s' a55ected'

P a g e | %$

P a g e | %7

Lab Module+: Modi#ying Data in $ables


Objectives !he lab be ins with issuin a bac"up statement. !hen 'ou ha#e a point of restore if (d#enture)or"s*++, sample database in somewa' become lo icall' corrupt. !hen 'ou will wor" with transactions, continue with 1BL statements and measure performance for insert with and without inde%es defined on the table. $n se#eral of the e%ercises 'ou will wor" 'our wa' throu h in se#eral steps. !he indented arrow s'mbol indicates a step. 2mportant1 For a successful completion of this lab, read the lab instructions carefully and reflect on what 'ou are doin and what is happenin . $f 'ou encounter an' uncertainties, please notif' 'our instructor. Exercise " ( bac"up set is ne#er out of place when dealin with relational databases. 6efore modif'in data in these labs we issue a bac"up statement, somethin mi ht o wron . Create a folder named B'6ac"ups in the root of the cHdri#e, then write and e%ecute the followin statement. This arrow symbol indicates a new step.

7stimated time to complete this lab8 -+ minutes

$,CBU9 #,T,$,SE ,d-ent reWorks.//0 TO #%SB " +CAPMy$ack 2sP,W_$U.bak+


Exercise 1 !his e%ercise will e%amine how to wor" with transactions. )or" 'our wa' throu h this e%ercise step b' step. $nspect what #alues the LastName column in a table hosts. )rite and e%ecute the followin in (d#enture)or"s*++, sample database.

SELECT Last7ame FROM 9erson.9erson


!a"e a loo" at the result setK 'ou will soon alter data in the table. Now, start =but do not end@ an explicit transaction, remember8 674$N !5(NS(C!$3N and C3BB$!<53LL6(CC !5(NS(C!$3N. $nside the transaction, write and e%ecute the followin .

U9#,TE 9erson.9erson SET Last7ame " +9ettersson+


$ssue the S7L7C! command pre#iousl' e%ecuted, ta"e a loo" at the result set. 7%ecute the followin to e%amine the number of acti#e transactions.

SELECT QQTR,7COU7T ,S ,ct!-eTransact!ons

P a g e | %8

!he result informs 'ou that an uncommitted transaction e%ists. $f a C3BB$!! !5(NS(C!$3N now is issued, e#er'bod' in the table ha#e a LastName of Pettersson, a restore of the database mi ht be at hand. Now, rollbac" the transaction.

Chec" the UU!5(NC3UN! #ariable. 7%ecute the S7L7C! command a ain to see what LastName is reported. Now the state of the database is what is was before 'our transaction started. Exercise 2 $n this e%ercise we will insert data in a table without trouble. (lthou h this the scope of this course do not co#er 11L, 'ou will create a table for insertion of data. .our instructor can answer an' &uestions the C57(!7 !(6L7 statement mi ht raise. Create the table b' writin and e%ecutin the followin .

CRE,TE T,$LE FdboG.FTem2C stomersG & FContact%#G F!ntG 7ULL8 FF!rst7ameG Fn-arc(arG&?/' 7ULL8 FLast7ameG Fn-arc(arG&?/' 7ULL8 FC!tyG Fn-arc(arG&</' 7ULL8 FState9ro-!nceG Fn-arc(arG&?/' 7ULL ' GO
$nsert data usin the ;(LU7S function. !r' to insert two rows of data. 1o this insertion of two rows in one batch and with onl' one ;alues function. No destination columns should be defined, althou h this is not recommended, 'ou do this for the outcome of this e%ercise. (lso, no NULL and 17F(UL! is to be put in the ;(LU7S=@.

!r' to insert the followin rows8 #o( 11 Column Contact$1 should be 1 Column FirstName should be Calen Column LastName should be 1elane' #o( 21 $nsert the followin row8 Column Contact$1 should be * Column FirstName should be /errman Column LastName should be Carlsson Column Cit' should be ;islanda Column StatePro#ince should be Cronober

P a g e | %6

.ou encountered an error messa e, readin the followin . Ms) :/@/>8 Le-el :=8 State :8 L!ne . T(e n mber o5 col mns 5or eac( ro4 !n a table -al e constr ctor m st be t(e same. !r' to insert the followin rows, notice that the last column reference is missin in both rows. No 17F(UL! or NULL should be present. #o( 11 Column Contact$1 should be E Column FirstName should be !ora Column LastName should be 7ri"sson Column Cit' should be 4udsmedsh'ttan #o( 21 $nsert the followin row8 Column Contact$1 should be 4 Column FirstName should be Charlie Column LastName should be Carpenter Column Cit' should be !appstrVm .ou encountered an error messa e, readin the followin . Ms) .:<8 Le-el :=8 State :8 L!ne : Col mn name or n mber o5 s 22l!ed -al es does not matc( table de5!n!t!on. Complete the $NS75! statement with the column list, tellin S2L Ser#er which columns to insert data into. !his is what 'ou alwa's should do.

)rite a S7L7C! statement to retrie#e all columns and rows from the table and e%ecute to 'our result. Exercise 3 /opefull' 'ou will encounter some issues re ardin an insert in this e%ercise. !he table Production.Product has some constraints due to its definition. $nsert a new row, the products name shall be 5acin 4i:mo. )or" 'our wa' throu h an' error messa es until the row has entered the table. Tip1 .ou can for 'our con#enience use 47!1(!7=@ for the dateHcolumns. Exercise 4 .ou will now use an $NS75!S7L7C! statement for insert into an e%istin table. !he followin &uer' will i#e 'ou the columns for an insert, ma"e sure 'ou follow 6est Practices and use a column list. Use the followin &uer' to construct an $NS75!S7L7C! statement for data input into LdboM.L!empCustomersM.

P a g e | )-

SELECT 9.$ s!nessEnt!ty%#8 9.F!rst7ame 8 9.Last7ame8 9,.C!ty 8 S9.7ame FROM 9erson.9erson ,S 9 DO%7 9erson.$ s!nessEnt!ty ,S $E O7 9.$ s!nessEnt!ty%#"$E.$ s!nessEnt!ty%# DO%7 9erson.$ s!nessEnt!ty,ddress ,S $E, O7 $E.$ s!nessEnt!ty%# " $E,.$ s!nessEnt!ty%# DO%7 9erson.,ddress 9, O7 $E,.,ddress%#"9,.,ddress%# DO%7 9erson.State9ro-!nce ,S S9 O7 9,.State9ro-!nce%# " S9.State9ro-!nce%# Exercise 5 $n this e%ercise we will e%amine insert performance differences. $f or if not the table tar eted for an insert ha#e inde%es defined, it will ma"e a difference. )e will also be aware of the importance of a column list when insertin data. !he followin !HS2L will be used in this e%ercise, write it and sa#e the file. 11 Em2ty t(e table 11 and 5l s( t(e b 55er and 2lan cac(es TRU7C,TE T,$LE dbo.Tem2C stomers GO #$CC #RO9CLE,7$UFFERS #$CC FREE9ROCC,CHE GO 11 %nsert data and meas re t(e t!me #ECL,RE QStart #,TET%ME.8 QSto2 #,TET%ME. SELECT QStart " SCS#,TET%ME&' %7SERT %7TO dbo.Tem2C stomers &Contact%#8 F!rst7ame8 Last7ame' SELECT $ s!nessEnt!ty%#8 F!rst7ame8 Last7ame FROM 9erson.9erson SELECT QSto2 " SCS#,TET%ME&' SELECT #,TE#%FF&ms8QStart8QSto2' as M!ll!Seconds First 'ou e%ecute the part to truncate the table and flush the . Second 'ou select and e%ecute the $NS75! statement from the declaration of #ariables to the 1(!71$FF=@ function. )ithout the column list the &uer' would not e%ecute, a couple of columns are missin in the $NS75! statement. 5ecord the output in milliseconds it too" to e%ecute. !his measurement is for the cold cache. Now, 9ust truncate the table, do not flush the buffer cache. 7%ecute the $NS75! statement a ain and record the time. !his measurement is for the warm cache. 5epeat the abo#e steps another time to see a fairl' accurate a#era e. !5UNC(!7 the table to prepare for the ne%t part of this e%ercise. !he ne%t !HS2L creates some inde%es to slow down the insert =but helps a lot with S7L7C!@. )rite it and sa#e it to 'our file.

P a g e | )1

CRE,TE U7%HUE CLUSTERE# %7#ER FUn!S e_Cl steredG O7 FdboG.FTem2C stomersG & FContact%#G ,SC ' GO CRE,TE 7O7CLUSTERE# %7#ER F7onCl stered_L7ameG O7 FdboG.FTem2C stomersG & FLast7ameG ,SC ' GO CRE,TE 7O7CLUSTERE# %7#ER F7onCl stered_F7ameG O7 FdboG.FTem2C stomersG & FF!rst7ameG ,SC '
Now is the time to measure the time it ta"es to insert data in inde%ed tables. !he wor"flow is the same this time. 7%ecute the part that truncates the table and flush the caches. Select and e%ecute the $NS75! statement from the declaration of #ariables to the 1(!71$FF=@ function. 5ecord the output in milliseconds it too" to e%ecute. !his measurement is for the cold cache. Now, 9ust truncate the table, do not flush the buffer cache. 7%ecute the $NS75! statement a ain and record the time. !his measurement is for the warm cache. 5epeat the abo#e steps another time to see a fairl' accurate a#era e.

Compare the time it too" this time and the pre#ious without inde%es. )hat difference can 'ou notice0 Exercise 6 $n this e%ercise we will deliberatel' encounter a series of error messa es to obser#e how S2L Ser#er ma"es use of different constraints for data inte rit'. !hese constraints are not alwa's implemented in the databases 'ou will wor" with bac" at 'our office, but it is preferred to ma"e use of the tools for data inte rit' S2L Ser#er e%pose when modelin a database schema. Objectives1 First 'ou will create a temporar' table populated from an e%istin table in (dwenture)or"s*++, and the tr' to push bac" the rows into the e%istin table. 1urin this process a series of errors will be raised. 3ote1 !his e%ercise is for lab e%perience, not a reflection of how common wor" with S2L Ser#er commonl' be carried out. 0art 11 )rite and e%ecute a S7L7C!...$N!3 statement that populates a table named W!emp!ab. !his local temporar' table shall ha#e si% columns selected from the Person.Person table, see below. ( filter must be applied, onl' LastName of X(chon X and X(ce#edoX shall be imported in the temporar' table.

P a g e | )2

!he columns from Person.Person table shall be. H 6usiness7ntit'$1 H Person!'pe H FirstName H LastName H !itle H 7mailPromotion ;erif' that two rows are imported b' &uer'in the table.

SELECT * FROM TTem2Tab


Use the rows in the temporar' table as a source for insert into the Person.Person table. )rite and e%ecute an $NS75!S7L7C! statement with a column list specif'in the si% columns selected. )hen tr'in to do the $NS75!, 'ou encounter the followin 8
Ms) .=.@8 Le-el :38 State :8 L!ne : I!olat!on o5 9R%M,RC BEC constra!nt +9B_9erson_$ s!nessEnt!ty%#+. Cannot !nsert d 2l!cate key !n ob;ect +9erson.9erson+. T(e statement (as been term!nated.

!he uni&ueness of records in Person.Person cannot be #iolated. !he Primar' Ce' tells us that S2L Ser#er will not o alon with this. )e must assi n new #alues to the "e' if an $NS75! shall ta"e place. !o do this we fetch the ma%imum #alue if 6usiness7ntit'$1 from Person.Person and UP1(!7 the temporar' table with two new #alues in two different manors. 0art 21 UP1(!7 the 6usiness7ntit'$1 column #ia a Sub2uer'. Use the B(N=@ function to fetch the ma%imum 6usiness7ntit'$1 from Person.Person and add Y1 to the 6usiness7ntit'$1 in 'our temporar' table the at the same time. Secondl', UP1(!7 the last row in 'our temporar' table usin the #alue in 6usiness7ntit'$1 column, in other words, add 1 to the e%istin #alue usin an UP1(!7 statement. !r' to do the $NS75! a ain, now when 'ou ha#e uni&ue #alues in the temporar' table. Luc"il' S2L Ser#er tells 'ou that this not is possible due a Forei n Ce' pointin to the Person.6usiness7ntit' table holdin the master records of indi#iduals in the database.
Ms) ?3@8 Le-el :=8 State /8 L!ne : T(e %7SERT statement con5l!cted 4!t( t(e FORE%G7 BEC constra!nt UFB_9erson_$ s!nessEnt!ty_$ s!nessEnt!ty%#U. T(e con5l!ct occ rred !n database U,d-ent reWorks.//0U8 table U9erson.$ s!nessEnt!tyU8 col mn +$ s!nessEnt!ty%#+. T(e statement (as been term!nated.

6efore we can enter new rows in the Person.Person table the Person.6usiness7ntit' table must ha#e entries of the new #alues of 6usiness7ntit'$1 in 'our temporar' table. )rite and e%ecute the followin twice to enter new #alues in the Person.6usiness7ntit' table8

P a g e | )%
Step 31 11 E*ec te t(e 5ollo4!n) t4!ce %nsert 9erson.$ s!nessEnt!ty Ial es&#EF,ULT8 #EF,ULT' $f 'ou ta"e a loo" at the table definition of the Person.6usiness7ntit' 'ou will notice the 6usiness7ntit'$1 column has an $17N!$!. specification, therefore 'ou onl' enter two 17F(UL!S. Now the 6usiness7ntit'$1 column has the two new #alues needed for insert into Person.Person. 6ecause application de#elopment and loo"ups of data are be'ond the scope of this course, we 9ust manipulate the tables directl'. !r' to do the $NS75! $N!3 Person.Person from 'our temporar' table a ain. !his time it should wor" fine. 1rop the temporar' table b' issuin the followin command.

#RO9 T,$LE TTem2Tab


;erif' that the rows entered into the Person.Person table e%ist. Use a S7L7C! command with a )/757 clause chec"in the column Bodified1ate. Use the e%ample format +.//>1/.1/0+ to select the two rows that ha#e the date part reater than the e%ample date. Exercise 7 )rite and e%ecute an UP1(!7 statement a ainst table Person.Person. Chan e column FirstName to R4urraI and the column LastName to R!9on I where column Contact$1 e&uals the first Contact$1 obtained in the pre#ious e%ercise, in one SET operation. Exercise )rite and e%ecute a sin le &uer' that raises the ListPrice b' 1+ Q on those products =table Producttion.Product@ that are co#ered b' the product subcate or' name R4lo#esI. .ou can use either a G3$N or a sub&uer' to ma"e it in one statement. Exercise ! )rite a &uer' that ma"es use the 3U!PU! clause. Update the table Production.ProductCate or', the column Name shall ha#e a #alue of RPartsI where the column ProductCate or'$1 is E. )e want to see the #alue before and after the update, as the followin result set shows. $e5ore ,5ter 111111111 1111111111 Clot(!n) 9arts &: ro4&s' a55ected' Exercise 1" )rite and e%ecute a 17L7!7 statement to remo#e rows from the dbo.!empCustomers table. !he rows to be remo#ed ha#e the #alue of XSmithX in the LastName column.

P a g e | ))

Lab Solutions
Lab2 Solutions
Solutions !he followin 1O code snippets are su estions for the second module. Exercise 1

SELECT 9rod ct%#8 F7ameG8 Color8 L!st9r!ce FROM 9rod ct!on.9rod ct SELECT 9rod ct%#8 F7ameG8 Color8 L!st9r!ce FROM 9rod ct!on.9rod ct WHERE L!st9r!ce VO / SELECT 9rod ct%#8 F7ameG8 Color8 L!st9r!ce FROM 9rod ct!on.9rod ct WHERE Color %S 7ULL

Exercise 2

Exercise 3

Exercise 4

SELECT 9rod ct%#8 F7ameG8 Color8 L!st9r!ce FROM 9rod ct!on.9rod ct WHERE Color %S 7OT 7ULL SELECT 9rod ct%#8 F7ameG8 Color8 L!st9r!ce FROM 9rod ct!on.9rod ct WHERE Color %S 7OT 7ULL ,7# L!st9r!ce O /

Exercise 5

Exercise 6

SELECT F7ameG N + A + N Color ,S F7ame ,nd ColorG FROM 9rod ct!on.9rod ct WHERE Color %S 7OT 7ULL SELECT +7,MEA + N F7ameG N + 11 N Color ,S F7ame ,nd ColorG FROM 9rod ct!on.9rod ct WHERE Color %S 7OT 7ULL COLORA +

Exercise 7

Exercise

SELECT 9rod ct%#8 F7ameG FROM 9rod ct!on.9rod ct WHERE 9rod ct%# $ETWEE7 3// ,7# ?//
Exercise !

SELECT 9rod ct%#8 F7ameG8 Color FROM 9rod ct!on.9rod ct WHERE Color %7 &+$lack+8+$l e+'

P a g e | ).
Exercise 1"

SELECT F7ameG8 L!st9r!ce FROM 9rod ct!on.9rod ct WHERE F7ameG L%BE +sW+ OR#ER $C F7ameG
Exercise 11

SELECT F7ameG8 L!st9r!ce FROM 9rod ct!on.9rod ct WHERE F7ameG L%BE +FsaGW+ OR#ER $C F7ameG
Exercise 12

SELECT F7ameG8 L!st9r!ce FROM 9rod ct!on.9rod ct WHERE F7ameG L%BE +s2oFXkGW+ OR#ER $C F7ameG
Exercise 13

SELECT #%ST%7CT Color FROM 9rod ct!on.9rod ct


Exercise 14

SELECT #%ST%7CT 9rod ctS bcate)ory%#8 Color FROM 9rod ct!on.9rod ct WHERE 9rod ctS bcate)ory%# %S 7OT 7ULL ,7# Color %S 7OT 7ULL OR#ER $C 9rod ctS bcate)ory%# ,SC8 Color #ESC
Exercise 15

SELECT 9rod ctS bCate)ory%# 8 LEFT&F7ameG8<?' ,S F7ameG 8 Color8 L!st9r!ce FROM 9rod ct!on.9rod ct WHERE Color %7 &+Red+8+$lack+' ,7# 9rod ctS bCate)ory%# " : OR L!st9r!ce $ETWEE7 :/// ,7# ./// OR#ER $C 9rod ct%# SELECT F7ameG8 %S7ULL&Color8 +Unkno4n+' ,S Color8 L!st9r!ce FROM 9rod ct!on.9rod ct

Exercise 16

Lab 3 Solutions

P a g e | )$
Exercise 1 Exercise 2

SELECT COU7T&*' FROM 9rod ct!on.9rod ct SELECT COU7T&9rod ctS bcate)ory%#' ,S HasS bCate)ory%# FROM 9rod ct!on.9rod ct

Exercise 3

SELECT 9rod ctS bcate)ory%# 8 COU7T&F7ameG' ,S Co nted9rod cts FROM 9rod ct!on.9rod ct GROU9 $C 9rod ctS bcate)ory%#
>irst

Exercise 7

SELECT COU7T&*' 1 COU7T&9rod ctS bcate)ory%#' FROM 9rod ct!on.9rod ct


Secon$

SELECT COU7T&*' ,S 7oS bCat FROM 9rod ct!on.9rod ct WHERE 9rod ctS bcate)ory%# %S 7ULL
Exercise 8

SELECT 9rod ct%#8 SUM&S ant!ty' ,S T(eS m FROM 9rod ct!on.9rod ct%n-entory GROU9 $C 9rod ct%#
Exercise 9

SELECT 9rod ct%#8 SUM&S ant!ty' ,S T(eS m FROM 9rod ct!on.9rod ct%n-entory WHERE Locat!on%# " 3/ GROU9 $C 9rod ct%# H,I%7G SUM&S ant!ty' V ://

Exercise :

SELECT S(el58 9rod ct%# 8 SUM&S ant!ty' ,S T(eS m FROM 9rod ct!on.9rod ct%n-entory WHERE Locat!on%# " 3/ GROU9 $C 9rod ct%#8 s(el5 H,I%7G SUM&S ant!ty' V ://
Exercise 8

SELECT ,IG&H ant!ty' ,S T(e,-) FROM 9rod ct!on.9rod ct%n-entory W(ere Locat!on%# " :/

P a g e | )7

Exercise ;

SELECT 9rod ct%#8 S(el5 8 ,IG&H ant!ty' ,S T(e,-) FROM 9rod ct!on.9rod ct%n-entory WHERE Locat!on%# " :/ ,7# S(el5 VO +7E,+ GROU9 $C ROLLU9 &S(el58 9rod ct%#' OR#ER $C S(el5
Exercise 10 SELECT Color 8Class 8COU7T&*' ,S T(eCo nt 8,IG&L!st9r!ce' ,S ,-)9r!ce FROM 9rod ct!on.9rod ct WHERE Class %S 7OT 7ULL ,7# Color %S 7OT 7ULL GROU9 $C GROU9%7G SETS &&Color'8 &Class''

Exercise 11

SELECT 9rod ctS bcate)ory%# 8 COU7T&7ame' as Co nted 8 GROU9%7G&9rod ctS bcate)ory%#' ,S %sGrandTotal FROM 9rod ct!on.9rod ct GROU9 $C ROLLU9 &9rod ctS bcate)ory%#' OR#ER $C 9rod ctS bcate)ory%#
Exercise 12

SELECT ROW_7UM$ER&' OIER &OR#ER $C 7ame' ,S #!ctOrder 87ame FROM 9rod ct!on.9rod ctCate)ory OR#ER $C #!ctOrder SELECT 2.7ame8 !.Locat!on%#8 !.H ant!ty 8R,7B&' OIER &9,RT%T%O7 $C !.Locat!on%# OR#ER $C !.H ant!ty #ESC' ,S FR,7BG FROM 9rod ct!on.9rod ct%n-entory ! %77ER DO%7 9rod ct!on.9rod ct ,S 2 O7 !.9rod ct%# " 2.9rod ct%# WHERE Locat!on%# %7&.8<83' OR#ER $C Locat!on%#8 FR,7BG

Exercise 13

P a g e | )8

Lab ! Solutions

Solutions Part one


Exercise 1 SELECT CR.F7ameG ,S Co ntry 8 S9.F7ameG ,S 9ro-!nce FROM 9erson.Co ntryRe)!on ,S CR DO%7 9erson.State9ro-!nce ,S S9 O7 CR.Co ntryRe)!onCode " S9.Co ntryRe)!onCode Exercise 2 SELECT CR.F7ameG ,S Co ntry 8 S9.F7ameG ,S 9ro-!nce FROM 9erson.Co ntryRe)!on ,S CR DO%7 9erson.State9ro-!nce ,S S9 O7 CR.Co ntryRe)!onCode " S9.Co ntryRe)!onCode WHERE CR.F7ameG %7 &+Germany+8+Canada+' OR#ER $C Co ntry ,SC8 9ro-!nce ,SC Exercise 3 SELECT so(.SalesOrder%# 8so(.Order#ate 8so(.Sales9erson%# 82.$ s!nessEnt!ty%# 82.$on s 82.SalesCT# FROM Sales.SalesOrderHeader ,S so( %77ER DO%7 Sales.Sales9erson ,S 2 O7 so(.Sales9erson%# " 2.$ s!nessEnt!ty%# SELECT so(.SalesOrder%# 8so(.Order#ate 8e.DobT!tle 82.$on s 82.SalesCT# FROM Sales.SalesOrderHeader ,S so( %77ER DO%7 Sales.Sales9erson ,S 2 O7 so(.Sales9erson%# " 2.$ s!nessEnt!ty%# %77ER DO%7 H manReso rces.Em2loyee ,S e O7 2.$ s!nessEnt!ty%# " e.$ s!nessEnt!ty%#

Exercise 4

P a g e | )6
Exercise 5

SELECT so(.SalesOrder%# 8so(.Order#ate 82rs.F!rst7ame 82rs.Last7ame 82.$on s FROM Sales.SalesOrderHeader ,S so( %77ER DO%7 Sales.Sales9erson ,S 2 O7 so(.Sales9erson%# " 2.$ s!nessEnt!ty%# %77ER DO%7 H manReso rces.Em2loyee ,S e O7 2.$ s!nessEnt!ty%# " e.$ s!nessEnt!ty%# %77ER DO%7 9erson.9erson ,S 2rs O7 e.$ s!nessEnt!ty%# " 2rs.$ s!nessEnt!ty%#

Exercise 6

SELECT so(.SalesOrder%# 8so(.Order#ate 82rs.F!rst7ame 82rs.Last7ame 82.$on s FROM Sales.SalesOrderHeader ,S so( %77ER DO%7 Sales.Sales9erson ,S 2 O7 so(.Sales9erson%# " 2.$ s!nessEnt!ty%# %77ER DO%7 9erson.9erson ,S 2rs O7 2.$ s!nessEnt!ty%# " 2rs.$ s!nessEnt!ty%#

Exercise 7

SELECT so(.SalesOrder%# 8so(.Order#ate 82rs.F!rst7ame 82rs.Last7ame FROM Sales.SalesOrderHeader ,S so( %77ER DO%7 9erson.9erson ,S 2rs O7 so(.Sales9erson%# " 2rs.$ s!nessEnt!ty%#

Exercise

SELECT so(.SalesOrder%# 8so(.Order#ate 82rs.F!rst7ame N + + N 2rs.Last7ame ,S Sales9erson 8sod.9rod ct%# 8sod.OrderHty FROM Sales.SalesOrderHeader ,S so( %77ER DO%7 9erson.9erson ,S 2rs O7 so(.Sales9erson%# " 2rs.$ s!nessEnt!ty%# %77ER DO%7 Sales.SalesOrder#eta!l ,S sod O7 so(.SalesOrder%# " sod.SalesOrder%# OR#ER $C Order#ate8 SalesOrder%#

P a g e | .Exercise !

SELECT so(.SalesOrder%# 8so(.Order#ate 82rs.F!rst7ame N + + N 2rs.Last7ame ,S Sales9erson 82.7ame ,S 9rod ct7ame 8sod.OrderHty FROM Sales.SalesOrderHeader ,S so( %77ER DO%7 9erson.9erson ,S 2rs O7 so(.Sales9erson%# " 2rs.$ s!nessEnt!ty%# %77ER DO%7 Sales.SalesOrder#eta!l ,S sod O7 so(.SalesOrder%# " sod.SalesOrder%# %77ER DO%7 9rod ct!on.9rod ct ,S 2 O7 2.9rod ct%# " sod.9rod ct%# OR#ER $C Order#ate8 SalesOrder%#

Exercise 1"

SELECT so(.SalesOrder%# 8so(.Order#ate 82rs.F!rst7ame N + + N 2rs.Last7ame ,S Sales9erson 82.7ame ,S 9rod ct7ame 8sod.OrderHty FROM Sales.SalesOrderHeader ,S so( %77ER DO%7 9erson.9erson ,S 2rs O7 so(.Sales9erson%# " 2rs.$ s!nessEnt!ty%# %77ER DO%7 Sales.SalesOrder#eta!l ,S sod O7 so(.SalesOrder%# " sod.SalesOrder%# %77ER DO%7 9rod ct!on.9rod ct ,S 2 O7 2.9rod ct%# " sod.9rod ct%# WHERE so(.S bTotal O :///// ,7# CE,R&so(.Order#ate' " .//3 OR#ER $C Order#ate8 SalesOrder%# Tip1 For abo#e &uer', S2L Ser#er cannot see" throu h an inde%, it can onl' do an inde% scan, on the 3rder1ate column if an inde% e%ists. 6elow &uer' allows thatK Notice the )/757 clause8

SELECT

P a g e | .1
so(.SalesOrder%# 8so(.Order#ate 82rs.F!rst7ame N + + N 2rs.Last7ame ,S Sales9erson 82.7ame ,S 9rod ct7ame 8sod.OrderHty FROM Sales.SalesOrderHeader ,S so( %77ER DO%7 9erson.9erson ,S 2rs O7 so(.Sales9erson%# " 2rs.$ s!nessEnt!ty%# %77ER DO%7 Sales.SalesOrder#eta!l ,S sod O7 so(.SalesOrder%# " sod.SalesOrder%# %77ER DO%7 9rod ct!on.9rod ct ,S 2 O7 2.9rod ct%# " sod.9rod ct%# WHERE so(.S bTotal O :///// ,7# so(.Order#ate O" +.//3/:/:+ ,7# so(.Order#ate V +.//?/:/:+ OR#ER $C Order#ate8 SalesOrder%# Exercise 11 SELECT c.7ame ,S Co ntry7ame 82.7ame ,S 9ro-!nce7ame FROM 9erson.Co ntryRe)!on ,S c LEFT OUTER DO%7 9erson.State9ro-!nce ,S 2 O7 c.Co ntryRe)!onCode " 2.Co ntryRe)!onCode OR#ER $C Co ntry7ame8 9ro-!nce7ame Exercise 12 SELECT C.C stomer%#8 SOH.SalesOrder%# FROM Sales.C stomer ,S C LEFT OUTER DO%7 Sales.SalesOrderHeader ,S SOH O7 C.C stomer%# " SOH.C stomer%# WHERE SOH.SalesOrder%# %S 7ULL OR#ER $C C stomer%# (bo#e results can also be achie#ed usin N3! 7N$S!S =as will be discussed in an upcomin module@8 SELECT C.C stomer%#8 7ULL ,S SalesOrder%# FROM Sales.C stomer ,S c WHERE 7OT ER%STS &SELECT * FROM Sales.SalesOrderHeader ,S so( WHERE so(.C stomer%# " c.C stomer%#' Exercise 13 SELECT 9.F7ameG ,S 9rod ct7ame 8 9M.F7ameG ,S 9rod ctModel7ame FROM 9rod ct!on.9rod ct ,S 9 FULL OUTER DO%7 9rod ct!on.9rod ctModel ,S 9M O7 2.9rod ctModel%# " 9M.9rod ctModel%# WHERE 9.F7ameG %S 7ULL OR 9M.F7ameG %S 7ULL

Lab solutions Part two


Exercise 1 Step one1

P a g e | .2

SELECT s2.$ s!nessEnt!ty%# 8 LEFT&9.5!rstname N + + N 9.lastname8</' ,S F llname FROM Sales.Sales9erson ,S S9 %77ER DO%7 H manReso rces.Em2loyee ,S E O7 E.$ s!nessEnt!ty%# " S9.$ s!nessEnt!ty%# %77ER DO%7 9erson.9erson ,S 9 O7 9.$ s!nessEnt!ty%# " E.$ s!nessEnt!ty%# Step t(o1 SELECT s2.$ s!nessEnt!ty%# 8 LEFT&9.5!rstname N + + N 9.lastname8</' ,S F llname 8 SUM&SOH.S bTotal' ,S FTotal S mG 8 COU7T&SOH.Sales9erson%#' ,S F7o. o5 OrdersG FROM Sales.Sales9erson ,S S9 %77ER DO%7 H manReso rces.Em2loyee ,S E O7 E.$ s!nessEnt!ty%# " S9.$ s!nessEnt!ty%# %77ER DO%7 9erson.9erson ,S 9 O7 9.$ s!nessEnt!ty%# " E.$ s!nessEnt!ty%# %77ER DO%7 Sales.SalesOrderHeader ,S SOH O7 S9.$ s!nessEnt!ty%# " SOH.Sales9erson%# GROU9 $C S9.$ s!nessEnt!ty%# 8 LEFT&9.5!rstname N + + N 9.lastname8</' Exercise 2 SELECT #,TE9,RT&year8Order#ate' ,S FCearG 8 LEFT&F7ameG8 .?' ,S Re)!on 8 SUM&S bTotal' ,S S mTotal FROM Sales.SalesOrderHeader ,S SOH DO%7 Sales.SalesTerr!tory ,S ST O7 SOH.Terr!tory%# " ST.Terr!tory%# GROU9 $C #,TE9,RT&year8 SOH.Order#ate'8 F7ameG OR#ER $C F7ameG #ESC 8 #,TE9,RT&year8Order#ate' ,SC Exercise 3 SELECT E.$ s!nessEnt!ty%# 8 LEFT&9.Lastname8:/' ,S Last7ame 8 COU7T&#H.#e2artment%#' ,S F#2t. Co ntG FROM H manReso rces.Em2loyee#e2artmentH!story ,S #H DO%7 H manReso rces.Em2loyee ,S E O7 #H.$ s!nessEnt!ty%# " E.$ s!nessEnt!ty%# DO%7 9erson.9erson ,S 9 O7 E.$ s!nessEnt!ty%# " 9.$ s!nessEnt!ty%# GROU9 $C E.$ s!nessEnt!ty%#8 9.Lastname H,I%7G COU7T&#H.#e2artment%#' O : Exercise 4 SELECT +M!n!m m Order Ial eA + N C,ST&M%7&S bTotal' ,S I,RCH,R&./'' ,S FT!ny Sales Re2ortG FROM Sales.SalesOrderHeader U7%O7

P a g e | .%
SELECT +Ma*!m n Order Ial eA + N C,ST&M,R&S bTotal' ,S I,RCH,R&./'' FROM Sales.SalesOrderHeader U7%O7 SELECT +,-era)e Order Ial eA + N C,ST&,IG&S bTotal' ,S I,RCH,R&./'' FROM Sales.SalesOrderHeader Exercise 5 SELECT C stomer%# FROM Sales.C stomer ERCE9T SELECT C stomer%# FROM Sales.SalesOrderHeader SELECT TO9&:/' L!st9r!ce8 F7ameG FROM 9rod ct!on.9rod ct OR#ER $C L!st9r!ce #ESC Exercise 7 SELECT TO9&:' 9ERCE7T L!st9r!ce8 F7ameG FROM 9rod ct!on.9rod ct OR#ER $C #aysToMan 5act re desc

Exercise 6

Lab% Solutions
!he followin &ueries are su estions for lab answers. Exercise 1

SELECT F7ameG FROM 9rod ct!on.9rod ct WHERE 9rod ctS bcate)ory%# " &SELECT 9rod ctS bcate)ory%# FROM 9rod ct!on.9rod ctS bcate)ory WHERE F7ameG " +Saddles+' SELECT F7ameG FROM 9rod ct!on.9rod ct WHERE 9rod ctS bcate)ory%# %7 &SELECT 9rod ctS bcate)ory%# FROM 9rod ct!on.9rod ctS bcate)ory WHERE F7ameG L!ke +$oW+'

Exercise 2

Exercise 3

SELECT 7ame FROM 9rod ct!on.9rod ct WHERE L!st9r!ce "& SELECT M%7&L!st9r!ce' FROM 9rod ct!on.9rod ct WHERE 9rod ctS bcate)ory%# " <'

Exercise 4 0art 11

SELECT F7ameG FROM 9erson.Co ntryRe)!on

P a g e | .) WHERE Co ntryRe)!onCode %7 &SELECT Co ntryRe)!onCode FROM 9erson.State9ro-!nce GROU9 $C Co ntryRe)!onCode H,I%7G COU7T&*' V :/'
0art 21

SELECT CR.F7ameG FROM 9erson.Co ntryRe)!on ,S CR DO%7 9erson.State9ro-!nce ,S S9 O7 CR.Co ntryRe)!onCode " S9.Co ntryRe)!onCode GROU9 $C CR.F7ameG H,I%7G COU7T&*'V:/
Exercise 5

SELECT so(.Sales9erson%# 8&SELECT ,IG&sod.S bTotal' FROM Sales.SalesOrderHeader ,S sod WHERE sod.Sales9erson%# %S 7OT 7ULL' 1 ,IG&S bTotal' ,S Sales#!55 FROM Sales.SalesOrderHeader ,S so( WHERE so(.Sales9erson%# %S 7OT 7ULL GROU9 $C so(.Sales9erson%#

Exercise 6 Step 11

SELECT ,IG&L!st9r!ce' FROM 9rod ct!on.9rod ct WHERE 9rod ctS bcate)ory%# %7 &:8.8<'
Step 21

SELECT 2..7ame 82..L!st9r!ce 1 &SELECT ,IG&2:.L!st9r!ce' FROM 9rod ct!on.9rod ct ,S 2: WHERE 2:.9rod ctS bcate)ory%# %7 &:8.8<'' ,S #!55 FROM 9rod ct!on.9rod ct ,S 2. WHERE 2..9rod ctS bcate)ory%# %7 &:8.8<'
Step 31 (s a deri#ed table8 SELECT * FROM &

P a g e | ..
SELECT 2..7ame 82..L!st9r!ce 1 &SELECT ,IG&2:.L!st9r!ce' FROM 9rod ct!on.9rod ct ,S 2: WHERE 2:.9rod ctS bcate)ory%# %7 &:8.8<'' ,S #!55 FROM 9rod ct!on.9rod ct ,S 2. WHERE 2..9rod ctS bcate)ory%# %7 &:8.8<' ' ,S * WHERE *.#!55 $ETWEE7 10// and 13//

Usin a common table e%pression8 W%TH * ,S & SELECT 2..7ame 82..L!st9r!ce 1 &SELECT ,IG&2:.L!st9r!ce' FROM 9rod ct!on.9rod ct ,S 2: WHERE 2:.9rod ctS bcate)ory%# %7 &:8.8<'' ,S #!55 FROM 9rod ct!on.9rod ct ,S 2. WHERE 2..9rod ctS bcate)ory%# %7 &:8.8<' ' SELECT * FROM * WHERE *.#!55 $ETWEE7 10// and 13//
Exercise 7 0art 21

SELECT 9.F!rst7ame N + + N 9.Last7ame FROM 9erson.9erson ,S 9 DO%7 H manReso rces.Em2loyee ,S E O7 E.$ s!nessEnt!ty%# " 9.$ s!nessEnt!ty%# WHERE ?/// V &SELECT $on s FROM Sales.Sales9erson S9 WHERE E.$ s!nessEnt!ty%# " S9.$ s!nessEnt!ty%#'

0art 31 Nothin , S2L Ser#er optimi:er e#aluates to the same plan. Exercise 0art 11

P a g e | .$ SELECT s2.$ s!nessEnt!ty%# FROM Sales.Sales9erson ,S s2 WHERE 7OT ER%STS &SELECT s.Sales9erson%# FROM Sales.Store ,S s WHERE s.Sales9erson%# " s2.$ s!nessEnt!ty%#'
0art 21

SELECT s2.$ s!nessEnt!ty%# FROM Sales.Sales9erson ,S s2 LEFT DO%7 Sales.Store ,S s O7 s.Sales9erson%# " s2.$ s!nessEnt!ty%# WHERE s.name %S 7ULL
Exercise !

11 T(e CTE W%TH Tem2Set &9rodS b%#8 Co nted9rods' ,S &SELECT 9rod ctS bcate)ory%#8 COU7T&9rod ct%#' FROM 9rod ct!on.9rod ct GROU9 $C 9rod ctS bcate)ory%#' 11 T(e H ery SELECT 9rod ctCate)ory%# 8 COU7T&9rod ctS bcate)ory%#' ,S S bCat 8 SUM&Co nted9rods' as S m9rods FROM 9rod ct!on.9rod ctS bcate)ory 9S R%GHT DO%7 Tem2Set T O7 &T.9rodS b%# " 9S.9rod ctS bcate)ory%#' GROU9 $C 9rod ctCate)ory%#

Lab + Solutions
Exercise " No solution code Exercise 1 No solution code

Exercise 2 The one (ith errors1 %7SERT %7TO FdboG.FTem2C stomersG I,LUES &:8+Balen+8+#elaney+' 8&.8+Herrman+8+Barlsson+8+I!slanda+8+Bronober)+' The one (ith no errors1

P a g e | .7
%7SERT %7TO FdboG.FTem2C stomersG I,LUES &:8+Balen+8+#elaney+8#EF,ULT8 #EF,ULT' 8&.8+Herrman+8+Barlsson+8+I!slanda+8+Bronober)+' Exercise 3 %7SERT %7TO 9rod ct!on.9rod ct &7ame8 9rod ct7 mber 8 Sa5etyStockLe-el8 Reorder9o!nt 8 StandardCost8 L!st9r!ce 8 #aysToMan 5act re8 SellStart#ate' I,LUES&+Rac!n)G!Lmo+8 +,$1:.<3+8 :/8 .8 ?/8 ./8 :8 GET#,TE&'' Exercise 4 %7SERT %7TO dbo.Tem2C stomers &Contact%#8 F!rst7ame8 Last7ame8 C!ty8 State9ro-!nce' SELECT 9.$ s!nessEnt!ty%#8 9.F!rst7ame 8 9.Last7ame8 9,.C!ty 8 S9.7ame FROM 9erson.9erson ,S 9 DO%7 9erson.$ s!nessEnt!ty ,S $E O7 9.$ s!nessEnt!ty%#"$E.$ s!nessEnt!ty%# DO%7 9erson.$ s!nessEnt!ty,ddress ,S $E, O7 $E.$ s!nessEnt!ty%# " $E,.$ s!nessEnt!ty%# DO%7 9erson.,ddress 9, O7 $E,.,ddress%#"9,.,ddress%# DO%7 9erson.State9ro-!nce ,S S9 O7 9,.State9ro-!nce%# " S9.State9ro-!nce%# Exercise 5 !he bi difference is not warm or cold cache =not so much data, althou h@, it is the inde%es that ma"es the bi difference. Exercise 6 The import into the temporar/ table1 11 9art : SELECT $ s!nessEnt!ty%#8 9ersonTy2e 8 F!rst7ame8 Last7ame 8 T!tle8 Ema!l9romot!on %7TO TTem2Tab FROM 9erson.9erson WHERE Last7ame %7 &+,c(on)+8+,ce-edo+' 11 C(eck select * 5rom TTem2Tab

The import bac$ to the 0erson*0erson table1 11 9art .A T(e 5!rst error. %7SERT %7TO 9erson.9erson &$ s!nessEnt!ty%#8 9ersonTy2e 8 F!rst7ame 8 Last7ame8 T!tle8 Ema!l9romot!on' SELECT * FROM TTem2Tab

P a g e | .8
11 Start 5!*!n)8 5!rst t(!s U9#,TE TTem2Tab SET $ s!nessEnt!ty%# " & SELECT M,R&$ s!nessEnt!ty%#' N : FROM 9erson.9erson ' 11 Second U2#ate TTem2Tab Set $ s!nessEnt!ty%# " $ s!nessEnt!ty%# N : W(ere F!rst7ame " +G sta-o+ and Last7ame " +,c(on)+ The import bac$ to the 0erson*0erson table continued1 11 9art < 11 E*ec te t(e 5ollo4!n) t4!ce %nsert 9erson.$ s!nessEnt!ty Ial es&#EF,ULT8 #EF,ULT' 4eri-ication1 SELECT * FROM 9erson.9erson WHERE Mod!5!ed#ate O +.//>1/.1/0+ 11 For an e*am2le o5 a date Exercise 7 U9#,TE 9erson.9erson SET F!rst7ame " +G rra+ 8 Last7ame " +T;on)+ WHERE $ s!nessEnt!ty%# " ./@0: U9#,TE 9rod ct!on.9rod ct SET L!st9r!ce " L!st9r!ce * :.: WHERE 9rod ctS bCate)ory%# " &SELECT 9rod ctS bCate)ory%# FROM 9rod ct!on.9rod ctS bCate)ory WHERE F7ameG " +Glo-es+'

Exercise

Exercise ! U9#,TE 9rod ct!on.9rod ctCate)ory SET 7ame " +9arts+ OUT9UT #ELETE#.7ame ,S F$e5oreG 8 %7SERTE#.7ame ,S F,5terG WHERE 9rod ctCate)ory%# " < Exercise 1" #ELETE FROM dbo.Tem2C stomers WHERE Last7ame " +Sm!t(+

P a g e | .6

Potrebbero piacerti anche