Dissecting SQL Server Execution Plans

Size: px
Start display at page:

Download "Dissecting SQL Server Execution Plans"

Transcription

1 The SQL Server Performance Series Dissecting SQL Server Execution Plans By Grant Fritchey In association with ISBN: Shelving: Database/SQL Server wwwsimpletalkpublishingcom

2 " #!!

3 !"#$%#&'('%' ) ))* *+, -%# -") +++)+ /+++*0**) -* ) 1+ *)*+ +++** 2)* * 3 -*

4 $%% 4 - # *%% % *%4 %567!%& 898: ; %& :%& :<=%$ :67% 6-67% 67>% 8-667?,)) 67% % % >3B*67 8*67 6( -( 67' 8*767$ C:A)( 67(( *@?A((

5 (& 5>67)!:($ A)67($ :( 7( 7(# " 7'% DAC' '& >,AC'$ E4 9?/E04 74' "AE44 44?E4& -896>64 67*><C!F<>,6>!F & & 9?/-0& &( > >*67&' +C,67&& && C&$,& $ (57@?A67)!:$% 767$% -7):$% -6@?A$ --@?A% % '5C?7:( (, '

6 - *, ' -, -AF$ 67# B*# B* # 7B*#( 7#4 75-!A#4 7# 7% %% 4567*9%( :9%( 9-9G<>,6>><C%(?6>6G9-9G<"-C" <" %4 A<<G?6>6G9-9E< " %& -%# <>6<>,6>%%?-@,<%%% <? H6<>%%( ->-?66> H- <"?A6G<>6, %%& >6<? A6%%& ><!CA-"%% D66A-"%% 6@-",B 68%%?-@>6C> <"%%# C6A-"%%# E9% 9% 97%( "<6@-", %( ",6@/0%' - >><8 %4 %$ &5< % %

7 A<%# <%('?<%(' - %(' D6F6%($ >6-,I<"AF%(# )%(# %'' $5@?A67%'& <>@?A%'& C7%4' C3%4& %4$ 5-%& >A67%& 67%&'?7,)%&' )%&4 -!;%&& 6767%&& 9*=))67%&# C?)67%$ < 1 %$ :A%$( %$' -%$4 %$4 C?)67%$4 %$$ 7%$

8 - # &$' '$( *,!-)? +,!-+* )+1 9 B!+JE*:A) )&+9*K,!-K9 ))+ ) ) - ) :A C /-0 * :A+:A + - * + :A :A 8* C 9)))))"*6:AC /"6C0 <*++))+ ** +* +)

9 %

10 * )%$*+% %% * *)++,) ))1* L *"7+ **)M*** * ) * F ***3):A )+ * **))* )+ 3N *5)L - L*) O

11 %,%($',$% 6+? ):A+ )35 8 3*; 7; 8 L7; 8 3)3; - ))+ * 35 ; 67 * *L :A **))*3 7 :A *7 9* +1+) )3 9* ) 7 ) ) + + *+ + **:A:A ) + L L * ) ) 1 + ) ) ) + L ) )+ )*7 *?*)) 7 + = * 1+) )+ +**= :A3+7+ )+ 5 9*7+*7@?A) -)7+ )*)* 9*:A:A 1P 7+*+ P7 9*) Q7 9*7*++* * 9*@?A7 -+)=) - *+ :A + ) + 7 = 9*+ ) * ) 7 + * ) ) ) )

12 ) * = :A+ )) 7+ 9*+ ) * * *7+) %(

13 %'

14 $(*$( * %4 :A)+ * )<)) )*) ),+:A, - + * + * + * =,!-+* *)1 "+,!-5* *,!-)+*+ *+,!-+ *))*+ 9*+ +,!-,!- *5* * *** < ) ),!-L ) + ),!-) < 7 +***))*:A* 8 )* ):A+ L*<))* :A7 L* ) *)*:A7+*+ *)) )) :A * * :A 7 * *):A 7++* <)):A+ )+ :A7 ) 67,!- - + * * 3 ) < ** ) *)*7)),),!-6+>)*

15 %& ( - /',$% % &, -7++) ))*3 :A3 67 * 3* 7+ * 3* 7 +)+,!-L) )3 >*3))+ Q< )+7)7):A 7+ *)*+ *7+ *- 7 ) 7+ +7@?A)+ )* +*L)*5!"7) =LL)* * =+** #$% #"**)) &!!'#% #P*L +7@? A7) 6A63!% # ":A) * "" *! #0 8 3:A + ) *3) * * ) )3 8 )))*:A+*L) :A **5 % 3 : <=+ *7/ )0+ * * +7% # +L**L )! 8 :A3:A+) % %-:A: 3)3) +:A* + *K<K

16 %567! - :A + :A * + L*) * 3 ) +3/ ):A/,? A0+* = +)7+K*K):A R )+ ) ) ) ) ) :A,?A + #! = ) 1+ + ) * 3 = )+ + /' ()*+ ' (,)+ 0 ) 1 )/+ -0 * 3+ = 3 + L ) 1 = +*!$"1 3 = ) )* KK * * * C 3 ++*** * 73P+7 *+=)* 3 :A3 ) 7+* )1 = * )7+)3C Q<+*)*79+* =*/0 ++***7 3 ) ) + C Q< ) 7 =* C)*)+ =*)))* )) ) 3P)7+ *7* *3P +=*+'## )3 )3+=*) + :A 7* ) )!+ ) ) ) +) LK K ) ) = ) 7 ) 7+ = * + )+*) 7)+)/0$$ * %$

17 % = *+ ) = +)*= = + * 3 + *)+)) )1+1+))7++ ** ),+ )*+=L)) * ** ) )))7) L 1 P ) )++=*)) ) 7 3 9*+ ) + 3 ) ))+ ) < = 7 + *# P7 / +!0 - = + ) ) +*! < 7 + * +* 37+ 8*+7)7 177+)5-7)7 /7)P 7(0 *)+ 7* = )3)) 3:A+*4$5$&6 -+*)7+ ) = * $ % # + + * * + L )=L*) "7)37) *+) + # % # ** *37 ( 7)7:A* * -+) # / = < + ) +

18 %567! = 77 ))+=* 7+L ) 3 ) 7 ) 7 3 )3) 6+)=* 7 ) / 0 ) = + * ))) +3* 67 ) * ) KK)) ) /*)% )4KK ) ) 40 =* + * ) ) / 0+ 1 ))*+)5? 3 KK)= L ) )* ) 7 5 ) ) 3 7 3,7 3 C 3 )+7 # 1 ) 3 ) *+)*)#? 7,,A,?A*3+)) 4$6*7)3 ) 3 * 3 *3 *+ * 8 +)/4$ *7+)* )3**))+*+* *+*1 ))+5 %# DBCC FREEPROCCACHE FL* 1**= 8 * )+* 3 ) ) 75

19 SELECT [cp][refcounts],[cp][usecounts],[cp][objtype],[st][dbid],[st][objectid],[st][text],[qp][query_plan] FROM sysdm_exec_cached_plans cp CROSS APPLY sysdm_exec_sql_text(cpplan_handle) st CROSS APPLY sysdm_exec_query_plan(cpplan_handle) qp ; 8 3*:A@? A 7) :AF@? A7 *! + + *L )) * 7 9*+ )) * 7 /4 4# ) )) * )) * )7+ /* )* 0 ) + L + ) ) + + ))")) *+ 7 /$#2'# +*L*7+ ) )5 CREATE TABLE TempTable ( Id INT IDENTITY(1, 1),Dsc NVARCHAR(50) ); INSERT INTO TempTable ( Dsc ) SELECT [Name] FROM [Sales][Store]; SELECT * FROM TempTable; DROP TABLE TempTable; F*5 Msg 208, Level 16, State 1, Line 7 Invalid object name 'TempTable' -7*))7 7)'

20 %567! =+* * L 7:A =+ ))) ) > )-7** )) /## 8 ) / 0 * *+ * )))3 L 3 3) :A )) ) 7 / 0+ )))**7 *)*+,!-L) " 3 )!6-7 *) + ) 7)5 /+ 40/ )*6 7)3 40/5376$-65))* #8% * &425$940/ A) A)*)@?A5 40/ = &47-55@? A))-7 67 * )) :A + 7 :A 9* * + ) ) %

21 **<) * 7 +L +**7+ ))) * )) ) :A /)) )70+ * )) ) -8 + ) * -8 ) *) **+L ))) " )*7+ L :AL ))-))* )4#6# #! * /5QQ*** 0 7*):A4 +': F ))': )5 5QQ***7Q?,! )**+ *7 )), * ) -8 + ))+ 7 ))+ )) * *L*)7 7* ) * +7)@? A+* )O ( ) )* 3 * <L+L+ 7:+L 4:#* + L ) 3) *>5 GRANT SHOWPLAN TO [username] * 7) *5 4 " ) )6-7+) 3 * ) 3+ *L ) <? +)*3**5 SELECT * FROM [dbo][databaselog];

22 %567! 8L *% # 3 =+L3 8)*= ))* *5 K,667K >3**) : >AA ) +*+*)$ % #+% ( "# 8L7*+ )+L ) = 7 * * * 3 * 7 ** ) * ))+ 7 -+*)-675 K -67K >*3**K -67K :? 6))KK*7* ) 3)3**)) +7 )73F 7 " -6

23 '," " % ) * ) 7 $ 7 < ) 6A6 + L 3 ) L))) # ; )+)+) **) C+7)) FL * *** *+ ++* / 0 )*)) +* *)F* *** )* 7+ ) 3 * *+ 7 * * * +L!* 3)+)=+ 7) +* 8 S%S+ ++ +S** - )3*)K/ 0K)%SE3+)* 3+3* 7 )**)))* 6#6 6)*+*+**6#6+ * 67+4$5$&6+ ***( "$ 9* =)*5 (-# *)* +* *+ + + *L /0$$ *L7/7) 3) 0+)* -+ )* *+ 7

24 %567! & #P* 3* ) * ) L * $&"*L % $4&P= + )) *+ 3 =)3R )=* $:P =) 73!* )+ * 3 *L )*)) *) ' 4 "% 6)))*)' )*)))(+*))) + A < )=L)**37 * + *P -)+*) Q<+C+< )7*)+*) Q<C+ :<= )* *) Q< /0+)*LC *

25 & FL *+L )F ) ) ) ) 3 + 4# -* ) * ) * )) ) 8 * 7 + L ) * ) * * D* * * ) *3 )* 7 - ) )+ * ) +!)* * * + ) 7+ <$4$5$&6+3 + )D** **7 +32<+* +))+ )+) ) - *L * 3 3 FL * + * +* )*+8 L )7)C 7+7+? ) >* 7 KK ) ) 4*)

26 %567! $ "&? ) ) )+ ) * ) + <=#) ) 3+ + ) 3 + ) ) +))) C < =# +* ) )$&-)*5 $:+* ) 2(+ 2%* 6*3) 7*3:A)3 **37: ' 3$%0 7+ 7*!7> ))FL)* +/*C 0 *5 4 7) L9*+ ) ) **+ **8L )**);

27 ) 3* 7 ) + *L ) ) )+ ; 8+ * 75767@?A67? )767+*L 7)677+)* )35 SET SHOWPLAN_ALL ON; L +*9<8 A-"I-AA<"+7) ) 3 :A * L 40/53755 ) ) 3 ) )+ &$6$+ <6$ <$5$6$ * 40/ '( )7+* 40/53755))+5 SET SHOWPLAN_ALL OFF; 8 3 ) 40/5376$-6 7 * )*#8%+* * /53755) 8)40/ )-7+5 SET STATISTICS PROFILE ON -5 SET STATISTICS PROFILE OFF," 8L* 3**+7 )*5 GO SELECT *

28 %567! FROM [dbo][databaselog]; GO SET SHOWPLAN_ALL OFF; GO # 8 73+*9) )5 ") * 7 7 )*4$5$&6 * *)* * 3 * -**77++L3= L 3+ * * K )K F *+ )) /G0 ) + * **)? ))L ++*32* + ))+*3) ) 40/53755**" - L ) + 6#46&+ $:)+,)A / 0+* 1 *5 4 A * * ) 7 :A 4))) SET SHOWPLAN_XML ON SET SHOWPLAN_XML OFF -)40/ / :A )*+ 7 ))+))@? A-+L 9<8 A-"I@? A )) ) )+ A)-+5

29 ( SET STATISTICS XML ON SET STATISTICS XML OFF," /+ <+L7**7 GO SET SHOWPLAN_XML ON; GO SELECT * FROM [dbo][databaselog]; SET SHOWPLAN_XML OFF; GO +)+*)$5 A)5 \Microsoft SQL Server\90\Tools\Binn\schemas\sqlserver\2003\03\showplan\showplanxmlxsd 7@?A) **** :A? /? 0 F * 7+ ) /) 0 F ))? G # 6 - ) ) )*@?A P )***L * 7+ * + 7 "+*))@? A) + ) 3+ L * ) 5 A) *L!+"7+ 7*L*)+*33) 44#8 +*) ))+ )?#5 <StmtSimple StatementText="SELECT * FROM [dbo][databaselog]; " StatementId="1" StatementCompId="1"

30 %567! StatementType="SELECT" StatementSubTreeCost=" " StatementEstRows="389" StatementOptmLevel="TRIVIAL"> <StatementSetOptions QUOTED_IDENTIFIER="false" ARITHABORT="true" CONCAT_NULL_YIELDS_NULL="false" ANSI_NULLS="false" ANSI_PADDING="false" ANSI_WARNINGS="false" NUMERIC_ROUNDABORT="false" /> <QueryPlan CachedPlanSize="9"> (% ) * ) 40/53755" = 7 + * 7 ) & #4* )+) ' -)+*#+*)*L)*+ + <RelOp NodeId="0" PhysicalOp="Table Scan" LogicalOp="Table Scan" EstimateRows="389" EstimateIO=" " EstimateCPU=" " AvgRowSize="8569" EstimatedTotalSubtreeCost=" " Parallel="0" EstimateRebinds="0" EstimateRewinds="0"> " ) 7 + L 7/ )*07+ +,) A + )) + 5 * )&# + ) 5 <OutputList> <ColumnReference Database="[AdventureWorks]" Schema="[dbo]" Table="[DatabaseLog]" Column="DatabaseLogID" /> <ColumnReference Database="[AdventureWorks]" Schema="[dbo]" Table="[DatabaseLog]" Column="PostTime" /> <ColumnReference Database="[AdventureWorks]" Schema="[dbo]" Table="[DatabaseLog]" Column="DatabaseUser" /> <ColumnReference Database="[AdventureWorks]" Schema="[dbo]" Table="[DatabaseLog]" Column="Event" /> <ColumnReference Database="[AdventureWorks]" Schema="[dbo]" Table="[DatabaseLog]" Column="Schema" /> <ColumnReference Database="[AdventureWorks]" Schema="[dbo]" Table="[DatabaseLog]" Column="Object" /> <ColumnReference Database="[AdventureWorks]" Schema="[dbo]" Table="[DatabaseLog]" Column="TSQL" /> <ColumnReference Database="[AdventureWorks]" Schema="[dbo]" Table="[DatabaseLog]" Column="XmlEvent" /> A+ 3!+)#) * 5 <TableScan Ordered="0" ForcedIndex="0" NoExpandHint="0"> * ))) 5

31 ( <DefinedValues> <DefinedValue> <ColumnReference Database="[AdventureWorks]" Schema="[dbo]" Table="[DatabaseLog]" Column="DatabaseLogID" /> </DefinedValue> <DefinedValue> <output cropped> /+ " F 7 * * K-KF)KTTK* ) ) * 7 K3K *! )+***! # % # ) ) ) 7+@? A)+) */0) <) ))7@? A) +? * A** )@? A)** ***-* +) + " ',*7):A+ )3 F* *+ ) + ))- 1)7 3+* *** ) :A4)@?A7+ 37F7+)3 * + )+ ) 7+ L :A4)*)* + 7 ) :A + * :A ) +C )+ ) :A/C 0) *) ' ' ) ) ) ) + ) )!</5QQ)QQ Q%$($4$70

32 %567! *7)*5 4:#6%5)*7)3* )40/5376$-6:A *7**:A4 + * ) ) *@?A8L )77+)) 4:#6%( +5 + *) ) )) 4:##5)37* )740/53755:A *7+)) 4:##?&#5 *-+ )*3 )) 4:#4 #57 *:A &425$ )7+ ) &4-5:A4:#- 54 # :A)4:#4 #) 4:#- 55)*7)3 7*40/ :#- 5?&#5A*@? A + ) )3 4 5*@? A:+ +)* ) )++) 4:#- 54 #5*7 )3+ ) 7 + A )+= * 6))3*@? A):* ) C * ) " 4" /+ :A4)*@?A@?A7 3=73 )+* 7+)+* 4 * K6 K K* K 7 *@?A*)+/U0 7*@?A 8 *@?A ) )+ ))* +5 (( 4! )+ -+ ) F-,? " )P* -A6>>-6

