Download presentation
Presentation is loading. Please wait.
Published byNaomi Blankenship Modified over 8 years ago
2
Sofia, Bulgaria | 9-10 October SQL Querying Tips & Techniques Richard Campbell
3
Sofia, Bulgaria | 9-10 October Who Am I? ●Consultant in development methodology and high scaling systems ●Microsoft Regional Director ●Partner in PWOP Productions ●Author of Advisor Answers ●www.campbellassociates.ca ●C●Consultant in development methodology and high scaling systems ●M●Microsoft Regional Director ●P●Partner in PWOP Productions ●A●Author of Advisor Answers ●w●www.campbellassociates.ca
4
Sofia, Bulgaria | 9-10 October PWOP Productions ●.NET Rocks! ●Internet Audio Talkshow for.NET Developers ●www.dotnetrocks.comwww.dotnetrocks.com ●dnrTV ●Screencast (see the code!) ●www.dnrtv.comwww.dnrtv.com ●.●.NET Rocks! ●I●Internet Audio Talkshow for.NET Developers ●w●www.dotnetrocks.com ●d●dnrTV ●S●Screencast (see the code!) ●w●www.dnrtv.com
5
Sofia, Bulgaria | 9-10 October Agenda ●Subqueries & CTEs ●Self-Joins & Recursion ●Ranking Functions ●The Crosstab Problem ●Deadlocks Tricks ●Subqueries & CTEs ●Self-Joins & Recursion ●Ranking Functions ●The Crosstab Problem ●Deadlocks Tricks
6
Sofia, Bulgaria | 9-10 October Advanced Queries ●No teaching the basics here ●We presume you already understand SELECT, JOIN, WHERE, GROUP BY, ORDER BY… ●And now you’re looking for more ●No teaching the basics here ●We presume you already understand SELECT, JOIN, WHERE, GROUP BY, ORDER BY… ●And now you’re looking for more
7
Sofia, Bulgaria | 9-10 October Subqueries ●Can act much like a join ●More efficient at finding general matches ●Good for comparing a set to a set ●Locating ranged values ●Finding duplicates ●Can act much like a join ●More efficient at finding general matches ●Good for comparing a set to a set ●Locating ranged values ●Finding duplicates
8
Sofia, Bulgaria | 9-10 October Common Table Expressions ●Uses ANSI syntax and semantics ●Very similar to a view, but embedded in the query ●May be used in front of SELECT, UPDATE, DELETE, INSERT ●Uses ANSI syntax and semantics ●Very similar to a view, but embedded in the query ●May be used in front of SELECT, UPDATE, DELETE, INSERT
9
Sofia, Bulgaria | 9-10 October Self-Joins ●Joining a Table to Itself ●Classic scenario: the organizational chart ●Each employee record has an employee_ID and a ReportsTo_ID that points to another employee ●There can be one (and only one) report path ●Joining a Table to Itself ●Classic scenario: the organizational chart ●Each employee record has an employee_ID and a ReportsTo_ID that points to another employee ●There can be one (and only one) report path
10
Sofia, Bulgaria | 9-10 October Recursive Queries ●This is the main reason MSFT introduced the CTEs ●Recursion is achieved by allowing the CTE refer to itself ●Such CTE must have special syntax ●WITH REC_CTE as ●(SELECT1 UNION ALL SELECT2) ●This is the main reason MSFT introduced the CTEs ●Recursion is achieved by allowing the CTE refer to itself ●Such CTE must have special syntax ●WITH REC_CTE as ●(SELECT1 UNION ALL SELECT2)
11
Sofia, Bulgaria | 9-10 October Ranking Queries ●Self-joins can be used to rank data ●Join on a >= expression ●Use COUNT(*) to set rank ●When two rows are ranked the same, they both get the greater value ●Self-joins can be used to rank data ●Join on a >= expression ●Use COUNT(*) to set rank ●When two rows are ranked the same, they both get the greater value
12
Sofia, Bulgaria | 9-10 October Ranking Functions ●SQL Server adds 4 ranking functions: ●Row_Number() ●RANK() ●DENSE_RANK() ●nTile(n) ●Ability to partition and “window” these functions ●Ability to perform aggregate operations over the ranking functions ●Can be used with User Defined Functions ●SQL Server adds 4 ranking functions: ●Row_Number() ●RANK() ●DENSE_RANK() ●nTile(n) ●Ability to partition and “window” these functions ●Ability to perform aggregate operations over the ranking functions ●Can be used with User Defined Functions
13
Sofia, Bulgaria | 9-10 October The Crosstab Problem ●What’s a Crosstab? ●Turning rows into columns ●For example: ●Sales by Product Per Month ●Sales by Product Per Salesperson ●Etc… ●What’s a Crosstab? ●Turning rows into columns ●For example: ●Sales by Product Per Month ●Sales by Product Per Salesperson ●Etc…
14
Sofia, Bulgaria | 9-10 October The Rozenshtein Method ●Taken from Optimizing Transact-SQL: Advanced Programming Techniques ●Uses boolean aggregates ●Each column has a numeric expression that resolves each row as a zero or one ●Taken from Optimizing Transact-SQL: Advanced Programming Techniques ●Uses boolean aggregates ●Each column has a numeric expression that resolves each row as a zero or one
15
Sofia, Bulgaria | 9-10 October PIVOT and UNPIVOT ●PIVOT ●Transforms a set of rows to columns ●Similar to Access TRANSFORM ●Useful for open schemas/OLAP scenarios ●UNPIVOT ●Reverse operation of PIVOT ●PIVOT ●Transforms a set of rows to columns ●Similar to Access TRANSFORM ●Useful for open schemas/OLAP scenarios ●UNPIVOT ●Reverse operation of PIVOT
16
Sofia, Bulgaria | 9-10 October Deadlocks ●What is a deadlock? ●Connection A locks table 1, needs table 2 ●Connection B locks table 2, needs table 1 ●Someone has to lose ●What is a deadlock? ●Connection A locks table 1, needs table 2 ●Connection B locks table 2, needs table 1 ●Someone has to lose
17
Sofia, Bulgaria | 9-10 October Deadlocks ●Solution: ●Don’t ever do that! ●Always update tables in the same order ●Plan an order of updates, always follow it ●Do your updates in stored procedures ●Even then, sometimes you’ll get deadlocks ●(busy databases can be funny things) ●Solution: ●Don’t ever do that! ●Always update tables in the same order ●Plan an order of updates, always follow it ●Do your updates in stored procedures ●Even then, sometimes you’ll get deadlocks ●(busy databases can be funny things)
18
Sofia, Bulgaria | 9-10 October Questions?
19
Sofia, Bulgaria | 9-10 October Please fill out the survey forms! They are the key to amazing prizes that you can get at the end of each day Thank you!
20
Sofia, Bulgaria | 9-10 October
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.