dell webinar 2014-06-24: subqueries for superheroes
DESCRIPTION
Slide deck from my Subqueries For Superheroes webinar, presented for Dell Software as part of their Experts Perspective series.TRANSCRIPT
Experts’ Perspective:Subqueries for Superheroes
Tracy McKibben
DBA Supervisor, Pearson VUE
2 Dell Software
Your host
• Richard Douglas
• Sales Engineer / SQL Evangelist
• Maidenhead SQL User Group Leader
• Blog: http://SQL.RichardDouglas.co.uk
• Twitter: @SQLRich
• Email: [email protected]
3 Dell Software
Tracy McKibben
DBA Supervisor, Senior SQL Server DBA
Pearson VUE
Blog: realsqlguy.com
Twitter: @RealSQLGuy
I’m not saying I’m Batman, I’m just saying that nobody has
ever seen me and Batman in the same room together...
4 Dell Software
Today’s Lineup
• What is a subquery?
• Correlated vs non-correlated
• Occupational hazards
• Subqueries in disguise
5 Dell Software
What is a subquery?
A query wrapped within, embedded within, or otherwise referenced by another query. Also known as an INNER query.
The lowly sidekicks of the SQL world.
6 Dell Software
Anatomy of a subquery
Which of these is a valid place to put a subquery?
SELECT ?
FROM ?
INNER JOIN ?
ON ?
WHERE ?
GROUP BY ?
HAVING ?
7 Dell Software
Anatomy of a subquery
SELECT <a subquery can go here>
FROM <or here>
INNER JOIN <or here>
ON <or here>
WHERE <or here>
GROUP BY <or here>
HAVING <or here>
It’s, like, the ultimate superpower!
8 Dell Software
Is this a subquery?
9 Dell Software
Is this a subquery?
10 Dell Software
Is this a subquery?
11 Dell Software
Is this a subquery?
12 Dell Software
Is this a subquery?
13 Dell Software
Is this a subquery?
14 Dell Software
Is this a subquery?
15 Dell Software
Is this a subquery?
16 Dell Software
Exercise Time!
17 Dell Software
Sometimes loopy
A subquery is either correlated or non-correlated.
What is the difference?
18 Dell Software
Sometimes loopy
A subquery is either correlated or non-correlated.
What is the difference?
A correlated subquery depends on the
outer query, looping through the outer
resultset, executing once for each row
in the outer query.
A non-correlated subquery stands
alone, only running once, independent
of the values in the outer query.
19 Dell Software
What’s the correlation?
Does this subquery stand alone (non-correlated), or does it need help (correlated)?
20 Dell Software
What’s the correlation?
Does this subquery stand alone (non-correlated), or does it need help (correlated)?
21 Dell Software
What’s the correlation?
Does this subquery stand alone (non-correlated), or does it need help (correlated)?
22 Dell Software
What’s the correlation?
Does this subquery stand alone (non-correlated), or does it need help (correlated)?
23 Dell Software
What’s the correlation?
Does this subquery stand alone (non-correlated), or does it need help (correlated)?
24 Dell Software
What’s the correlation?
Does this subquery stand alone (non-correlated), or does it need help (correlated)?
25 Dell Software
What’s the correlation?
Does this subquery stand alone (non-correlated), or does it need help (correlated)?
26 Dell Software
What’s the correlation?
Does this subquery stand alone (non-correlated), or does it need help (correlated)?
27 Dell Software
Exercise Time!
28 Dell Software
Occupational hazards
Subqueries offer a lot of power and flexibility.
29 Dell Software
Occupational hazards
Subqueries offer a lot of power and flexibility.
But
sometimes,
things go
terribly
wrong
30 Dell Software
COUNT vs EXISTS
Are there any Green Lantern symbols in this collection?
How do you know?
Did you count them, or did you simply see green and say “yes”?
Did you do this:
WHERE COUNT() > 0?
or, did you do this:
WHERE EXISTS()?
31 Dell Software
NOT IN vs NULL
Be careful of NULL values returned by inner query or value list in a NOT IN clause. NULL has no value and can produce surprising results.
Always returns an empty resultset.
32 Dell Software
MAX vs RANK
MAX/MIN are often used in a subquery to determine the oldest/greatest/most recent row in the outer query.
RANK/OVER and other windowing functions are more efficient. or
33 Dell Software
Exercise Time!
34 Dell Software
Subqueries In Disguise
Some T-SQL constructs behave much like subqueries.
• non-indexed views
• common table expressions (CTE)
• user-defined functions
Be wary of performance problems.
35 Dell Software
Exercise Time!
36 Dell Software
The conclusion
• correlated vs non-correlated - know the difference and potential impacts
• multiple ways to get the same data, but not all perform well
• know new language features like RANK
• don’t count unless you need a number
• be wary of NULL values and hidden subqueries
37 Dell Software
Join us at these upcoming SQL events!
SQL Community Corner
Date Location
June 25PASS DBA Virtual Chapter – “The Day After Tomorrow; Why You Need to Baseline”
July 12 SQLSaturday #312 - Sacramento 2014
July 17-19 SQLBits XII – Telford, UK
July 26 SQLSaturday #302 – Albany 2014
38 Dell Software
Visit our SQL Community on ToadWorld-www.toadworld.com/platforms/sql-server/default.aspx
Pg. 39© 2012 Quest Software Inc. All rights reserved.
Solution Area Product Description
Fast, flexible backup and
recovery with industry-leading
compression technology
Discover and resolve
performance issues in production
before they impact end users and
service levels
Deepest possible understanding
of database performance and
norms
Plan and develop applications
that deliver both functionality and
optimal performance
Backup and
Recovery
Performance
& Operations
Performance
Tuning
Development
Comprehensive schema, object,
security and change
managementAdministration
Community crowdsourcing for
SQL Server tracing and
performance information!
Community,
Knowledge,
Training
40 Dell Software
41 Dell Software
42 Dell Software
Questions?
Continue the conversation on Twitter via #DellSQL
Thank You!