33 (' >5 :A5! :A5! " 7 A 7 7+*1 )) <*@?A+)@? A+ $'$% 4! < L )+)"))+ *@?A*)))+3 7 "+ K>K 8 + *%

34 %567! (4 "# " A8 L) + ) **+ 7+**)=7+ ))+*@? A )*@?A+ 7 -+)*@?A ) *@? A * + K67 6,K "## 7*))@? A * A A 7+ 7:A!7+* ))

35 ! (& *8 )) * :A 3 ***? )) )3+)+ *L*=* 377 7F* )) ) + + *+ L * * L*

36 5>67)!: ( 6 (,% (, /',$% % $( &, '(, ($ ) 7+ *+ 7 ) 4$5$&6+ <6$+ 234$6 <$5$6$ 3+ *)*1) +*L )*75 P+*L 1P*L*1 * /0$$P)))7!!!P*7 2+<#7 " *+ * 7 * +7 + )*/0* 6)*78* LLLL +***/4# 6#4 09*+ )$# )+*L=$#) )*7?3 ) +*) ) +))!<5 )75 5! # #++ 37,? A/,?A0 ## # + )+ )))7 &*:A 5!!#:A+ -+,+ )+/>0+8+ *L)+!< + *+**) K K +**) <) +* * ),!-,!-+ )* * * +)) +) 5 4# (#+ 4 &# 2%4 &# 2%4 3 # 2%4

37 ( 3 # 2%4 6#4 0 35!1 6 & 4 # &4 # A= $!4# -, >! * ** 73 < *P+ (!!!+ > P 3 ) 7!)+3*)*) 7+ )))? )** ) # AL + 3, )*/))O038& -8 5 SELECT * FROM PersonContact *75

38 5>67)!: (# "# 87)3 ) 7 + **+*7*@7& 7& 2< )**%##$ 7:A!/) ** )+ +* *+) 3-7)++*77 = * )77* * ) ) * 7+ L ) /0$$ ) 3 * > **:A), 5 83)) /0$$5

39 ' SELECT * FROM PersonContact **)5 " 7)))+*** )* * = 73)+ )7 7:A!/)0- #% * * )*+ *+)*7) ) * &# 2% 4 8& +)@7& 7& 2+* 7)

40 5>67)!: " **) 7 *+* = %, 5 '% AL )) 3 8& R 75 SELECT ContactID FROM PersonContact WHERE Address LIKE 'sab%' 8 7 " * )( 7+2-7& 7$# 36$ *-+*? ))* )7 " + ) *)

41 ' "$ A7+77* C ),37+3 = ) ) Q<3)7P7 )!5'" AL3)1)* 5 SELECT ContactID, LastName,

42 5>67)!: '( Phone FROM PersonContact WHERE Address LIKE 'sab%' F*)'5 "% +*O>) ) + ) * 7 2-7& 7$# )3+,-77 ) 3+)3= **)-) 7+)4+ **$#& 2<

43 7& 2< 7)*+* 53 +*)&

44 5>67)!: '4 ") -DA & *7 - D A = * + / *,0 * )7/) )0 )DA3) )) )7!7) 3+)*) 7+DA -DAC* "A1 )* & + * * 7 +* AC)

45 '& "* +"A1)1 ) ) + D A 3+ " A1 *) 7DA )D A* / 7* 0+ " A * *7 # ))7*% 3** +*) F 7)*35 SELECT * FROM [dbo][databaselog]

46 5>67)!: '$ " - )+ L) )7 +3=*) *-* **) +78 /10)*) + * ) ) ) 3 =**7- +3=)* 7* *)** - )* + <+) *+ * * * 3 )* *+ 7) (, 5'"

47 ' )*)))<5!3 + * )) ) 7 2< 5 SELECT * FROM [dbo][databaselog] WHERE DatabaseLogID = 1 "+ )3+3=)) 7 8 7))*/0$$ +379***;

48 5>67)!: '# "# ) ) 7+*K!%K< K!%K 7)3 "7+3=)>,AC+*) / L70+*) )* *+ L7/ *0+*)7 Q< *)) )) +* *"A

49 4 "## # 7 )>,A+K!%K+ /)>, A0*)3 +* +*L ))!)>, A*+3* ) Q<P * C*+*** AL 1 3 )* 3 )+ 353) SELECT e[title], a[city], c[lastname] + ', ' + c[firstname] AS EmployeeName

50 5>67)!: 4% FROM [HumanResources][Employee] e JOIN [HumanResources][EmployeeAddress] ed ON e[employeeid] = ed[employeeid] JOIN [Person][Address] a ON [ed][addressid] = [a][addressid] JOIN [Person][Contact] c ON e[contactid] = c[contactid]; 7)3*)% "# * 7)) * +* ) +5 % 78 /'4S0 9? 1 *0 8$# 8/S0 ( 78& /%S0 AL)* )% +)* 7 0 8$# + * 7 8 *7+ L)! +*%(+* * ,7&7 4' 27#&*%#+&%'* *

51 4 "#$ 3=2&+* =+ )7 + ***78 %#+&%'*'4S)3) %'%( 3 =) 7 ) +* 3 = ))* +)) * 7+)*7 0 >+7)7 )*%'5

52 5>67)!: 4( "#%!) * * 9? 1 + * * * 5! # 9 3 * ) )37+ *) 3 )* **)A + 9) *:A)))**+ +)) - ++)3 = + + * 3 ) * 7+*) + + "**+0 1*:A1* *))* 1+ + * +)** 1!) + =+ ) <+) +9?1 )) )1 7+ ) 0 8$#82 *8 9? 1)))*+)) 9? 1**) 1+ ))* 7 < + 9? 1 )) 1 /" A? ? 1 75

53 4' 7 /0$$ /0$$* / +KKL 0 *L77 8 9? 1 ))*)3=1* +))*1* ) 8 96> >6 *+ 9? 1 ))1 ) + )++ 9? 1 *)1 87 * )* +%&/L %&*0+ )*+#-))** + ) - ))))6-3 ) * 7 0 8$# 9? *))9? 7, 5 -)9? E+*&#2%4 8& + 7& 2+ * 7) 7 *%4 "#&

54 5>67)!: ") 4 )%4 + * 1 *& 20 8$# 8& % " 7 44 *7+ 1 *)+351+*%& "#) #>) * ) 1 / 70)* )*) **+)) )) 1C+)1 *7 " +7*)%+ )+* )*%#

55 4& "#+ ) + ) + ) P + $#3 * & 853 & 83 * * 8 *=++L7)3 )) + 7! 9 " A E+ 3 = )! 1 7 )? E+ * )* -8 5 SELECT ccustomerid FROM SalesSalesOrderDetail od JOIN SalesSalesOrderHeader oh ON odsalesorderid = ohsalesorderid JOIN SalesCustomer c ON ohcustomerid = ccustomerid 37*)%$ "#* -7+3=) 7 & 7 4#0

56 5>67)!: 4$ /0$$*)3+*) * "7+*) &4#0 1!1 -? E * 1 7+**)? E+*)%+* 1 4# &2< + 1 -? E))*1* +* 1 )1+3= ) 0 1 )+ )? E+ 0 ) )) 9 E 3 = 7!AB <? E1*) + 1)* 9? E+ * * - )+ 1 * ))? E1 ) + 3 = ) )? E+? E*1 + ))7

57 4 *( <)3*3*) 5R*+/0$$8L* + 37 >)* **) /+ * /0$$ SELECT e[title], a[city], c[lastname] + ',' + c[firstname] AS EmployeeName FROM [HumanResources][Employee] e JOIN [HumanResources][EmployeeAddress] ed ON e[employeeid] = ed[employeeid] JOIN [Person][Address] a ON [ed][addressid] = [a][addressid] JOIN [Person][Contact] c ON e[contactid] = c[contactid] WHERE e[title] = 'Production Technician - WC20' ; *7)35 " ) +* = ) /0$$ 7+/0$$ )* +* * )&#2%4 /)%0

58 5>67)!: 4# "# =+ +* )+* * 8*78& + 3+=* ))351 =* * L * ) + 7 * 1 ) 0 8$# )7 )-))1 * */0$$+3* /0$$ 3+ * ) * :A * KK**+* 1 * +*/0$$* 3 * ) + 7+ * )*)) )=)3+ * * /0$$ + * %%'4 '4 )

59 & 3L))+3E* * 3 4 ($' &: $(( &: 8 3+ )) 7 *<$75 SELECT * FROM [Production][ProductInventory] ORDER BY [Shelf] 7*) " &#2%4 4) 7+)* * *3=*7 )<$ )+)+*)) 4/)( *0

60 5>67)!: &% "$ )**)/)'0+L %&# * %&# * ) 7++%&#* "% $&S))3 7+*37

61 & - ) + * * 4S) 3L 7+)*= ** ))*5*/0$$? +3* 9*+)/0$$7+) *3 )* +** <5 ; )+ L ;7+ 7 ;* + )+*)7 )7+*3)+ ) *)*)3 )*3)*5 SELECT * FROM [Production][ProductInventory] ORDER BY [ProductID] 87*)45 "& -33+<$ +*L7 * +*7+* + ) 73== + ) )+:A4))8 )+ :A 4 ) )>-? )!) +:A ++ * 8 + :A 8+* ) )) )+8+>-? *9? )19? * * 3 AL 3 &365

62 5>67)!: &( SELECT [City], COUNT([City]) AS CityCount FROM [Person][Address] GROUP BY [City] 7* * ") 37 * 7+ )*) 3/0$$)** ) 3&36 ) 3 =*)+)9? "9? 7*KK * ) 9? ) 1- *9? *1+9? *:A )* +*KK<+ : )+ * * KK)) /0$$ )* + ) )* 0= SELECT [City], COUNT([City]) AS CityCount FROM [Person][Address] GROUP BY [City] HAVING COUNT([City]) > 1 7**)$5

63 &' "*!0= #)+&+ %<) )**) 0=23)8 )*0 4$4 #L(' " 8 0=23)+ 3+ 0=23 ))-*7+ )*))3*+* /0$$3 )* (# (4 " 8 7)++* 5 -> 6> ->*6>*? )+) * =+ )7++*** * * +* 8 + <> 7 +

64 5>67)!: +=3 2(+ )+ +/0*) 3%(+,)+ +3%(+ +))+) )*&#(+- &#(+ - *) )2(+ - * )2(++ )) - *))1 - * * ) 8 ) + *+ )+*7)) *)!L8 ** +*7+) ))*7+ *5 " 7 >: >* B ))*+ ** * 4$%)6$+* * 3 = 3? ) * - -) + + ) ** === * +1*-+ )7*)=) * + * * ) * ) * * ; )* 3*3=+ 2(+*"<) 1 )<")1 + ) **3 )* )1 *),!-;+) * * + Q< )+ *+ ) )+ *3+) &4

65 && 7+))3)* *+ Q< ),;'" 67)3 ) )* 3L 8 7 )4$5$&63+**7) 234$6+<6$+<$5$6$3, 9234$65 INSERT INTO [AdventureWorks][Person][Address] ( [AddressLine1], [AddressLine2], [City], [StateProvinceID], [PostalCode], [rowguid], [ModifiedDate] ) VALUES ( '1313 Mockingbird Lane', 'Basement', 'Springfield', '79', '02134', NEWID(), GETDATE() ) ; / L)) *0+*# "+ 7 ))+ )+ * * 5 & 4 ) * 3 +L *)7* ))&4 #)! *3*) )*")*+*7*+* )+) - * ) ) ) * + ) $6<6$)-)&#2%2+* 1 ) ) =" ) "6> + 884' * ) &# 2% 4 84' *+* "6>)

66 5>67)!: ) ) ) 4' 2 1 * 7* 7+-) 7)67%%'3=<+*+ * 884' 2 ) ) 84' R * ) '" )*5 &$ UPDATE [Person][Address] SET [City] = 'Munro', [ModifiedDate] = GETDATE() WHERE [City] = 'Monroe' ; 7* *5 "$ AL 7+))) 7+*)*)7+ +*)) ) 7 ) ) )*/0$$C&DE <6$7+)* +) + ) 6* <6$3 35 )6)4$5$&6+<6$+ ) + + ) * + 64$5$&6 7$!4#/)) 0 ) * 1 A7+) */ ) " A ) /* 7 +** 70 3+**3 7+** ) +7+ $6<6$(+)3 "**)<6$+ 7C + )7) * + -)+*:AA6+* <6$

67 & ) + ) * ) * * 7+ 7*)+* )) +7* )) )+)* Q<)3 <6$ 8 )7*<$5$6$;7+L )*7 DELETE FROM [Person][Address] WHERE [AddressID] = 52; (%*75 "$# * )) 1*** * > +*+*+ 3- ) ** * +)))) +4$5$&6<$5$6$ + +* 7, )) )*) &#2%4 6<#*5 Prefix: [AdventureWorks][Person][Address]AddressID = Scalar Operator(CONVERT_IMPLICIT(int,[@1],0)) +F A+*2 )L + * L );)3 3 +)=

68 5>67)!: &# "$ -) + ) 7 7 )"AE)A)E )1 ** 3) ) E+ )* *L+))7 )+ ) + * + <$5$6$ "$$

69 ! $ 1 * 7 9*+ * ) +* ) )*3)= 371 +L7 = A * = 7 ))!7+*) =+)7)7

70 $% ( < / % /+ /',$% % $( &, '(, ) * *7@? A78 A7L+ *7+*7@? A7 ) :A * )+*7? ) 7+))77 ) 7 7 L,!-*)*+7 7 :A ** 7 7 $ +?)@?A:A4 A7 +@? A A *? ) 7 7 A) )));) 6+@? A )) ) +@? A )*,!-+? =7 ++L))) ) * 7 7 ) ) ;L3* )**:A4 )+ )))7+ ))@? A))<+) ):A++ +* *7 ) :A +**) "! AL 77)3**+ +*L5 SELECT ContactID, LastName, Phone FROM PersonContact WHERE Address LIKE 'sab%' $:A4))7 7+ ) *

71 $ "# "*+*L37> 40/53755 * * 7 " :A ) 7+40/53755))5 SET SHOWPLAN_ALL ON ; GO SELECT ContactID, LastName, Phone FROM PersonContact WHERE Address LIKE 'sab%' GO SET SHOWPLAN_ALL OFF ; GO )+*) 5 " >*%+46%)*7) :A * 7 ) + L ) 6+* "$ *):A*7/6A6+ 0*537/537/+ 46% ) -346%)* 5"A1+7+7 C)+LK) K 7 )* ) + * ))/G0+8 )3**)?+>G>7+ *))*)77

72 ) 32< +*, ) +8 +)* +7) 7+ 7 ) 7?**+**(/" (0* 7!746%/7!0* 7*8& 5 OBJECT:([AdventureWorks][Person][Contact][IX_Contact_ Address]), SEEK:([AdventureWorks][Person][Contact][ Address] >= N'sab' AND [AdventureWorks][Person][Contact][ Address] < N'saC'), WHERE:([AdventureWorks][Person][Contact][ Address] like N'sab%') ORDERED FORWARD <=# ) 537/ ) * 3/6A68 96>60+5 [AdventureWorks][Person][Contact][ContactID], [AdventureWorks][Person][Contact][ Address] < /0 3 3),)B) 3=7 + ) $: (+ 7 %#* "7 * ' /", 40+ * 7 8& * * ** 7 ) + * * + +)+8*) 46%)5 --Clustered Index Seek(OBJECT:([AdventureWorks][Person][Contact][PK_Contact_ContactID]), SEEK:([AdventureWorks][Person][Contact][ContactID]=[AdventureWorks][Person][ Contact][ContactID]) LOOKUP ORDERED FORWARD) "+)$:+%* 9*+)7)+$$% +L %#*3 $$% 7 ))37)D AC<+ )7+*) ) * % *- ) + *= 8"*/",0**"A 1 ) * <=#"+*+ 5*& 2< ) + $:+ $2+ 6#46& + )) ) +**L! "! 7 )3*)*9*+* AL 6 7 ))*3+)1/0$$5 $(

73 $' SET SHOWPLAN_ALL ON ; GO SELECT c[lastname], a[city], cu[accountnumber], FROM st[name] AS TerritoryName [Person][Contact] c JOIN [Sales][Individual] i ON c[contactid] = i[contactid] JOIN [Sales][CustomerAddress] ca ON i[customerid] = ca[customerid] JOIN PersonAddress a ON [ca][addressid] = [a][addressid] JOIN [Sales]Customer cu ON cu[customerid] = i[customerid] JOIN [Sales][SalesTerritory] st ON [cu][territoryid] = [st][territoryid] WHERE st[name] = 'Northeast' AND a[stateprovinceid] = 55 ; GO SET SHOWPLAN_ALL OFF ; GO 8 73+*'* 7)) "% *)+)/G0 + ))+* ' 2 "& */0$$ )* --Index Seek( OBJECT:([AdventureWorks][Person][Address][IX_Address_StateProvinceID] AS [a]), SEEK:([a][StateProvinceID]=(55)) ORDERED FORWARD)

