• Like
Subqueries For Superheroes
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Subqueries For Superheroes

  • 741 views
Published

A quick look at how T-SQL subqueries can be used to improve query performance, as well as some ways in which they can cause problems.

A quick look at how T-SQL subqueries can be used to improve query performance, as well as some ways in which they can cause problems.

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
741
On SlideShare
0
From Embeds
0
Number of Embeds
3

Actions

Shares
Downloads
8
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Subqueries For Superheroes
  • 2. 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...
  • 3. What is a subquery?
  • 4. What is a subquery? A query wrapped within another query. Also known as an INNER query. The lowly sidekicks of the SQL world.
  • 5. Anatomy of a subquery Which of these is a valid place to put a subquery? SELECT ? FROM ? INNER JOIN ? ON ? WHERE ? GROUP BY ? HAVING ?
  • 6. 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!
  • 7. Is this a subquery?
  • 8. Is this a subquery?
  • 9. Is this a subquery?
  • 10. Is this a subquery?
  • 11. Is this a subquery?
  • 12. Is this a subquery?
  • 13. Is this a subquery?
  • 14. Is this a subquery?
  • 15. Exercise time!
  • 16. Sometimes loopy A subquery is either correlated or non-correlated. What is the difference?
  • 17. 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.
  • 18. What’s the correlation? Does this subquery stand alone (non-correlated), or does it need help (correlated)?
  • 19. What’s the correlation? Does this subquery stand alone (non-correlated), or does it need help (correlated)?
  • 20. What’s the correlation? Does this subquery stand alone (non-correlated), or does it need help (correlated)?
  • 21. What’s the correlation? Does this subquery stand alone (non-correlated), or does it need help (correlated)?
  • 22. What’s the correlation? Does this subquery stand alone (non-correlated), or does it need help (correlated)?
  • 23. What’s the correlation? Does this subquery stand alone (non-correlated), or does it need help (correlated)?
  • 24. What’s the correlation? Does this subquery stand alone (non-correlated), or does it need help (correlated)?
  • 25. What’s the correlation? Does this subquery stand alone (non-correlated), or does it need help (correlated)?
  • 26. Exercise time!
  • 27. Occupational hazards Subqueries offer a lot of power and flexibility.
  • 28. Occupational hazards Subqueries offer a lot of power and flexibility. But sometimes, things go terribly wrong
  • 29. COUNT vs EXISTS Are there any Green Lantern symbols in this collection? Did you count them, or did you simply see green and say “yes”? How do you know? Did you do this: WHERE COUNT() > 0? or, did you do this: WHERE EXISTS()?
  • 30. 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.
  • 31. 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. defeats
  • 32. Exercise time!
  • 33. 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.
  • 34. Exercise time!
  • 35. 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
  • 36. Subqueries For Superheroes