74 $4 ) 2+ ) 4$5$&6 + ) )* 3 * )* 3 7 )* 7 7+2)7-+46%+ *7 --Clustered Index Seek (OBJECT:([AdventureWorks][Person][Address][PK_Address_AddressID] AS [a]), SEEK:([a][AddressID]=[AdventureWorks][Person][Address][AddressID] as [a][addressid]) LOOKUP ORDERED FORWARD) +)*11 /*0 ") **)*+**%%+*) 3+ 7 & --Index Scan( OBJECT:([AdventureWorks][Sales][CustomerAddress][AK_CustomerAddress_rowguid] AS [ca])) %#+U*)7+9? 1 *& "* * *)? +* /*%0 )* )+*) 7 7&7&2 +*))*= L *"A

75 $& " +*) 3+ ) "+ * * + /G @72'#7&2 + ) " A+)**+ )*7L )>*'>*%4+*78& "# + >*(+*)"A1 *' 7*%4 "A 1

76 $$ "## * * >* %&+ * ) 7 4#84#6 )+)) "A13+*)*) -))+7 7+* ) ) 3 A 3 +=)8 L * * * ) 7+*7 7* /+ L ),!-* ) * 7 ) 9*+ * :A +* * K))K) * *:A4+*)@? A A )) * 7+ *,!- *)**)7@?A )9+*** * 77+@? A =+ ) /* 30+*=+ 7) +,!-*L )+ * )+, 6 8 =)7 A ) + )7,!-*)+ )3+ A +$+* 3 = ) 7 3 )*+* ))@?A

77 $ /+ + L 7 + L 7**7 8 40/ @? A)/ )** / ** 7 SET SHOWPLAN_XML ON ; GO SELECT c[lastname], a[city], cu[accountnumber], st[name] AS TerritoryName FROM [Person][Contact] c JOIN [Sales][Individual] i ON c[contactid] = i[contactid] JOIN [Sales][CustomerAddress] ca ON i[customerid] = ca[customerid] JOIN PersonAddress a ON [ca][addressid] = [a][addressid] JOIN [Sales]Customer cu ON cu[customerid] = i[customerid] JOIN [Sales][SalesTerritory] st ON [cu][territoryid] = [st][territoryid] WHERE st[name] = 'Northeast' AND a[stateprovinceid] = 55 ; GO SET SHOWPLAN_XML OFF 5

78 $# "# )+ A )) 7 +* 7+KUKKK *)+) 3 - * )) ) 55QQ)Q3Q'Q$Q*Q) ))*@? A** # -)) #/ %0+)) )?#5 <QueryPlan CachedPlanSize="52" CompileTime="29293" CompileCPU="6277" CompileMemory="520"> =)+*)+C "7 7 + *!2% ) 7 7 =8 ) 7 )+)7+, 6 8 =+* )+ ) )7 # - %+ ) ) *@?A+*@? A):+*@? A) F*@? A)*I7I3I

79 <MissingIndexes> <MissingIndexGroup Impact="308535"> <MissingIndex Database="[AdventureWorks]" Schema="[Sales]" Table="[CustomerAddress]"> <ColumnGroup Usage="EQUALITY"> <Column Name="[AddressID]" ColumnId="2" /> </ColumnGroup> <ColumnGroup Usage="INCLUDE"> <Column Name="[CustomerID]" ColumnId="1" /> </ColumnGroup> </MissingIndex> </MissingIndexGroup> </MissingIndexes> 7 + >< + )+ =)+* 32E*+))355 <RelOp NodeId="0" PhysicalOp="Nested Loops" LogicalOp="Inner Join" EstimateRows="194953" EstimateIO="0" EstimateCPU="637415e-005" AvgRowSize="119" EstimatedTotalSubtreeCost=" " Parallel="0" EstimateRebinds="0" EstimateRewinds="0"> )* ))**) " + ) 7 + * 1 $$% +@? A ) * ))**3+* *7 7+ ) " V&+ ) 7 + * " A1" V+*5 <RelOp NodeId="26" PhysicalOp="Clustered Index Seek" LogicalOp="Clustered Index Seek" EstimateRows="1" EstimateIO=" " EstimateCPU=" " AvgRowSize="28" EstimatedTotalSubtreeCost=" " Parallel="0" EstimateRebinds="127784" EstimateRewinds="147074"> 8 7)3+**K667V%4'#%&K > " * ) &# +) 5 <OutputList> <ColumnReference Database="[AdventureWorks]" Schema="[Person]" Table="[Contact]" Alias="[c]" Column="LastName" /> <ColumnReference Database="[AdventureWorks]" Schema="[Person]" Table="[Address]" Alias="[a]" Column="City" /> <ColumnReference Table="[cu]" Column="AccountNumber" ComputedColumn="1" /> <ColumnReference Database="[AdventureWorks]" Schema="[Sales]" Table="[SalesTerritory]" Alias="[st]" Column="Name" /> A+ 3 ))KK KK / KK0+ K-K / KK0 KK /KK0+3/A"++-" "0)* * ****

80 )"++*) " A+ +* L % <NestedLoops Optimized="0"> <OuterReferences> <ColumnReference Database="[AdventureWorks]" Schema="[Sales]" Table="[Customer]" Alias="[cu]" Column="TerritoryID" /> </OuterReferences> / @?A+ )*8*L+1)) SET STATISTICS XML ON ; GO SELECT c[lastname], a[city], cu[accountnumber], st[name] AS TerritoryName FROM [Person][Contact] c JOIN [Sales][Individual] i ON c[contactid] = i[contactid] JOIN [Sales][CustomerAddress] ca ON i[customerid] = ca[customerid] JOIN PersonAddress a ON [ca][addressid] = [a][addressid] JOIN [Sales]Customer cu ON cu[customerid] = i[customerid] JOIN [Sales][SalesTerritory] st ON [cu][territoryid] = [st][territoryid] WHERE st[name] = 'Northeast' AND a[stateprovinceid] = 55 ; GO SET STATISTICS XML OFF ; GO 8 * - + *?# ) <!##/ $0 +))7)35 <QueryPlan DegreeOfParallelism="0" MemoryGrant="82" CachedPlanSize="57" CompileTime="29293" CompileCPU="6277" CompileMemory="520"> 1)) *-@? A7 62+*+ * )7))8 ) + 3) <RunTimeInformation> <RunTimeCountersPerThread Thread="0" ActualRows="4" ActualEndOfScans="1" ActualExecutions="1" /> </RunTimeInformation> <NestedLoops Optimized="0">! -+@? A+,!-L*L *+),!-**

81 +*** ) + A ) ) )@?A +,!-) ))7 )@?A+*,!- *,!- *

82 '5C?7: ( ( = '%(%,% +$( $+/ '(: % -*L+3*7+ * 7:A;77 9*+ 1 :A * ) + 7 7)*7 * * :A 3 *L )*L*7+ ))+)7+))-8 * 8L * ) -85 CREATE PROCEDURE NVARCHAR(3) AS SET NOCOUNT ON ; GO SELECT [st][salestaxrateid], [st][name], [st][taxrate], [st][taxtype], [sp][name] AS StateName FROM [Sales][SalesTaxRate] st JOIN [Person][StateProvince] sp ON [st][stateprovinceid] = [sp][stateprovinceid] WHERE [sp][countryregioncode] ORDER BY [StateName] 8 *75 EXEC = 'US' 73+*%5 "#

83 ' # )++*&#2%4 +* ) + F&!&+ ** 4, ) 4#6% 2% 4 ) 351*)4 "7+*@5 )+ + )*+71 ) 3 5 * ) ) 8 L7+*L +:A**)* *4$5$&6:** <)*:A'# ) )* +) L) )*4$5$&6 F *4$5$&6*) )4$5$&63<+4$5$&6 *+* L )3 * * + ) * # 4 # C )*+*** 7 +) +*L *3)1) 5 SELECT [p][name], [p][productnumber], [ph][listprice] FROM [Production][Product] p INNER JOIN [Production][ProductListPriceHistory] ph ON [p][productid] = ph[productid] AND ph[startdate] = ( SELECT TOP ( 1 ) [ph2][startdate] FROM [Production][ProductListPriceHistory] ph2 WHERE [ph2][productid] = [p][productid] ORDER BY [ph2][startdate] DESC )

84 '5C?7: 4 " 8 *3))* 7+** *!1? E ) 7 * + 3 -? E*)*! +*/7) 1R ) *0 * )*)))*)- *))** *8 ++)) -* L 7+? E * )* )* )9 E+*3=8* * 6 /) *0 KK

85 & "$ "7+** 7* +)&$S))3 (#4 * ) 3+ 6(A+ ) * * -* ** **)* 3) ) ) * 6 )** 3+K%K #* *+1 *C D8C D C D8C 5 0D + * C4<D3'5

86 '5C?7: $ "% * ) 1 "A + ) # : "***)7* +* *3 ))*+ *L * :A 4 5 * 3+*))7 :A4*) +)*) ) 5 +&44 5 6$ 5 5 * )+ + * )*) *) KK ))*-AF+55QQ) QQ Q%$4%4&7!* 7 ) * 3 > *)) SELECT [p][name], [p][productnumber], [ph][listprice] FROM [Production][Product] p CROSS APPLY ( SELECT TOP ( 1 ) [ph2][productid], [ph2][listprice] FROM [Production][ProductListPriceHistory] ph2 WHERE [ph2][productid] = [p][productid] ORDER BY [ph2][startdate] DESC ) ph

87 )*)7 +* 45 "& 6 * * * * ) 5 ) % 4 63 )35 *)*3));<*) 3*4$ & < )? 3 8 *)3+* +5 (293 row(s) affected) Table 'ProductListPriceHistory' Scan count 396, logical reads 795, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob readahead reads 0 Table 'Product' Scan count 1, logical reads 15, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0 )*3 +5 (293 row(s) affected) Table 'ProductListPriceHistory' Scan count 504, logical reads 1008, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob readahead reads 0 Table 'Product' Scan count 1, logical reads 15, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0-3+3* 3 )*/$#403* /%0 )*)*/0$$) 35 WHERE [p][productid] = '839' 8 *3*/0$$+** &5

88 '5C?7: # ") # ++ )7 )) / * + &#2%4 *3 )*/0$$ 3+**$5 "* 4+* &# 2% 4 &# 2% 4 + ) /0$$*=)7) *+)* "*+L <))3+* * 8 *3* +*5 (1 row(s) affected) Table 'ProductListPriceHistory' Scan count 1, logical reads 4, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0 Table 'Product' Scan count 0, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0 8 *3*3+*5 (1 row(s) affected) Table 'ProductListPriceHistory' Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0 Table 'Product' Scan count 0, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0 "*+*)/0$$+3))+* + 3*'

89 # ):A 7 * + 7 )! > ) )) 3*** * )+ *+ +*) /0$$ 8 /0$$ + + * 5)C7)) *) # " :A4:A+* + 7/60-6KK7 *):A *)* :A * ) )+ ++C +6 )) )*3 # <))6 '-8 ) ) 7+ 3+$#!+)*5 ALTER PROCEDURE [int] AS BEGIN SET NOCOUNT ON; -- Use recursive query to list out all Employees required for a particular Manager WITH [EMP_cte]([EmployeeID], [ManagerID], [FirstName], [LastName], [Title], [RecursionLevel]) -- CTE name and columns AS ( SELECT e[employeeid], e[managerid], c[firstname], c[lastname], e[title], 0 -- Get the initial Employee FROM [HumanResources][Employee] e INNER JOIN [Person][Contact] c ON e[contactid] = c[contactid] WHERE e[employeeid] UNION ALL SELECT e[employeeid], e[managerid], c[firstname], c[lastname], e[title], [RecursionLevel] Join recursive member to anchor FROM [HumanResources][Employee] e INNER JOIN [EMP_cte] ON e[employeeid] = [EMP_cte][ManagerID] INNER JOIN [Person][Contact] c ON e[contactid] = c[contactid] ) -- Join back to Employee to return the manager name SELECT [EMP_cte][RecursionLevel], [EMP_cte][EmployeeID], [EMP_cte][FirstName], [EMP_cte][LastName], [EMP_cte][ManagerID], c[firstname] AS 'ManagerFirstName', c[lastname] AS 'ManagerLastName' -- Outer select from the CTE FROM [EMP_cte] INNER JOIN [HumanResources][Employee] e ON [EMP_cte][ManagerID] = e[employeeid] INNER JOIN [Person][Contact] c ON e[contactid] = c[contactid] ORDER BY [RecursionLevel], [ManagerID], [EmployeeID] OPTION (MAXRECURSION 25) END; % 6 5 5QQ*** Q3Q34Q34 7Q

90 '5C?7: A5 #% SET STATISTICS XML ON; GO EXEC = 9; GO SET STATISTICS XML OFF; GO 8)77L * 8*7@? A )5 " )&# 2% 4 0 8$# 8& 4 # KK )3)+ 5'#+3 ) 7+*+ ))& ) & ):A )#5 "+ *6# 4# ) -+)* 1 A7+) */)"A70 3+ ) - 1 ) ))32355)*6

91 # )" <RunTimeInformation> <RunTimeCountersPerThread Thread="0" ActualRows="4" ActualRebinds="1" ActualRewinds="0" ActualEndOfScans="1" ActualExecutions="1" /> </RunTimeInformation> * )* + +* +* * ** R** )*-* ))*%5 "# -) & * 2%4# ** +* *1 ) *1K3KP*))* + * * ) * * / + 7) **1)**7**L** * 34 *+ 4#8'2'#&+ ) + ) + + )-3)*5 SELECT * FROM [Sales][vIndividualCustomer] WHERE [CustomerID] = ; 7*%%5

92 '5C?7: #( "## 8 *+'2'#&+ * * ) 3; > * :A * + * 1 ) =+ +7 ))+3=* 1+* 1)** *)37) 3 + * * + L * )3=))**, 34-7B*+=*+K*7K - 7 * 7 + 7*++* 7*) ) ) 3+ 7 *+ 1) 3 7*+++ *? 7*)) ) 7* +*7*< +)7* ))+ ) *7*7+)) 1234$6+234$6* 7*-,!-+)* 7** 7* ) :)7* )+*7+ *4$5$&63+) 3 7+ ) 7 * *-8 '4' &! 4' &! )35

93 #' SELECT * FROM [Person][vStateProvinceCountryRegion] 7*%5 "# 7 * 7 *+ 7 * * 757)*+=* 77* 7 =+ 3L )7*7+)*3*7 7*%5 SELECT sp[name] AS [StateProvinceName], cr[name] AS [CountryRegionName] FROM [Person][StateProvince] sp INNER JOIN [Person][CountryRegion] cr ON sp[countryregioncode] = cr[countryregioncode] ; ==7 * 9*+ 7 7+)*35 SELECT a[city], v[stateprovincename], v[countryregionname] FROM [Person][Address] a JOIN [Person][vStateProvinceCountryRegion] v ON [a][stateprovinceid] = [v][stateprovinceid] WHERE [a][addressid] = ; )71 *7*8 + * 5 "#$ )7)=*+**)%+ = ) ) 7 7 * *

94 '5C?7: #4 * 3 ) * ) + )7** %%, - ) )) 7 L+)7* -)33*+K*)7 ;K ) 7 )) 3 = 7=)9*+)7 + *+ ) ),, &55 5" <) *3 5 K KL3)*-* + :A 4 ) +)* +!A*7)*+ *+)+ 7/ <= ) : 6 7+)7/@5 0+*++ )/2< !A + 3) ) 7)* 7 ) 7+ +) 8*35 SELECT [sod][productid], [sod][orderqty], [sod][unitprice] FROM [Sales][SalesOrderDetail] sod WHERE [sod][productid] = *%'+*) ) %%*+** 7**"<6@-", + 9)4

95 #& "#% 2% 4 ) * * + 3 )) C)+ 7 2 "#& -))%4+7) 7+4#24#<#27 =*7) 8DAC+* =3 3+? +)7 :A+** ) #<#7 2+ 9*+ :A 4+*)23&5<$ * 7

96 '5C?7: 23&5<$ * 7 :A 4 ) ) *7+)+ )7)+)))) ) ) )7+ )*+**723&5<$ 7)**L+* &4-5+ ))**)* &4-5 )) 7))* #$ IF EXISTS ( SELECT * FROM sysindexes WHERE OBJECT_ID = OBJECT_ID(N'[Sales][SalesOrderDetail]') AND name = N'IX_SalesOrderDetail_ProductID' ) DROP INDEX [IX_SalesOrderDetail_ProductID] ON [Sales][SalesOrderDetail] WITH ( ONLINE = OFF ) ; CREATE NONCLUSTERED INDEX [IX_SalesOrderDetail_ProductID] ON [Sales][SalesOrderDetail] ([ProductID] ASC) INCLUDE ( [OrderQty], [UnitPrice] ) WITH ( PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY] ; GO SET STATISTICS XML ON ; GO SELECT [sod][productid], [sod][orderqty], [sod][unitprice] FROM [Sales][SalesOrderDetail] sod WHERE [sod][productid] = 897 ; GO SET STATISTICS XML OFF ; GO --Recreate original index IF EXISTS ( SELECT * FROM sysindexes WHERE OBJECT_ID = OBJECT_ID(N'[Sales][SalesOrderDetail]') AND name = N'IX_SalesOrderDetail_ProductID' ) DROP INDEX [IX_SalesOrderDetail_ProductID] ON [Sales][SalesOrderDetail] WITH ( ONLINE = OFF ) ; CREATE NONCLUSTERED INDEX [IX_SalesOrderDetail_ProductID] ON [Sales][SalesOrderDetail] ([ProductID] ASC) WITH ( PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY] ; GO EXEC = = N'Nonclustered = = = = = = N'IX_SalesOrderDetail_ProductID' ; Run this code in Management Studio with the "Include Actual Execution Plan" option turned on, and you will see the execution plan shown in Figure 16:

97 # "#) 7 ) * 7*+,! AL * 3 ) * 7 + * = 7 +L )*))*) * ) L "#* - %+)7 )7)))7 # '8-7L )* + )* ) 3)*-)+3 )*7)7 +)7* = 7+ 4#84#<# # <#7 2<+ 2<)7,!+40/ &45 DBCC SHOW_STATISTICS('SalesSalesOrderDetail', 'IX_SalesOrderDetail_ProductID') *)C+ 5 All density Average Length Columns ProductID E-06 8 ProductID, SalesOrderID E ProductID, SalesOrderID, SalesOrderDetailID

98 '5C?7:, + * + 7 +*)($4#(##+* =*)7-7 * 7L* )7)+)) +L*)7 ))73 )) ) 7 * AL L 7 )3)+ L ) ) AL -8L *= 3 ) 3 ) 0125 ## SELECT sodorderqty, sod[salesorderid], sod[salesorderdetailid], sod[linetotal] FROM [Sales][SalesOrderDetail] sod WHERE sod[orderqty] = 10 7)3*%5 "# 8&#2%4 # )+*?EA* AL*735 CREATE NONCLUSTERED INDEX [IX_SalesOrderDetail_OrderQty] ON [Sales][SalesOrderDetail] ( [OrderQty] ASC ) WITH ( PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF,IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY] C)+)+LL* %R*+*7** *+ )+ 7 + L 7 * 7 <&& 40/ &45 All density Average Length Columns OrderQty E-05 6 OrderQty, SalesOrderID E OrderQty, SalesOrderID, SalesOrderDetailID 8)?%) 2+?73) +%%(%$*4#<# '% )?1L+ )+37 ))3

99 % )*3*+** 7 ) = F ) = 7* 3+ +*L)) 3/ 40 > 7+ * ++*7 + )3 ) L)* -8+ L* 7* 5 DROP INDEX [Sales][SalesOrderDetail][IX_SalesOrderDetail_OrderQty], ))) *)) * ")) *+ 7 )* 7 * + *+ +73**))7 7+ * +*75 IF EXISTS ( SELECT * FROM sysobjects WHERE object_id = OBJECT_ID(N'[NewOrders]') AND type in ( N'U' ) ) DROP TABLE [NewOrders] GO SELECT * INTO NewOrders FROM SalesSalesOrderDetail GO CREATE INDEX IX_NewOrders_ProductID on NewOrders ( ProductID ) GO /?@A)0-) Estimated Plan SET SHOWPLAN_XML ON GO SELECT [OrderQty],[CarrierTrackingNumber] FROM NewOrders WHERE [ProductID] = 897 GO SET SHOWPLAN_XML OFF GO BEGIN TRAN UPDATE NewOrders SET [ProductID] = 897 WHERE [ProductID] between 800 and 900 GO -- Actual Plan SET STATISTICS XML ON GO SELECT [OrderQty],[CarrierTrackingNumber] FROM NewOrders WHERE [ProductID] = 897

100 '5C?7: %% ROLLBACK TRAN GO SET STATISTICS XML OFF 4 + %0+ )! ):A*) A5 "#+ -75 "! )%#) 7+ + * 7* 4$5$& )+ 2< 5 ) *) )* 9*+) * +*) '((%*%'('")+L +** 7 + *+ + 7**7 *)) **+*)+ + ) 7 7 ) = 3 * *) 7 L

101 % L) F) F) 7+

102 4567*9 ( > $%($,% /',$% % *,,% %(! *=++ 5?0=7) 3 10=13 6#0 )7) L *) )+ L )*5!-) * 3 * )))1)3+L!!:!8 )) + L L )7* ) + * ) 3 )3)))) *)*) :)623 7)*5 SELECT OPTION (<hint>,<hint>) : L 234$6 7* * 4$5$&6 FL3!)*+*51) O?$(( ($' *P040<$P /*<24623&6& 6$0 =+ 7 **3)3 )))7L SELECT [c][suffix], COUNT([c][Suffix]) AS SuffixUsageCount FROM [Person][Contact] c GROUP BY [Suffix] 3 ) L * ) 3 ) )

103 %' ))+)+7+ *%5 "# -+ K)K=) 7*9? /-0 * + ) 7 ) 4#$+*5 " L ) * + * )) 9? <$35 SELECT [c][suffix], COUNT([c][Suffix]) AS SuffixUsageCount FROM [Person][Contact] c GROUP BY [Suffix] OPTION ( ORDER GROUP ) **(5 "$

104 4567*9 8L=+<$+ ) +L )46)4!!!+*** -*+3)4#$ )%$$$'4)+) ) 7,+)*+7 + =9? ) - +:6*=* )+)8 3* )=+L =3+*))7 )7 +(??$% '%,$% )) * C" <" 3+ =+) * *)* )) )*C" <"7 7 3 * ) ) ) 5 %4 SELECT [pm1][name], [pm1][modifieddate] FROM [Production][ProductModel] pm1 UNION SELECT [pm2][name], [pm2][modifieddate] FROM [Production][ProductModel] pm2 "% F = + 7)++ )*7 )($$ ) ) C" <" * )) )+ $$3235 SELECT [pm1][name], [pm1][modifieddate] FROM [Production][ProductModel] pm1 UNION SELECT [pm2][name], [pm2][modifieddate] FROM [Production][ProductModel] pm2 OPTION ( MERGE UNION )

105 %& "& F ) 323! 1 ) & 9*+? E**)+ *L)=*4)3 )($$4'L* 8 ) SELECT [pm1][name], [pm1][modifieddate] FROM [Production][ProductModel] pm1 UNION SELECT [pm2][name], [pm2][modifieddate] FROM [Production][ProductModel] pm2 OPTION ( HASH UNION ) *7+*&5 ") 7 )+* 9*+ /'#$0)3+) ) 3+ 9? L )) ) +*) )3+ ))3 $$?+(? 7$,% 13 9*+)1/0)1+ 13 FL)))) Q<+ )3!)) )? L ))*3*9 375

106 4567*9 %$ SELECT s[name] AS StoreName, ct[name] AS ContactTypeName, c[lastname] + ', ' + c[lastname] FROM [Sales][Store] s JOIN [Sales][StoreContact] sc ON [s][customerid] = [sc][customerid] JOIN [Person][Contact] c ON [sc][contactid] = [c][contactid] JOIN [Person][ContactType] ct ON [sc][contacttypeid] = [ct][contacttypeid] "* -+37)350 AL Q<)3 ) +? G? + K <K 7 Table 'Contact' Scan count 0, logical reads 1586, Table 'Worktable' Scan count 0, logical reads 0, Table 'Address' Scan count 1, logical reads 216, Table 'CustomerAddress' Scan count 753, logical reads 1624, Table 'Store' Scan count 1, logical reads 103, Table 'StoreContact' Scan count 20, logical reads 42, Table 'ContactType' Scan count 1, logical reads 2, +& *3 *3)9 E** A E)35 OPTION ( LOOP JOIN ) "

107 % "*0151 ) 37L3+* +)* 8 )3 + ) )+) %$ %' 9*+*+))5 Table 'ContactType' Scan count 0, logical reads 1530, Table 'Contact' Scan count 0, logical reads 1586, Table 'StoreContact' Scan count 712, logical reads 1432, Table 'Address' Scan count 0, logical reads 1477, Table 'CustomerAddress' Scan count 701, logical reads 1512, Table 'Store' Scan count 1, logical reads 103, " * ) + ) * + *L )8 )**) 3 $$123;))35 OPTION ( MERGE JOIN ) "+ 7)* )+ ; Table 'Worktable' Scan count 11, logical reads 91, Table 'CustomerAddress' Scan count 1, logical reads 6, Table 'StoreContact' Scan count 1, logical reads 4, Table 'ContactType' Scan count 1, logical reads 2, Table 'Store' Scan count 1, logical reads 103, Table 'Address' Scan count 1, logical reads 18, Table 'Contact' Scan count 1, logical reads 33, 8L* + ) 89*+ )*7+* *040123*? ))) 35 OPTION ( HASH JOIN )

108 4567*9 %# "# 8L ) 7 9 E 7 * 3 Q<5 Table 'Worktable' Scan count 0, logical reads 0, Table 'Contact' Scan count 1, logical reads 569, Table 'Store' Scan count 1, logical reads 103, Table 'Address' Scan count 1, logical reads 216, Table 'CustomerAddress' Scan count 1, logical reads 67, Table 'StoreContact' Scan count 1, logical reads 4, Table 'ContactType' Scan count 1, logical reads 2, 7 +? 6>6E< " ) Q<)3+*))* + *L ) ) + *L ) ) * )+)L+) *) 46 ) = ) 7 ) LL * ) +* LL )*375 SELECT * FROM [Sales][SalesOrderDetail] sod JOIN [Sales][SalesOrderHeader] soh ON [sod][salesorderid] = [soh][salesorderid] "## 3)3+ )+ *)7 )%*3 5 OPTION ( FAST 10 )

109 %% "# )0 )1+=3 5 1 ) * )+ ) * * * + * )* +L))) )3)3*%#$( %/))%*0 )+%() 3%%+$)3+ )7) 3+)'4** )* **+ ) )** ))) ) )))O $( $(( FL ) 3 ) L * 3* )* "+=**1 &$ <$ = ) 1 3)* )L))1 == *+* ) 3)79335 SELECT pc[name] ProductCategoryName, psc[name] ProductSubCategoryName, p[name] ProductName, pd[description], pm[name] ProductModelName, c[name] CultureName, d[filename], pri[quantity], pr[rating], pr[comments] FROM [Production][Product] p LEFT JOIN [Production][ProductModel] pm ON [p][productmodelid] = [pm][productmodelid] LEFT JOIN [Production][ProductDocument] pdo ON p[productid] = pdo[productid] LEFT JOIN [Production][ProductSubcategory] psc ON [p][productsubcategoryid] = [psc][productsubcategoryid] LEFT JOIN [Production][ProductInventory] pri ON p[productid] = pri[productid] LEFT JOIN [Production][ProductReview] pr ON p[productid] = pr[productid] LEFT JOIN [Production][Document] d ON pdo[documentid] = d[documentid] LEFT JOIN [Production][ProductCategory] pc ON [pc][productcategoryid] = [psc][productcategoryid] LEFT JOIN [Production][ProductModelProductDescriptionCulture] pmpd ON pmpd[productmodelid] = pm[productmodelid] LEFT JOIN [Production][ProductDescription] pd ON pmpd[productdescriptionid] = pd[productdescriptionid] LEFT JOIN [Production][Culture] c ON c[cultureid] = pmpd[cultureid]

110 4567*9! * ) + L) L %%% "$ < )** )))) 44( 3&$<$35 OPTION (FORCE ORDER) *%' "% +/$,L %'R )7%(-1*7 6A6 ) 3 C)+ ) * )) =)3 %%&$& )))1 )))3 +*L) * F ) + 3 1)+ *F+:A) 7)++*F) *77*335

111 %% "& 8 7*+*/* 05 ") 9+ L * / 0 ) ) + + ) ))+ )) + L )) L* -< ) -< 3 ) * 3+ ***)K? 7,)K )*+ L ))35 sp_configure 'cost threshold for parallelism', 1 ; GO RECONFIGURE WITH OVERRIDE ; GO SELECT wo[duedate], MIN(wo[OrderQty]) MinOrderQty, MIN(wo[StockedQty]) MinStockedQty, MIN(wo[ScrappedQty]) MinScrappedQty, MAX(wo[OrderQty]) MaxOrderQty, MAX(wo[StockedQty]) MaxStockedQty, MAX(wo[ScrappedQty]) MaxScrappedQty FROM [Production][WorkOrder] wo GROUP BY wo[duedate] ORDER BY wo[duedate] GO sp_configure 'cost threshold for parallelism', 5 ; GO RECONFIGURE WITH OVERRIDE ; GO *7))+ )%& = 7) A ) &# 2% 4? #3: 7 /U0 **))+ )* 9*+***3+*8 )*)+* ))3 5

112 4567*9 %%( SELECT wo[duedate], MIN(wo[OrderQty]) MinOrderQty, MIN(wo[StockedQty]) MinStockedQty, MIN(wo[ScrappedQty]) MinScrappedQty, MAX(wo[OrderQty]) MaxOrderQty, MAX(wo[StockedQty]) MaxStockedQty, MAX(wo[ScrappedQty]) MaxScrappedQty FROM [Production][WorkOrder] wo GROUP BY wo[duedate] ORDER BY wo[duedate] OPTION ( MAXDOP 1 ) *7+++ * )/* ))05 "* -+L)7L &#2%4 * * 4? + 0 1) * ) )+)4 )4# ))7++ ) * * * 7 3)) 8 = * + L * $,+,@ $( F ) 3 * 3 ) + +* * ) 8 ) 7+ ) ) ) * ++ )+* 7 * ) 62 2G$**:A4 * ==37)+ ))*3 7)* ) +7+*+ )+ )+ ++ ** 7 :A+** 5 ' >>6<? A6 $& 25$)3+7=)

113 %%' 3+ *) :A 4+* ) +62 2G$ 8)*)35 SELECT * FROM [Person][Address] WHERE [City] = 'Newark' SELECT * FROM [Person][Address] WHERE [City] = 'London' 8L*L*))75 " )!))3+)31 7+*)%#'( 37+*** )*):A*L+5 NVARCHAR(30) = 'Newark' SELECT * FROM [Person][Address] WHERE [City] = 'London' SELECT * FROM [Person][Address] WHERE [City] 8L7) 35

114 4567*9 %%4 "+ L7) 3* L*) F& AL) 3+*== )"*5 NVARCHAR(30) = 'London' SELECT * FROM [Person][Address] WHERE [City] = 'London' SELECT * FROM [Person][Address] WHERE [City] OPTION ( OPTIMIZE FOR = 'Newark' ) ) " LAL * ) / ) 3 LAL0*7 7)3,)3+L*))* 362 2G$* )= )*+*7 * ) + L"*L+ ))+LAL C ) 3 * ) * 62 2G$*))+ F * 3

115 %%& $( =+) + + L L3 )*3+ * ($+, F 3 ) * ) 7*3=) 3) 13 +/ :A:A03) 63:A+))+ )) +* ) 3+ ) 7 * * ) ) * ) ),)3 + L * ) 7 > ))*3+)) 3 )) $& 25$*:A4 = * 77)3 )*++L )))* 7+ +):A3+ +* >)+ ) 7* F +* L + $& 25$3))) *3 )3 ) ) 3 :A+ 1 8 *+ =) ) 3+=* * )*)35 INT = 277 SELECT [soh][salesordernumber], [soh][orderdate], [soh][subtotal], [soh][totaldue] FROM [Sales][SalesOrderHeader] soh WHERE [soh][salespersonid] = 288 SELECT [soh][salesordernumber], [soh][orderdate], [soh][subtotal], [soh][totaldue] FROM [Sales][SalesOrderHeader] soh WHERE [soh][salespersonid] )75

116 4567*9 %%$ "# 8 )*)+*)3 )) + ) ) 3 $& 25$ + L 3) 3 $& 25$ + * $& 25$)3 INT = 277 SELECT [soh][salesordernumber], [soh][orderdate], [soh][subtotal], [soh][totaldue] FROM [Sales][SalesOrderHeader] soh WHERE [soh][salespersonid] OPTION ( RECOMPILE ) = 288 SELECT [soh][salesordernumber], [soh][orderdate], [soh][subtotal], [soh][totaldue] FROM [Sales][SalesOrderHeader] soh WHERE [soh][salespersonid] OPTION ( RECOMPILE ) )*)35 " "3*2-74#074#2<7 )S) ) 3+)4S *35) 7*** )*

117 %% ($&' % *****75 & -* =?-@= *4 $ -* 1/A<! $-6 +**+L )+ ** )4653 *L = 8 * +*3=*L K K ))** ) % L+ = *)+*+ = * * + ) )+ * *+ ))* ),/ ) ) +@$$2-$<53 C*7*):A ))3 )) =+) - *@$$53+ * ) )37 #3+) /% 3,* F*<)3LL 7 ) 3 =+ 7+ * 8 ) 7+ * ) <) *++* *3*)) +))*L +L + **3 $-3<=2$/43)7**3 )= )=7* )**)/*+3)*0 1 * * * * /260(3$-3<+7**3 +7*)**) + 9*+)7*+

118 4567*9 +))L ) C)7**-8+*35 %%# SELECT * FROM [Person][vStateProvinceCountryRegion] (*75 "$ -7*7+7) )* 3+623($-3<=2$/4++*'5 "% "**L78 <=+* 7 ) * &# 2% 4 8&!84' 1!1+)4' 4 ) 3 ) '% 7 * * '+ )) )7)=* +/('(,$% 8 ) 67:A+ )3* -$&423 )*3 B *(+$&$=*)) ) )%8 + 7* C L7 + )+7 ' % = A *

119 7 % ->):A)1 *L+) >5*) /K K0* /K K0*)1 )** B)))!>5*+* ) )*>3316))) 0 >5*)+*+ 3 1+,) *? ))/ *0!)1:A*=L )))1 +)L )) % -)13/++0*1 E) **? + ) -*5 SELECT [pm][name], [pm][catalogdescription], p[name] AS ProductName, i[diagram] FROM [Production][ProductModel] pm LEFT JOIN [Production][Product] p ON [pm][productmodelid] = [p][productmodelid] LEFT JOIN [Production][ProductModelIllustration] pmi ON [pm][productmodelid] = [pmi][productmodelid] LEFT JOIN [Production][Illustration] i ON [pmi][illustrationid] = [i][illustrationid] WHERE [pm][name] LIKE '%Mountain%' ORDER BY [pm][name] ; 8L)*75 %)1+>1+**) )1) )) 7 + **L * )

120 4567*9 %% "& H H *L 7R 7 # L '&S ) ) 3 < 1+ <$ 4 * &# 2% 4 #2# 1*A * 72# 1 * A ) #'$ 8 )**L= "A1)9? 1;8) 51 * #5 SELECT [pm][name], [pm][catalogdescription], p[name] AS ProductName, i[diagram] FROM [Production][ProductModel] pm LEFT LOOP JOIN [Production][Product] p ON [pm][productmodelid] = [p][productmodelid] LEFT JOIN [Production][ProductModelIllustration] pmi ON [pm][productmodelid] = [pmi][productmodelid] LEFT JOIN [Production][Illustration] i ON [pmi][illustrationid] = [i][illustrationid] WHERE [pm][name] LIKE '%Mountain%' ORDER BY [pm][name] ; )*7*3+*L)*5 ") +***9? +**"A -+ )1)A +*)1 -+ " A 1 ) 4&S ) +

121 % *9? )'&S-+ )%&(' )5* $$+L)*5 "* # "A? E) $&'$+))) )? E+*3+ 9? "A ) ))+*3 * Q<)3)3+ +5 Original (Hash) Table 'Illustration' Scan count 1, logical reads 273 Table 'ProductModelIllustration' Scan count 1, logical reads 183 Table 'Worktable' Scan count 0, logical reads 0 Table 'ProductModel' Scan count 1, logical reads 14 Table 'Product' Scan count 1, logical reads 15 Loop Table 'Illustration' Scan count 1, logical reads 273 Table 'ProductModelIllustration' Scan count 1, logical reads 183 Table 'Product' Scan count 1, logical reads 555 Table 'ProductModel' Scan count 1, logical reads 14 Merge Table 'Illustration' Scan count 1, logical reads 273 Table 'ProductModelIllustration' Scan count 1, logical reads 183 Table 'Product' Scan count 1, logical reads 15 Table 'ProductModel' Scan count 1, logical reads 14 *? A137 ) 91))** +A1+444*3)%4)? 91))+ +* 9 3 * *? 1 =*! 7)+ 9 1* )) )+ * +L?1*) + *L 1+ * * = +* )*=KK * 77+)) +) 7* - * = )+ ))

122 4567*9 + )) 1 ) *) L))7+**L * 7)) +)!<*:A4 #! 7:A4/260** )5 %( FROM TableName WITH (hint, hint, ) /260*3+3+ * 7+ * /260 ) 6 * L 3 /260*+ ) %$/% 8 7*)*3+)3$-3< *$-3<=2$/437** ) K7K*)* )*)7*) *)7) :A46,*77*) = 7 * ) 3 7 * 3)*)5 -" I"CAA -" I8->" " <"-I"CAAIF 6A,I"CAA -" I-,, " -> 9-!<> :C<6,I,6" 6> "C?6> I><C",-!<>)) C3$-3<)=7)7* '+ * 3 ) ) 7 B*+ '4' &!I-8=7* * * 7 ) ( 1 8 3$-3< SELECT a[city], v[stateprovincename], v[countryregionname] FROM [Person][Address] a JOIN [Person][vStateProvinceCountryRegion] v WITH ( NOEXPAND ) ON [a][stateprovinceid] = [v][stateprovinceid] WHERE [a][addressid] = ; Now, instead of a 3- table join, we get the following execution plan:

123 %' ",%/89 "*+ * 7 ) *+ *L )+*)#4&4$ %(+ *)7 * 7 7+*7+)+ )75 FROM TableName WITH (INDEX(0)) 9*+ ) 7 *7 /7** 05 FROM TableName WITH (INDEX ([IndexName])) F7) + )7 * AL3,"+6"5 SELECT [de][name], [e][title], [c][lastname] + ', ' + [c][firstname] FROM [HumanResources][Department] de JOIN [HumanResources][EmployeeDepartmentHistory] edh ON [de][departmentid] = [edh][departmentid] JOIN [HumanResources][Employee] e ON [edh][employeeid] = [e][employeeid] JOIN [Person][Contact] c ON [e][contactid] = [c][contactid] WHERE [de][name] LIKE 'P%' 875

124 4567*9 %4 "+ 8) 7 71 " A*L* ))* 70 8< L 8 23<$-+)*5 SELECT [de][name], [e][title], [c][lastname] + ', ' + [c][firstname] FROM [HumanResources][Department] de WITH ( INDEX ( PK_Department_DepartmentID ) ) JOIN [HumanResources][EmployeeDepartmentHistory] edh ON [de][departmentid] = [edh][departmentid] JOIN [HumanResources][Employee] e ON [edh][employeeid] = [e][employeeid] JOIN [Person][Contact] c ON [e][contactid] = [c][contactid] WHERE [de][name] LIKE 'P%' )*75 "$ 8&#2%4 )2% * $(#&'( $(#(#8 7)+)) )**+ 9*+ 7 L ) ) 3 * 3*))),(($* E /)= * ) * ) ) 3

125 %& / 3-3 +? ) 46246/ ) ):A"+*L7)*3 ) 5 SELECT [pm][name] AS ProductModelName, [p][name] AS ProductName, SUM([pin][Quantity]) FROM [Production][ProductModel] pm JOIN [Production][Product] p ON [pm][productmodelid] = [p][productmodelid] JOIN [Production][ProductInventory] pin ON [p][productid] = [pin][productid] GROUP BY [pm][name], [p][name] ; 75 "$# -+2%4 # ) ) 1 &# 2% 4 ) +0 ) 2' &#2% ) 4!!! )**) 3 )) )3* + 5 SELECT [pm][name] AS ProductModelName, [p][name] AS ProductName, SUM([pin][Quantity]) FROM [Production][ProductModel] pm JOIN [Production][Product] p WITH ( FASTFIRSTROW ) ON [pm][productmodelid] = [p][productmodelid] JOIN [Production][ProductInventory] pin ON [p][productid] = [pin][productid] GROUP BY [pm][name], [p][name] )*75

126 4567*9 %$ "$! = )) ) # )+ L * )) 35*)*) ) # )+ * 1+)*7 +* 1 1 )%%&$3%%## <++*)** 3+) # ) * + 3 4' % # * + L 3) Q< +*)W%(W+** ) 8 <=)+ )3 +E:* > - ) <= *L) 1) *-? ) + ) =! 3) ) *3**3 R ) + )

127 % ( A '($( $(,$% "? * :A + * * 9*+ *7* + 3 ) + +* 9*+ * / )0 )) ))))+ * 7 + ) + * / 0 / 0 ) )) * ) ** ++)+* ))7 - * 7 + ) A))) 7 * * ) )+ 7+*+)+ $6&0 3$-6 + ) -8 L ** 3 ) * * * )) 7 DECLARE CurrencyList CURSOR FOR SELECT CurrencyCode FROM [Sales][Currency] WHERE Name LIKE '%Dollar%' OPEN CurrencyList FETCH NEXT FROM CurrencyList = 0 BEGIN -- Normally there would be operations here using data from cursor FETCH NEXT FROM CurrencyList END CLOSE CurrencyList DEALLOCATE CurrencyList GO + *%5

128 &5< %# "# $" C K, 6 67 K 7) 3)7) 7+*5 " 8L * ) ) 5 DECLARE CurrencyList CURSOR FOR SELECT CurrencyCode FROM [Sales][Currency] WHERE Name LIKE '%Dollar%'

129 %( "$ )* )*) ++*L 7 +* 7 4#8& "% 7 %' * )* +*3)) 3+ )3 )**/ 4+ *0

130 &5< %(% "& 8 *+*7+ 7 * * )/4+ *0 L)K* K ") -)+*)? : *) + 7 +*69 )*+))/*LL)05

131 %( "* +)<+*)*, <, ) ) )R + ) +* ++* ))++*))+ ))5 " C,?A3 )+**):A)+ :A) = && # 7))7+)+) * && # + )? )L* ::(*)$3&4+ $6&0&45 OPEN CurrencyList FETCH NEXT FROM CurrencyList

132 &5< %(( "+ :'*7*:A$6&0&4* +*+)/025$+&3< WHILE = 0 BEGIN --Normally there would be operations here using data from cursor FETCH NEXT FROM CurrencyList END "# +:4:&+) CLOSE CurrencyList DEALLOCATE CurrencyList "##

133 %('!$" 8 *7+K,67K+ 7L *)*5 "# )) + ) * ) / * ) *+))+ )*L0 < + +$6&0&4+ :A * )+$6&03 4$5$&6 9)+ 7 * L)7+* +)))$6&0 3++*+75 "#$ + $" ) )) 8L R 7*L, C,+ +)+ * L ))+)*5 DECLARE CurrencyList CURSOR STATIC FOR 5! # "*7F7%' * ) ) 3+* ) 31,3 "#%

134 &5< + + * 2%4 ) 4#8&, ) 4! L 4!A**J 7+** 3 %(4 "#& 3* ))+:A 7*3++) ) +* K%K) > L * )) +L* ) K7:7 * )*7 "#) +**)+ 7 :

135 %(& #? : K * ) * K*+)+* * /70 : *) 7 7" + + : ) 3+ ) : + L)++ *L 4 +*4+ +*7 + * ) * * ) 7*** ) # )*73-67+** ) 3 * + 7*) * ) : *+ * * /025$ "## 7 ) * " **$3&4*8 7*) )

136 &5< %($ "# ): + ) 7+ $6&0 +*+ KK * ) + + D)))*+ *)) )) 7+)))*7 AL)5 DECLARE CurrencyList CURSOR KEYSET FOR 5! # 7*%#5 "#$

137 %( "**L** +L=*) R)))) ) + : + 7 )) )* *D+ + 1 )) * : *+ ))* *) 5 " ):)+*)) )7:++1+ " A 4#8& * D * & 4 ) ) )&#2%+ +)1))"A )** # 8 7)+**%5 "# $3&4+7 : + $6&0 3$-6 $6&0 &4))*8 +L) ))3*+ *, )* * +

138 &5< (B$%: 6 ) + 7) + * * )*)$<735K,667 K5 %(# DECLARE CurrencyList CURSOR READ_ONLY FOR 8) + *** *5 " + ) *L 7 C) )+ )* * ) + 7 & 37**) Q<+ )7+3 * 57 7L* * *3*L )3))3 )) )*7***))+** )+ ) * ) ) ) 3+* ) +++ *) 9L3++*5 TABLE ( [DateOrderNumber] INT IDENTITY(1, 1),[Name] VARCHAR(50),[OrderDate] DATETIME,[TotalDue] MONEY,[SaleType] VARCHAR(50) ) INT,@TotalDue Money INSERT ( [Name],[OrderDate],[TotalDue] )

139 %' SELECT s[name],soh[orderdate],soh[totaldue] FROM [Sales][SalesOrderHeader] AS soh JOIN [Sales][Store] AS s ON soh[customerid] = s[customerid] WHERE soh[customerid] = 17 ORDER BY soh[orderdate] DECLARE ChangeData CURSOR FOR SELECT [DateOrderNumber],[TotalDue] OPEN ChangeData FETCH NEXT FROM = 0 BEGIN -- Normally there would be operations here using data from cursor < 1000 SET SaleType = 'Poor' WHERE [DateOrderNumber] ELSE > 1000 < SET SaleType = 'OK' WHERE [DateOrderNumber] ELSE > < SET SaleType = 'Good' WHERE [DateOrderNumber] ELSE SET SaleType = 'Great' WHERE [DateOrderNumber] FETCH NEXT FROM END CLOSE ChangeData DEALLOCATE ChangeData SELECT * 8 L*3+L )35 Number Name OrderDate TotalDue SaleType 1 Trusted Catalog Store Good 2 Trusted Catalog Store Good 3 Trusted Catalog Store Great 4 Trusted Catalog Store Great 5 Trusted Catalog Store OK 6 Trusted Catalog Store OK 7 Trusted Catalog Store OK 8 Trusted Catalog Store OK 9 Trusted Catalog Store OK 10 Trusted Catalog Store Good 11 Trusted Catalog Store OK 12 Trusted Catalog Store Poor 6 67 / * 0 ) + +*)7) #80 4#84+

140 &5< + ) + ) ) $)) %%( AL )-67+**)) )5 %'% "$ 8)) &S) )+*))))+) )%* )+**)+*4S) ) ) +* * 7 ) $6&0&4 )) *) +*L )5 DECLARE ChangeData CURSOR STATIC "*+*)+L7 )- 675 "% " * ) %S ) ) + *L + * ) 9*37

141 %' '&*37$4) AL*D)5 DECLARE ChangeData CURSOR KEYSET )* )-67 "& -+)%S+ - + ) /(0 * ) AL5 DECLARE ChangeData CURSOR READ_ONLY "*+ )*&5 ") 9+$6&0))%S)+ ) + ' )+)AL )*L )5 DECLARE ChangeData CURSOR FAST_FORWARD

142 &5< /<735 )*+ $<735 * & +/<735$< /< +* ) ) 9*+ + L AL)* /<7355 %'( DECLARE ChangeData CURSOR FORWARD_ONLY KEYSET 7+)L+ ))+D6F6 3* 9*+*)*;8* 5 SELECT ROW_NUMBER() OVER(ORDER BY soh[orderdate]),s[name],soh[orderdate],soh[totaldue],case WHEN soh[totaldue] < 1000 THEN 'Poor' HEN soh[totaldue] BETWEEN 1000 AND THEN 'OK' WHEN soh[totaldue] BETWEEN AND THEN 'Good' ELSE 'Great' END AS [SaleType] FROM [Sales][SalesOrderHeader] AS soh JOIN [Sales][Store] AS s ON soh[customerid] = s[customerid] WHERE soh[customerid] = 17 ORDER BY soh[orderdate] 3 7! ) )) ) 4#0 ' * * 7+*L+ ) *) ))7+* 75 "* * )3* 4'S ) L + * * 6*3) 7*) ) * ) * * ) ) 9*+*+* )) *) = 1 ) * ) L ))*+***%+*)%+

143 ! %'' * 1*) *? )+ ) ):A:A 1* 9*+* ) * ) + * L 7++*,L)* ** + )+** + ) )) *)) 7 < + + ))) 5 **)1 )**

144 &5< %'4

145 %'& ( C /+,% /',$% % $( /+ 8 ):A4+@? A )+)@? A+*+7 F *@?A*:A) 5 A@? A@?A+* )@:3@? A7 :@? A@: A <6"@? A@?A *3)++ )3 8*)@? A+<>@?A6) )<>@? A3)):A*))7 +*))) :A <6"@? A * * A 8* 7 7 ) <6"@? A 3+ * )+7 )*77*3 A ) ) ** +* C * ) :A + +*)) :A)) ) :A + *@?A 3 ) )*)3@?A)+- 5 F<>@? A))*)5 6P@?A/5 A0 /P)*@?A+* L:MN) $-52&26P*7))@? A+ 3) 60P-$-52&26)+ )@?A 6)3))):A )3)))* 8 * 7 * *

146 ) 7+ 3 ) 3) ) A 3)*+*L- 569L 35 %'$ SELECT c[firstname], c[lastname], c[ address], c[phone], e[employeeid], e[gender], a[addressline1], a[addressline2], a[city], a[stateprovinceid], a[postalcode] FROM [Person][Contact] c INNER JOIN [HumanResources][Employee] e ON c[contactid] = e[contactid] INNER JOIN [HumanResources][EmployeeAddress] ea ON e[employeeid] = ea[employeeid] INNER JOIN [Person][Address] a ON ea[addressid] = a[addressid] FOR XML AUTO 7*%5 "# )) *7)KK3 L ):A6A6+*-54$5$&6 <*L3 AL+*+3 *-C<5 SELECT sname AS StoreName, ccontactid, ccontacttypeid FROM SalesStore s JOIN [Sales][StoreContact] c ON s[customerid] = c[customerid] ORDER BY s[name] FOR XML AUTO

147 %' " A)*5 <s StoreName="A Bicycle Association"> <c ContactID="956" ContactTypeID="11" /> </s> <s StoreName="A Bike Store"> <c ContactID="322" ContactTypeID="11" /> </s> ++)*- 5/ 3P)7+) 3 )@? A) $-52&26+ * 3 * )@? A+) SELECT 1 AS Tag, NULL AS Parent, sname AS [Store!1!StoreName], NULL AS [Contact!2!ContactID], NULL AS [Contact!2!ContactTypeID] FROM SalesStore s JOIN [Sales][StoreContact] c ON s[customerid] = c[customerid] UNION ALL SELECT 2 AS Tag, 1 AS Parent, sname AS StoreName, ccontactid, ccontacttypeid FROM SalesStore s JOIN [Sales][StoreContact] c ON s[customerid] = c[customerid] ORDER BY [Store!1!StoreName], [Contact!2!ContactID] FOR XML EXPLICIT 7)3*7*(5

148 %'# "$ A++ )*5 <Store StoreName="A Bicycle Association"> <Contact ContactID="956" ContactTypeID="11" /> </Store> <Store StoreName="A Bike Store"> <Contact ContactID="322" ContactTypeID="11" /> </Store> )- 5$-52&26L+ *+ ) ) )) L ) 6* 7+ *+ ) 3 +*@? A+) +C" <"7+ ) )33-7)- 56*)6$ ) )* * :A45 SELECT s[name] AS StoreName, ( SELECT ccontactid, ccontacttypeid FROM [Sales][StoreContact] c WHERE c[customerid] = s[customerid] FOR XML AUTO, TYPE, ELEMENTS ) FROM [Sales][Store] s ORDER BY s[name] FOR XML AUTO, TYPE $5$ $364)* *+))@? A5

149 %4 <s StoreName="A Bicycle Association"> <c> <ContactID>956</ContactID> <ContactTypeID>11</ContactTypeID> </c> </s> 7))+*'5 "% @ "& $%A**K+*)*)4& 5 & 2<& 62<1*) 74 7C,@* 1

150 * 3 + )@?A)/0+)) ))@? A @?A 7 C + 3*L * **5 %4% SELECT s[name] AS "@StoreName", c[contactid] AS "StoreContact/@ContactId", c[contacttypeid] AS "StoreContact/@ContactTypeID" FROM [Sales][Store] s JOIN [Sales][StoreContact] c ON s[customerid] = c[customerid] ORDER BY s[name] FOR XML PATH 7*+* /%0@? A)*5 <row StoreName="A Bicycle Association"> <StoreContact ContactId="956" ContactTypeID="11" /> </row> <row StoreName="A Bike Store"> <StoreContact ContactId="322" ContactTypeID="11" /> </row> $%/+ <))@? A+ 7*)*:A * 7 ) )@? A )+@? A)3)* + )*@:@8 + + >+)@?A-9@?A-C< * 5 Table 'StoreContact' Scan count 1, logical reads 7, Table 'Store' Scan count 1, logical reads 103, A 6@A ) * * 5 Table 'Worktable' Scan count 0, logical reads 0, Table 'StoreContact' Scan count 2, logical reads 8, Table 'Store' Scan count 2, logical reads A-C<*F6*)C,@+ )5 Table 'StoreContact' Scan count 701, logical reads 1410, Table 'Store' Scan count 1, logical reads )* **:A L

151 %4 A) *= <ROOT> <Currency CurrencyCode="UTE" CurrencyName="Universal Transactional Exchange"> <CurrencyRate FromCurrencyCode="USD" ToCurrencyCode="UTE" CurrencyRateDate="1/1/2007" AverageRate="553" EndOfDateRate= "558" /> <CurrencyRate FromCurrencyCode="USD" ToCurrencyCode="UTE" CurrencyRateDate="6/1/2007" AverageRate="928" EndOfDateRate= "1057" /> </Currency> </ROOT> +*L*+C67+* *C687)C6C,8L + + +)@?A9L5 BEGIN TRAN AS INTEGER AS NVARCHAR(MAX) = '<ROOT> <Currency CurrencyCode="UTE" CurrencyName="Universal Transactional Exchange"> <CurrencyRate FromCurrencyCode="USD" ToCurrencyCode="UTE" CurrencyRateDate="1/1/2007" AverageRate="553" EndOfDayRate= "558" /> <CurrencyRate FromCurrencyCode="USD" ToCurrencyCode="UTE" CurrencyRateDate="6/1/2007" AverageRate="928" EndOfDayRate= "1057" /> </Currency> </ROOT>' EXEC INSERT INTO [Sales][Currency] ( [CurrencyCode], [Name], [ModifiedDate] ) SELECT CurrencyCode, CurrencyName, GETDATE() FROM OPENXML (@idoc, 'ROOT/Currency',1) WITH ( CurrencyCode NCHAR(3), CurrencyName NVARCHAR(50) ) INSERT INTO [Sales][CurrencyRate] ( [CurrencyRateDate], [FromCurrencyCode], [ToCurrencyCode], [AverageRate], [EndOfDayRate], [ModifiedDate] ) SELECT CurrencyRateDate, FromCurrencyCode, ToCurrencyCode, AverageRate, EndOfDayRate, GETDATE() FROM OPENXML(@iDoc, 'ROOT/Currency/CurrencyRate',2) WITH ( CurrencyRateDate DATETIME '@CurrencyRateDate', FromCurrencyCode NCHAR(3) '@FromCurrencyCode', ToCurrencyCode NCHAR(3) '@ToCurrencyCode', AverageRate MONEY '@AverageRate', EndOfDayRate MONEY '@EndOfDayRate' ) EXEC ROLLBACK TRAN 3+ * * 7 + ) 234$6 ) 234$6& +*&5

152 %4( ") - 3 ) A - <6"@?A *4 ),AA* :A )* )3> )3)+3 67)7@? A A 7%#7 7%#7 ' ) :A+$ "* **)@? A<A) 4 9++*<6"@? A) + )@?A " + L ))* 3* )) 7) $6& 5 "+ 3)+ )73) ) ) * & & F + A > ) **) +*? E + )

153 /! %4' & & 6& &)? E+ <= A )*3+) * - + <6"@? A ) ) A *+ *)7+ ) 1 4$5$&6 *<6"@?A )+) *1 *3 < * ) A++@? A * ) * - * :A 4+ )@: ) 3@? A 6))+ :A 8 *3)@?A+) + )*)7))*5 8( '#( (+5)7* 8%(+537!* +1$-2464*:A 8(+5)+@? A )3@? A+)A8 <>/)++ *+ 0*3+))7 L 1*73* )77 L) * 3)* ' 8% 3)3***@? A )* 7+*L 3 ) ) *)*5 SELECT c[lastname], c[firstname], e[hiredate], e[title] FROM [Person][Contact] c INNER JOIN [HumanResources][Employee] e ON c[contactid] = e[contactid] INNER JOIN [HumanResources][JobCandidate] jc ON e[employeeid] = jc[employeeid] AND jc[resume]exist(' declare namespace res=" /res:resume/res:employment/res:empjobtitle[contains(,"sales Manager")]') = 1

154 %44 3++)**)+ 7%5 "# + +* 7 -&# 2%4 1& )* # >)-351 ))1& *)$# +) *** ) * + 6# =# B K@? A>*@)K )@: )>)<6"@?A39*+ B+>7 +)3 ) B*)**)5 "#%# * # 3 * ) 3 *) 3 5 L7+)& *)

155 %4& ' 2! 7+*L3,) )= A + + A8 <> 7/+ A+ 8 +< +>07)@:+ :A5 P@? A7 ) ' * Q6 /P/7 *1 /0$$:A P+1<$:A/0 P) +)4$5$&6 :A7)@?A)) #7+*:A4 7+*) )**)) 3)) +*@?A )@?A+*L 8AL7375 SELECT sdemographicsquery(' declare namespace ss=" for $s in /ss:storesurvey where ss:storesurvey/ss:squarefeet > return $s ') AS Demographics FROM [Sales][Store] s WHERE s[salespersonid] = 279 "# 3)*3 -:A34 ** 4#2E,OP+ -33*L3) * *++ ***3 -*+)%)&#2% 4 4# +) 4#2) ))35+)1) ))1+*))5&#2%4 + L@?A7

156 %4$ "#$! F%(7-5747<! + *) 7 &2))!*+&#2%4 &2+ 4)) A)E +)1)@? A ) <-)@? A * *)4 <))@:) )*:A )@? A 7* * *) + ) ) ) 7L )*L *@: )@?A F ):A@: **7L )* )) + * )) )? ) /5QQ)Q Q Q('4%70 ) <>@?A+ )

157 %4 ) <6"@?A * *L *<6"@?A *:A* )*) *@? A*:A- *+* L )

158 %4#

159 %& ( D 3% $, + * * 7 + * 7)73+ ):A 1++*+7+ )+*****7 *7* )+**)) )7+)*5 A7* P**+*3 7* =P* 7= )C 1 )++:A CP73* R ** CP7+)* ) = ( *L*7 ) + ) + L =)1+) 7++*L** * 9*+)=+*)*7 + L ) ) ** =+*+*+ AL * L 7 / L 0)*+ * )) * + ) )) 3+ 7 )) 3 +)) DROP PROCEDURE [Sales][uspGetDiscountRates] ; GO CREATE PROCEDURE [Sales][uspGetDiscountRates] INT,@SpecialOfferId INT ) AS BEGIN TRY -- determine if sale using special offer exists IF EXISTS ( SELECT * FROM [Sales][Individual] i INNER JOIN [Sales][Customer] c ON icustomerid = ccustomerid INNER JOIN [Sales][SalesOrderHeader] soh ON sohcustomerid = ccustomerid INNER JOIN [Sales][SalesOrderDetail] sod

160 5- ON soh[salesorderid] = sod[salesorderid] INNER JOIN [Sales][SpecialOffer] spo ON sod[specialofferid] = spo[specialofferid] WHERE i[contactid] AND spo[specialofferid] ) BEGIN SELECT c[lastname] + ', ' + c[firstname],c[ address],i[demographics],spo[description],spo[discountpct],sod[linetotal],p[name],p[listprice],sod[unitpricediscount] FROM [Person][Contact] c INNER JOIN [Sales][Individual] i ON c[contactid] = i[contactid] INNER JOIN [Sales][Customer] cu ON i[customerid] = cu[customerid] INNER JOIN [Sales][SalesOrderHeader] soh ON cu[customerid] = soh[customerid] INNER JOIN [Sales][SalesOrderDetail] sod ON soh[salesorderid] = sod[salesorderid] INNER JOIN [Sales][SpecialOffer] spo ON sod[specialofferid] = spo[specialofferid] INNER JOIN [Production][Product] p ON sod[productid] = p[productid] WHERE ccontactid AND sod[specialofferid] END -- use different query to return other data set ELSE BEGIN SELECT c[lastname] + ', ' + c[firstname],c[ address],i[demographics],sohsalesordernumber,sod[linetotal],p[name],p[listprice],sod[unitprice],st[name] AS StoreName,ec[LastName] + ', ' + ec[firstname] AS SalesPersonName FROM [Person][Contact] c INNER JOIN [Sales][Individual] i ON c[contactid] = i[contactid] INNER JOIN [Sales][SalesOrderHeader] soh ON i[customerid] = soh[customerid] INNER JOIN [Sales][SalesOrderDetail] sod ON soh[salesorderid] = sod[salesorderid] INNER JOIN [Production][Product] p ON sod[productid] = p[productid] LEFT JOIN [Sales][SalesPerson] sp ON sohsalespersonid = spsalespersonid LEFT JOIN [Sales][Store] st ON spsalespersonid = stsalespersonid LEFT JOIN [HumanResources][Employee] e ON spsalespersonid = e[employeeid] LEFT JOIN Person[Contacct] ec ON e[contactid] = ec[contactid] WHERE i[contactid] END --second result SET = 16 BEGIN SELECT p[name],p[productline] FROM [Sales][SpecialOfferProduct] sop INNER JOIN [Production][Product] p ON sop[productid] = p[productid] WHERE sop[specialofferid] = 16; END %&%

161 ; %& END TRY BEGIN CATCH SELECT ERROR_NUMBER() AS ErrorNumber,ERROR_MESSAGE() AS ErrorMessage ; RETURN ERROR_NUMBER() ; END CATCH RETURN 0 ; ) * ) 3 ) 3 +!) + 3) ))3 C)+,!- 8L7*)*5 EXEC = 12298, -- = int )5 "#& )+ * *+) 3 ) )) 3 7 *5

162 5- %&( " < + * * 9*+ ) *+)3)) ) 3 ) 7 ) )) + ) 3*))+ ) 3+**4 #2<V %& 8 7+*L * L1)) = )* ** F*** *)3**+*L7+ L+*(5 "$ **+ * ) 7D +** FLK*K+*'5

163 %&' "% -* +L *+7 )**7)+** L -)+*1)) 3 /&S0+ *+ &# 2% 4 4#82'# " ) 7 7 & 2< +* =)3-7))3 8 7! *+ + )) 7 ) + *@?A*L7 ) L1*7+ * +)7))) R1),L E +)**+ **+) :A ) ) * L ) )+ *+ 5 K %<!##K+* ):A **73!)KK+* K&6###K+*)++ *:A )*3*)K4K + :A * ) 3 / 0!)+* )*7+)K%K )*

Reading Execution Plans: The Deep Dive for SQL Saturday #117

Reading Execution Plans: The Deep Dive for SQL Saturday #117 Reading Execution Plans: The Deep Dive for SQL Saturday #117 Grant Fritchey www.scarydba.com Grant Fritchey Product Evangelist for Red Gate Software Twitter: @gfritchey Blog: scarydba.com Email: grant@scarydba.com

More information

--NOTE: We are now using a different database. USE AdventureWorks2012;

--NOTE: We are now using a different database. USE AdventureWorks2012; --* BUSIT 103 Assignment #6 DUE DATE : Consult course calendar /* Name: Christopher Singleton Class: BUSIT103 - Online Instructor: Art Lovestedt Date: 11/01/2014 */ --You are to develop SQL statements

More information

Introduction to SQL Server 2005/2008 and Transact SQL

Introduction to SQL Server 2005/2008 and Transact SQL Introduction to SQL Server 2005/2008 and Transact SQL Week 3: The ins and outs of JOINS Can you relate? Steve Stedman - Instructor Steve@SteveStedman.com This Weeks Overview Introduction to SQL Server

More information

Exam Actual. Higher Quality. Better Service! QUESTION & ANSWER

Exam Actual. Higher Quality. Better Service! QUESTION & ANSWER Higher Quality Better Service! Exam Actual QUESTION & ANSWER Accurate study guides, High passing rate! Exam Actual provides update free of charge in one year! http://www.examactual.com Exam : 70-444GB2312

More information

Database: [AdventureWorks]... 4 Schema: [dbo]... 4 Schema: [HumanResources]... 4 Schema: [Person]... 4 Schema: [Production]...

Database: [AdventureWorks]... 4 Schema: [dbo]... 4 Schema: [HumanResources]... 4 Schema: [Person]... 4 Schema: [Production]... Database: [AdventureWorks]... 4 Schema: [dbo]... 4 Schema: [HumanResources]... 4 Schema: [Person]... 4 Schema: [Production]... 4 Schema: [Purchasing]... 4 Schema: [Sales]... 4 Table: [dbo].[awbuildversion]...

More information

1.2 - The Effect of Indexes on Queries and Insertions

1.2 - The Effect of Indexes on Queries and Insertions Department of Computer Science and Engineering Data Administration in Information Systems Lab 2 In this lab class, we will approach the following topics: 1. Important Concepts 1. Clustered and Non-Clustered

More information

1.1 - Basics of Query Processing in SQL Server

1.1 - Basics of Query Processing in SQL Server Department of Computer Science and Engineering 2013/2014 Database Administration and Tuning Lab 3 2nd semester In this lab class, we will address query processing. For students with a particular interest

More information

Module 4: Creating and Tuning Indexes

Module 4: Creating and Tuning Indexes Module 4: Creating and Tuning Indexes Overview Planning Indexes Creating Indexes Optimizing Indexes 1 Lesson 1: Planning Indexes How SQL Server Accesses Data What Is a Clustered Index? What Is a Heap?

More information

Extend your queries with APPLY

Extend your queries with APPLY Extend your queries with APPLY SQL Server offers a way to combine tables with table-valued functions, and to create the equivalent of correlated derived tables. Tamar E. Granor, Ph.D. As I ve been digging

More information

Fair Comments. Published Thursday, November 10, :23 PM

Fair Comments. Published Thursday, November 10, :23 PM Fair Comments Published Thursday, November 10, 2011 4:23 PM To what extent is good code self-documenting? In one of the most entertaining sessions I saw at the recent PASS summit, Jeremiah Peschka (blog

More information

/* Enabling Actual Execution Plan and Client Statistics Options, we can see that the performance and plans are the equivalent */

/* Enabling Actual Execution Plan and Client Statistics Options, we can see that the performance and plans are the equivalent */ --turn on Include Actual Execution Plan CTRL+M SET STATISTICS IO ON SET STATISTICS PROFILE ON SET STATISTICS TIME ON ----------------------------------------------------------------- -------------------------------------------------------

More information

Query Optimizer. Chapter 19. In This Chapter. c Tools for Editing the Optimizer Strategy. c Phases of Query Processing c How Query Optimization Works

Query Optimizer. Chapter 19. In This Chapter. c Tools for Editing the Optimizer Strategy. c Phases of Query Processing c How Query Optimization Works Chapter 19 Query Optimizer In This Chapter c Phases of Query Processing c How Query Optimization Works c Tools for Editing the Optimizer Strategy c Optimization Hints 508 Microsoft SQL Server 2012: A Beginner

More information

Design Business Use Cases for a Data Mining Course Based on an Enterprise Level Database

Design Business Use Cases for a Data Mining Course Based on an Enterprise Level Database Design Business Use Cases for a Data Mining Course Based on an Enterprise Level Database Ying Xie, Young-Seok Kim CSIS Department, Kennesaw State University, Kennesaw, GA, USA yxie2@kennesaw.edu, ykim35@students.kennesaw.edu

More information

Judge, Jury, and Executioner. What happens when you run a query and how to read all about it.

Judge, Jury, and Executioner. What happens when you run a query and how to read all about it. Judge, Jury, and Executioner What happens when you run a query and how to read all about it. Por favor silenciar los Teléfonos Celulares What s the big deal? Talk about how a query is processed and gets

More information

Database Administration and Tuning

Database Administration and Tuning Department of Computer Science and Engineering 2012/2013 Database Administration and Tuning Lab 7 2nd semester In this lab class we will approach the following topics: 1. Schema Tuning 1. Denormalization

More information

Dataedo. AdventureWorks. Data Dictionary Generated with

Dataedo. AdventureWorks. Data Dictionary Generated with Dataedo AdventureWorks Data Dictionary 2017-05-30 Generated with of Contents 1. AdventureWorks... 7 2. Business Entities... 8 3. People... 9 3.1. s... 9 3.1.1. : Person.Address... 9 3.1.2. : Person.AddressType...10

More information

Query Processing Task

Query Processing Task Execution Plans Indexing to achieve optimal query plans Author: Ted Krueger - LessThanDot The obstacle SQL Server and data storage itself is only as good as the means in which we can read the data. Very

More information

Introduction to SQL Server 2005/2008 and Transact SQL

Introduction to SQL Server 2005/2008 and Transact SQL Introduction to SQL Server 2005/2008 and Transact SQL Week 2 TRANSACT SQL CRUD Create, Read, Update, and Delete Steve Stedman - Instructor Steve@SteveStedman.com Homework Review Review of homework from

More information

Required Materials - SQL Server Management Studio - Your copy of the Northwind database

Required Materials - SQL Server Management Studio - Your copy of the Northwind database Analyzing and Optimizing Queries Objective After completing this lab, you will be able to: Analyze the performance of queries using text-based and graphical analysis tools. Perform some common optimizations

More information

Overview. Implementing Stored Procedures Creating Parameterized Stored Procedures Working With Execution Plans Handling Errors

Overview. Implementing Stored Procedures Creating Parameterized Stored Procedures Working With Execution Plans Handling Errors إعداد د. عبدالسالم منصور الشريف 1 Overview Implementing Stored Procedures Creating Parameterized Stored Procedures Working With Execution Plans Handling Errors 2 1 Lesson 1: Implementing Stored Procedures

More information

SQL Server Execution Plans

SQL Server Execution Plans High Performance SQL Server SQL Server Execution Plans SQL Server Execution Plans Grant Fritchey Execution plans show you what s going on behind the scenes in SQL Server. They can provide you with a wealth

More information

Course Outline. SQL Server Performance & Tuning For Developers. Course Description: Pre-requisites: Course Content: Performance & Tuning.

Course Outline. SQL Server Performance & Tuning For Developers. Course Description: Pre-requisites: Course Content: Performance & Tuning. SQL Server Performance & Tuning For Developers Course Description: The objective of this course is to provide senior database analysts and developers with a good understanding of SQL Server Architecture

More information

Going OVER and Above with SQL

Going OVER and Above with SQL Going OVER and Above with SQL Tamar E. Granor Tomorrow's Solutions, LLC Voice: 215-635-1958 Email: tamar@tomorrowssolutionsllc.com The SQL 2003 standard introduced the OVER keyword that lets you apply

More information

exam.87q.

exam.87q. 70-761.exam.87q Number: 70-761 Passing Score: 800 Time Limit: 120 min 70-761 Querying Data with Transact-SQL Exam A QUESTION 1 Note: This question is part of a series of questions that present the same

More information

SQL Server 2012 Development Course

SQL Server 2012 Development Course SQL Server 2012 Development Course Exam: 1 Lecturer: Amirreza Keshavarz May 2015 1- You are a database developer and you have many years experience in database development. Now you are employed in a company

More information

Index Creation Performance during Data Loading

Index Creation Performance during Data Loading Technical Note 6: Index Creation Performance during Data Loading Technical Note 6 Index creation performance during Data Load Last Modified: October 2, 2003 Version 1 Area: Data Loading Siebel Releases:

More information

exam.75q Querying Data with Transact-SQL

exam.75q Querying Data with Transact-SQL 70-761.exam.75q Number: 70-761 Passing Score: 800 Time Limit: 120 min 70-761 Querying Data with Transact-SQL Exam A QUESTION 1 You need to create an indexed view that requires logic statements to manipulate

More information

Including missing data

Including missing data Including missing data Sometimes an outer join isn t suffi cient to ensure that all the desired combinations are shown. Tamar E. Granor, Ph.D. In my recent series on the OVER clause, I failed to mention

More information

Chris Singleton 03/12/2017 PROG 140 Transactions & Performance. Module 8 Assignment

Chris Singleton 03/12/2017 PROG 140 Transactions & Performance. Module 8 Assignment Chris Singleton 03/12/2017 PROG 140 Transactions & Performance Module 8 Assignment Turn In: For this exercise, you will submit one WORD documents (instead of a.sql file) in which you have copied and pasted

More information

Course Topics. Microsoft SQL Server. Dr. Shohreh Ajoudanian. 01 Installing MSSQL Server Data types

Course Topics. Microsoft SQL Server. Dr. Shohreh Ajoudanian. 01 Installing MSSQL Server Data types Dr. Shohreh Ajoudanian Course Topics Microsoft SQL Server 01 Installing MSSQL Server 2008 03 Creating a database 05 Querying Tables with SELECT 07 Using Set Operators 02 Data types 04 Creating a table,

More information

Administração e Optimização de BDs 2º semestre

Administração e Optimização de BDs 2º semestre DepartamentodeEngenhariaInformática 2008/2009 AdministraçãoeOptimizaçãodeBDs2ºsemestre AuladeLaboratório3 Inthislabclasswewillapproachthefollowingtopics: 1. BasicsofQueryProcessinginSQLServer2008 2. FullTableScans

More information

Database Foundations. 6-9 Joining Tables Using JOIN. Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Database Foundations. 6-9 Joining Tables Using JOIN. Copyright 2014, Oracle and/or its affiliates. All rights reserved. Database Foundations 6-9 Roadmap Introduction to Oracle Application Express Structured Query Language (SQL) Data Definition Language (DDL) Data Manipulation Language (DML Transaction Control Language (TCL)

More information

IT Certification Exams Provider! Weofferfreeupdateserviceforoneyear! h ps://www.certqueen.com

IT Certification Exams Provider! Weofferfreeupdateserviceforoneyear! h ps://www.certqueen.com IT Certification Exams Provider! Weofferfreeupdateserviceforoneyear! h ps://www.certqueen.com Exam : 70-457 Title : Transition Your MCTS on SQL Server 2008 to MCSA: SQL Server 2012, Part 1 Version : Demo

More information

PASS4TEST. IT Certification Guaranteed, The Easy Way! We offer free update service for one year

PASS4TEST. IT Certification Guaranteed, The Easy Way!  We offer free update service for one year PASS4TEST \ http://www.pass4test.com We offer free update service for one year Exam : 70-461 Title : Querying Microsoft SQL Server 2012/2014 Vendor : Microsoft Version : DEMO Get Latest & Valid 70-461

More information

KillTest. 半年免费更新服务

KillTest.   半年免费更新服务 KillTest 质量更高 服务更好 学习资料 http://www.killtest.cn 半年免费更新服务 Exam : 70-442GB2312 Title : PRO:Design & Optimize Data Access by Using MS SQL Serv 2005 Version : Demo 1 / 19 1. OrderItems OrderItems OrderID (PK,

More information

Reorganize and Rebuild Indexes in the Database

Reorganize and Rebuild Indexes in the Database Reorganize and Rebuild Indexes in the Database This topic describes how to reorganize or rebuild a fragmented index in SQL Server 2017 by using or. The SQL Server Database Engine automatically maintains

More information

Module 13: Optimizing Query Performance

Module 13: Optimizing Query Performance Module 13: Optimizing Query Performance Overview Introduction to the Query Optimizer Obtaining Execution Plan Information Using an Index to Cover a Query Indexing Strategies Overriding the Query Optimizer

More information

General Concepts SQL SERVER REDUCING INDEX FRAGMENTATION

General Concepts SQL SERVER REDUCING INDEX FRAGMENTATION Article: Reducing Index Fragmentation Date: 08/03/2012 Posted by: HeelpBook Staff Source: Link Permalink: Link SQL SERVER REDUCING INDEX FRAGMENTATION General Concepts When you perform any data modification

More information

Before working on this problem set, be sure to watch these videos on the Aunt Kathi Coder Girl page:

Before working on this problem set, be sure to watch these videos on the Aunt Kathi Coder Girl page: Code Girl T-SQL Problem Set 2 - Solution Most of the time you not need all of the rows returned from a table. You will frequently need to filter the rows with a WHERE clause. The WHERE clause contains

More information

Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved.

Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables Copyright 2004, Oracle. All rights reserved. Objectives After completing this lesson, you should be able to do the following: Write SELECT statements to access data

More information

Sql Server Syllabus. Overview

Sql Server Syllabus. Overview Sql Server Syllabus Overview This SQL Server training teaches developers all the Transact-SQL skills they need to create database objects like Tables, Views, Stored procedures & Functions and triggers

More information

TABLENAME: OFFENDER. OffenderID (PK) (FK to ContactID on Contact Table) FullName

TABLENAME: OFFENDER. OffenderID (PK) (FK to ContactID on Contact Table) FullName I need to craft a SQL Query that combines various fields from the CONTACT, DOCKET, VICTIM, and OFFENDER Tables shown below, as well as shown on the Source Tables Sheet, into a kind of Super Table named

More information

Appendix A. Using DML to Modify Data. Contents: Lesson 1: Adding Data to Tables A-3. Lesson 2: Modifying and Removing Data A-8

Appendix A. Using DML to Modify Data. Contents: Lesson 1: Adding Data to Tables A-3. Lesson 2: Modifying and Removing Data A-8 A-1 Appendix A Using DML to Modify Data Contents: Lesson 1: Adding Data to Tables A-3 Lesson 2: Modifying and Removing Data A-8 Lesson 3: Generating Numbers A-15 A-2 Using DML to Modify Data Module Overview

More information

Assignment Grading Rubric

Assignment Grading Rubric Unit 5 Assignment Unit Outcomes Create result set based on queries using Inner joins. Create result set based on queries using Outer joins. Course Outcomes IT350-3: Create SELECT statements that join multiple

More information

Braindumps Questions. Querying Microsoft SQL Server 2012

Braindumps Questions. Querying Microsoft SQL Server 2012 Braindumps.70-461.120Questions Number: 70-461 Passing Score: 800 Time Limit: 120 min File Version: 9.0 http://www.gratisexam.com/ Querying Microsoft SQL Server 2012 Excellent Questions, I pass with 90%

More information

MySQL. Prof.Sushila Aghav

MySQL. Prof.Sushila Aghav MySQL Prof.Sushila Aghav Introduction SQL is a standard language for storing, manipulating and retrieving data in databases. SQL is a part of many relational database management systems like: MySQL, SQL

More information

Stored Procedures and Functions. Rose-Hulman Institute of Technology Curt Clifton

Stored Procedures and Functions. Rose-Hulman Institute of Technology Curt Clifton Stored Procedures and Functions Rose-Hulman Institute of Technology Curt Clifton Outline Stored Procedures or Sprocs Functions Statements Reference Defining Stored Procedures Named Collections of Transact-SQL

More information

Get Your Optimizer to Give up All Its Secrets. Brian

Get Your Optimizer to Give up All Its Secrets. Brian Get Your Optimizer to Give up All Its Secrets Brian Hansen brian@tf3604.com @tf3604 Brian Hansen brian@tf3604.com @tf3604.com children.org 20 Years working with SQL Server Development work since 7.0 Administration

More information

Get Your Optimizer to Give up All Its Secrets

Get Your Optimizer to Give up All Its Secrets Get Your Optimizer to Give up All Its Secrets Brian Hansen brian@tf3604.com @tf3604 SQL Saturday #631 Wausau, Wisconsin 16 September 2017 Brian Hansen 15+ Years working with SQL Server Development work

More information

CLARITY WRITE BACK CONFIGURATION White Paper

CLARITY WRITE BACK CONFIGURATION White Paper CLARITY WRITE BACK CONFIGURATION White Paper Clarity 7 White Paper: Write-Back Configuration Version 7.0 2nd Edition Microsoft is a registered trademark. Microsoft SQL Server, Office, Excel, Word, Internet

More information

/* --6). Name: Christopher Singleton Date: 02/12/2017 Class: PROG 140 Project: Module 04

/* --6). Name: Christopher Singleton Date: 02/12/2017 Class: PROG 140 Project: Module 04 /* --6. Name: Christopher Singleton Date: 02/12/2017 Class: PROG 140 Project: Module 04 About this Original Script: Drops Database IF EXISTS / Creates Database / Inserts three rows into three tables with

More information

Views in SQL Server 2000

Views in SQL Server 2000 Views in SQL Server 2000 By: Kristofer Gafvert Copyright 2003 Kristofer Gafvert 1 Copyright Information Copyright 2003 Kristofer Gafvert (kgafvert@ilopia.com). No part of this publication may be transmitted,

More information

Seminar 3. Stored procedures. Global variables. Dynamic Execution. The OUTPUT clause. Cursors

Seminar 3. Stored procedures. Global variables. Dynamic Execution. The OUTPUT clause. Cursors Seminar 3. Stored procedures. Global variables. Dynamic Execution. The OUTPUT clause. Cursors Transact-SQL Server Stored Procedures A stored procedure is a group of Transact-SQL statements compiled into

More information

How to use SQL to create a database

How to use SQL to create a database Chapter 17 How to use SQL to create a database How to create a database CREATE DATABASE my_guitar_shop2; How to create a database only if it does not exist CREATE DATABASE IF NOT EXISTS my_guitar_shop2;

More information

Retrieving Data from Multiple Tables

Retrieving Data from Multiple Tables Islamic University of Gaza Faculty of Engineering Computer Engineering Dept. Database Lab (ECOM 4113) Lab 5 Retrieving Data from Multiple Tables Eng. Mohammed Alokshiya November 2, 2014 An JOIN clause

More information

Index. Accent Sensitive (AS), 20 Aggregate functions, 286 Atomicity consistency isolation durability (ACID), 265

Index. Accent Sensitive (AS), 20 Aggregate functions, 286 Atomicity consistency isolation durability (ACID), 265 Index A Accent Sensitive (AS), 20 Aggregate functions, 286 Atomicity consistency isolation durability (ACID), 265 B Balanced (B)-Trees clustered index, 237 non-clustered index, 239 BULK INSERT statement

More information

Querying Microsoft SQL Server (461)

Querying Microsoft SQL Server (461) Querying Microsoft SQL Server 2012-2014 (461) Create database objects Create and alter tables using T-SQL syntax (simple statements) Create tables without using the built in tools; ALTER; DROP; ALTER COLUMN;

More information

PIVOT = Crosstabs, SQL Style

PIVOT = Crosstabs, SQL Style PIVOT = Crosstabs, SQL Style SQL Server s PIVOT keyword lets you create crosstabs Tamar E. Granor, Ph.D. A crosstab is a result table or cursor where the set of columns is based on data values in the source.

More information

Private Institute of Aga NETWORK DATABASE LECTURER NIYAZ M. SALIH

Private Institute of Aga NETWORK DATABASE LECTURER NIYAZ M. SALIH Private Institute of Aga 2018 NETWORK DATABASE LECTURER NIYAZ M. SALIH Data Definition Language (DDL): String data Types: Data Types CHAR(size) NCHAR(size) VARCHAR2(size) Description A fixed-length character

More information

Parameter Sniffing Problem with Stored Procedures. Milos Radivojevic

Parameter Sniffing Problem with Stored Procedures. Milos Radivojevic Parameter Sniffing Problem with Stored Procedures Milos Radivojevic About Me DI Milos Radivojevic, Vienna, Austria Data Platform Architect Database Developer MCTS SQL Server Development Contact: MRadivojevic@SolidQ.com

More information

Join, Sub queries and set operators

Join, Sub queries and set operators Join, Sub queries and set operators Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS Cartesian Products A Cartesian product is formed when: A join condition is omitted A join condition is invalid

More information

Introduction to Statistics in SQL Server. Andy Warren

Introduction to Statistics in SQL Server. Andy Warren Introduction to Statistics in SQL Server Andy Warren www.sqlandy.com @sqlandy www.linkedin.com/in/sqlandy Why Do We Need Statistics? We can t build a good plan to get the rows we need without having an

More information

Developing SQL Databases (beta)

Developing SQL Databases (beta) 70-762 Number: 70-762 Passing Score: 800 Time Limit: 120 min File Version: 1.0 Developing SQL Databases (beta) Exam A QUESTION 1 You develop a Microsoft SQL Server 2012 database that contains a table named

More information

Microsoft Exam Querying Microsoft SQL Server 2012 Version: 13.0 [ Total Questions: 153 ]

Microsoft Exam Querying Microsoft SQL Server 2012 Version: 13.0 [ Total Questions: 153 ] s@lm@n Microsoft Exam 70-461 Querying Microsoft SQL Server 2012 Version: 13.0 [ Total Questions: 153 ] Question No : 1 CORRECT TEXT Microsoft 70-461 : Practice Test You have a database named Sales that

More information

DECLARE @mynumber INT = 10; PRINT 'The value of @mynumber'; PRINT @mynumber; SET @mynumber = 20; PRINT 'The value of @mynumber'; PRINT @mynumber; SET @mynumber = @mynumber + 5; PRINT @mynumber; GO -------------------------------------------------

More information

It Depends. Database administration for developers

It Depends. Database administration for developers It Depends Database administration for developers About Me Email: maggiepint@gmail.com https://www.tempworks.com www.maggiepint.com Slide Share: http://www.slideshare.net/ MaggiePint Stuff your DBA Does!

More information

INTRODUCTION TO INDEXES AND SARGABILITY

INTRODUCTION TO INDEXES AND SARGABILITY INTRODUCTION TO INDEXES AND SARGABILITY Jared Karney @sqlknowitall http://www.sqlknowitall.com jared.karney@microsoft.com WHAT ARE INDEXES AND WHY DO WE CARE? Database objects we create Make queries run

More information

PassReview. PassReview - IT Certification Exams Pass Review

PassReview.  PassReview - IT Certification Exams Pass Review PassReview http://www.passreview.com PassReview - IT Certification Exams Pass Review Exam : 70-761 Title : Querying Data with Transact- SQL Vendor : Microsoft Version : DEMO Get Latest & Valid 70-761 Exam's

More information

Guide to Database Corruption

Guide to Database Corruption Guide to Database Corruption Table of Contents 1. Introduction... 4 2. Page and Object Allocation Storage Concepts... 4 2.1 Object Allocation Map... 5 2.2 Linkage... 5 3. Table Consistency DBCC Commands...

More information

SQL Commands & Mongo DB New Syllabus

SQL Commands & Mongo DB New Syllabus Chapter 15 : Computer Science Class XI ( As per CBSE Board) SQL Commands & Mongo DB New Syllabus 2018-19 SQL SQL is an acronym of Structured Query Language.It is a standard language developed and used

More information

SQL Coding Guidelines

SQL Coding Guidelines SQL Coding Guidelines 1. Always specify SET NOCOUNT ON at the top of the stored procedure, this command suppresses the result set count information thereby saving some amount of time spent by SQL Server.

More information

Note: Reference Link:

Note: Reference Link: Note: Reference Link: http://msdn.microsoft.com/en-us/library/ms174318.aspx Types Of Functions: Function Rowset Functions Aggregate Functions Ranking Functions Scalar Functions Description Return an object

More information

Technical Manual. Lesotho Health Data Warehouse. Prepared by: Gareth Daniell. Prepared for:

Technical Manual. Lesotho Health Data Warehouse. Prepared by: Gareth Daniell. Prepared for: Technical Manual Lesotho Health Data Warehouse Prepared by: Gareth Daniell Prepared for: The Lesotho Department of Health Planning and Statistics, Lesotho Bureau of Statistics and the World Bank, General

More information

MICROSOFT EXAM QUESTIONS & ANSWERS

MICROSOFT EXAM QUESTIONS & ANSWERS MICROSOFT 70-461 EXAM QUESTIONS & ANSWERS Number: 70-461 Passing Score: 700 Time Limit: 300 min File Version: 36.2 http://www.gratisexam.com/ MICROSOFT 70-461 EXAM QUESTIONS & ANSWERS Exam Name: Querying

More information

L e a r n S q l select where

L e a r n S q l select where L e a r n S q l The select statement is used to query the database and retrieve selected data that match the criteria that you specify. Here is the format of a simple select statement: select "column1"

More information

Introduction to SQL Server 2005/2008 and Transact SQL

Introduction to SQL Server 2005/2008 and Transact SQL Introduction to SQL Server 2005/2008 and Transact SQL Week 4: Normalization, Creating Tables, and Constraints Some basics of creating tables and databases Steve Stedman - Instructor Steve@SteveStedman.com

More information

-- Jerad Godsave -- November 9, CIS Assignment A8.sql

-- Jerad Godsave -- November 9, CIS Assignment A8.sql -- Jerad Godsave -- November 9, 2015 -- CIS 310-01 4158 -- Assignment A8.sql -- 1. List the products with a list price greater than the average list price of all products. -- result set: ItemID

More information

Testpassport. Банк экзамен

Testpassport. Банк экзамен Testpassport Банк экзамен самое хорошое качество самая хорошая служба Exam : 70-433 Title : TS: Microsoft SQL Server 2008, Database Development Version : DEMO 1 / 8 1.You have a user named John. He has

More information

Assignment Grading Rubric

Assignment Grading Rubric Final Project Outcomes addressed in this activity: Overview and Directions: 1. Create a new Empty Database called Final 2. CREATE TABLES The create table statements should work without errors, have the

More information

IDS Data Warehouse Project Documentation

IDS Data Warehouse Project Documentation IDS Data Warehouse Project Documentation Jim Duffy TakeNote Technologies June 2014 1 Databases IDS_DataWarehouse This database contains all of the raw data tables including lookup tables) used to store

More information

Module 8: Implementing Stored Procedures

Module 8: Implementing Stored Procedures Module 8: Implementing Stored Procedures Table of Contents Module Overview 8-1 Lesson 1: Implementing Stored Procedures 8-2 Lesson 2: Creating Parameterized Stored Procedures 8-10 Lesson 3: Working With

More information

Keys are fields in a table which participate in below activities in RDBMS systems:

Keys are fields in a table which participate in below activities in RDBMS systems: Keys are fields in a table which participate in below activities in RDBMS systems: 1. To create relationships between two tables. 2. To maintain uniqueness in a table. 3. To keep consistent and valid data

More information

Index. B Batches comparing, bit or 64-bit architecture, 5 Blocking, 42 Buff er cache, 143 Bulk changed map (BCM), 85 87

Index. B Batches comparing, bit or 64-bit architecture, 5 Blocking, 42 Buff er cache, 143 Bulk changed map (BCM), 85 87 Index A ActiveX data objects for.net, 2 ActualRowsRead attribute, 14 Allocation ring buffer, 92 93 ALTER EVENT SESSION, 150 ALTER INDEX REBUILD, 147 ALTER INDEX REORGANIZE, 147 ALTER SERVER CONFIGURATION

More information

Department of Computer Science University of Cyprus. EPL342 Databases. Lab 11 SQL-DML 4. (Stored Procedures, Cursors) Panayiotis Andreou

Department of Computer Science University of Cyprus. EPL342 Databases. Lab 11 SQL-DML 4. (Stored Procedures, Cursors) Panayiotis Andreou Department of Computer Science University of Cyprus EPL342 Databases Lab 11 SQL-DML 4 (Stored Procedures, Cursors) Panayiotis Andreou http://www.cs.ucy.ac.cy/courses/epl342 12-1 Before We Begin Start the

More information

MICROSOFT EXAM QUESTIONS & ANSWERS

MICROSOFT EXAM QUESTIONS & ANSWERS MICROSOFT 70-461 EXAM QUESTIONS & ANSWERS Number: 70-461 Passing Score: 700 Time Limit: 300 min File Version: 36.2 http://www.gratisexam.com/ MICROSOFT 70-461 EXAM QUESTIONS & ANSWERS Exam Name: Querying

More information

MICROSOFT EXAM QUESTIONS & ANSWERS

MICROSOFT EXAM QUESTIONS & ANSWERS MICROSOFT 70-457 EXAM QUESTIONS & ANSWERS Number: 70-457 Passing Score: 700 Time Limit: 300 min File Version: 61.0 http://www.gratisexam.com/ MICROSOFT 70-457 EXAM QUESTIONS & ANSWERS Exam Name: Transition

More information

Microsoft.PrepKing v MrT.146q

Microsoft.PrepKing v MrT.146q Microsoft.PrepKing.70-451.v2012-06-24.MrT.146q Passing Score: 700 Time Limit: 120 min File Version: 1.0 http://www.gratisexam.com/ Exam Name: Pro: Designing Database Solutions And Data Access Using Microsoft

More information

HOW TO CREATE AND MAINTAIN DATABASES AND TABLES. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL

HOW TO CREATE AND MAINTAIN DATABASES AND TABLES. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL HOW TO CREATE AND MAINTAIN DATABASES AND TABLES By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate

More information

/* Module 9 Subqueries

/* Module 9 Subqueries /* Module 9 Subqueries This first part of this demo uses the AdventureWorksDW2012 database which is the data warehouse that corresponds to the AdventureWorks2012 operational database. */ USE AdventureWorksDW2012;

More information

Answer: The tables being joined each have two columns with the same name and compatible data types, and you want to join on both of the columns.

Answer: The tables being joined each have two columns with the same name and compatible data types, and you want to join on both of the columns. Page 1 of 22 Item: 1 (Ref:Cert-1Z0-071.6.2.4) In which situation would you use a natural join? The tables being joined do not have primary and foreign keys defined. The tables being joined have matching

More information

SQL Best Practices. Chandra Sekhar

SQL Best Practices. Chandra Sekhar SQL Best Practices Chandra Sekhar Agenda Application failing with "String too large error" My application is not running fast enough Push your application logic to User Defined Functions Use Scalar Subquery

More information

Overview. Data Integrity. Three basic types of data integrity. Integrity implementation and enforcement. Database constraints Transaction Trigger

Overview. Data Integrity. Three basic types of data integrity. Integrity implementation and enforcement. Database constraints Transaction Trigger Data Integrity IT 4153 Advanced Database J.G. Zheng Spring 2012 Overview Three basic types of data integrity Integrity implementation and enforcement Database constraints Transaction Trigger 2 1 Data Integrity

More information

2017 Shelby Systems, Inc. Other brand and product names are trademarks or registered trademarks of the respective holders.

2017 Shelby Systems, Inc. Other brand and product names are trademarks or registered trademarks of the respective holders. 2017 Shelby Systems, Inc. Other brand and product names are trademarks or registered trademarks of the respective holders. The following topics are presented in this session: Database naming conventions

More information

Intermediate SQL: Aggregated Data, Joins and Set Operators

Intermediate SQL: Aggregated Data, Joins and Set Operators Intermediate SQL: Aggregated Data, Joins and Set Operators Aggregated Data and Sorting Objectives After completing this lesson, you should be able to do the following: Identify the available group functions

More information

Troubleshooting Performance Problems in SQL Server 2008

Troubleshooting Performance Problems in SQL Server 2008 Troubleshooting Performance Problems in SQL Server 2008 SQL Server Technical Article Writers: Sunil Agarwal, Boris Baryshnikov, Keith Elmore, Juergen Thomas, Kun Cheng, Burzin Patel Technical Reviewers:

More information

Actual4Test. Actual4test - actual test exam dumps-pass for IT exams

Actual4Test.  Actual4test - actual test exam dumps-pass for IT exams Actual4Test http://www.actual4test.com Actual4test - actual test exam dumps-pass for IT exams Exam : 070-761 Title : Querying Data with Transact- SQL Vendor : Microsoft Version : DEMO Get Latest & Valid

More information

Microsoft Examsoon Exam Questions & Answers

Microsoft Examsoon Exam Questions & Answers Microsoft Examsoon 70-461 Exam Questions & Answers Number: 70-461 Passing Score: 700 Time Limit: 300 min File Version: 23.7 http://www.gratisexam.com/ Microsoft 70-461 Exam Questions & Answers Exam Name:

More information

Optimizing SQL. Kate Amorella Proff. AKA SQL Glitter Bombs. Research Analyst

Optimizing SQL. Kate Amorella Proff. AKA SQL Glitter Bombs. Research Analyst Optimizing SQL AKA SQL Glitter Bombs Kate Amorella Proff Research Analyst Join In Please add your name and email to the sign in sheet. If you would like to take part in a SQL user group listserv, place

More information

Structured Query Language

Structured Query Language University College of Southeast Norway Structured Query Language Hans-Petter Halvorsen, 2016.01.08 The Tutorial is available Online: http://home.hit.no/~hansha/?tutorial=sql http://home.hit.no/~hansha

More information

Use Schema_id Sql Server Schema Id Sys Tables

Use Schema_id Sql Server Schema Id Sys Tables Use Schema_id Sql Server Schema Id Sys Tables schema_id) = s. The column principal_id in sys.schemas contains the ID of the schema owner, so to get the name you can Use the column principal_id in sys.tables,

More information