SlideShare ist ein Scribd-Unternehmen logo
1 von 36
Downloaden Sie, um offline zu lesen
Subqueries
For Superheroes
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...
What is a subquery?
What is a subquery?
A query wrapped within another query. Also
known as an INNER query.
The lowly sidekicks of the
SQL world.
Anatomy of a subquery
Which of these is a valid place to put a
subquery?
SELECT ?
FROM ?
INNER JOIN ?
ON ?
WHERE ?
GROUP BY ?
HAVING ?
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!
Is this a subquery?
Is this a subquery?
Is this a subquery?
Is this a subquery?
Is this a subquery?
Is this a subquery?
Is this a subquery?
Is this a subquery?
Exercise time!
Sometimes loopy
A subquery is either correlated or non-correlated.
What is the difference?
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.
What’s the correlation?
Does this subquery stand alone (non-correlated),
or does it need help (correlated)?
What’s the correlation?
Does this subquery stand alone (non-correlated),
or does it need help (correlated)?
What’s the correlation?
Does this subquery stand alone (non-correlated),
or does it need help (correlated)?
What’s the correlation?
Does this subquery stand alone (non-correlated),
or does it need help (correlated)?
What’s the correlation?
Does this subquery stand alone (non-correlated),
or does it need help (correlated)?
What’s the correlation?
Does this subquery stand alone (non-correlated),
or does it need help (correlated)?
What’s the correlation?
Does this subquery stand alone (non-correlated),
or does it need help (correlated)?
What’s the correlation?
Does this subquery stand alone (non-correlated),
or does it need help (correlated)?
Exercise time!
Occupational hazards
Subqueries offer a lot of power and flexibility.
Occupational hazards
Subqueries offer a lot of power and flexibility.

But
sometimes,
things go
terribly
wrong
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()?
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.
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
Exercise time!
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.
Exercise time!
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
Subqueries
For Superheroes

Weitere ähnliche Inhalte

Andere mochten auch

Linguistic landscape
Linguistic landscapeLinguistic landscape
Linguistic landscape
ayurkosky
 

Andere mochten auch (19)

Teamwork
TeamworkTeamwork
Teamwork
 
Acercamiento a las_ciencias_naturales_lepri
Acercamiento a las_ciencias_naturales_lepriAcercamiento a las_ciencias_naturales_lepri
Acercamiento a las_ciencias_naturales_lepri
 
Ppt toy3
Ppt toy3Ppt toy3
Ppt toy3
 
Swipp Plus Overview for Marketers
Swipp Plus Overview for MarketersSwipp Plus Overview for Marketers
Swipp Plus Overview for Marketers
 
White Paper: DB2 and FAST VP Testing and Best Practices
White Paper: DB2 and FAST VP Testing and Best Practices   White Paper: DB2 and FAST VP Testing and Best Practices
White Paper: DB2 and FAST VP Testing and Best Practices
 
Manage vm’s and services across private clouds and windows azure with system ...
Manage vm’s and services across private clouds and windows azure with system ...Manage vm’s and services across private clouds and windows azure with system ...
Manage vm’s and services across private clouds and windows azure with system ...
 
Mayrikis voski dzerqer
Mayrikis voski dzerqerMayrikis voski dzerqer
Mayrikis voski dzerqer
 
Introduction to the EMC XtremIO All-Flash Array
Introduction to the EMC XtremIO All-Flash ArrayIntroduction to the EMC XtremIO All-Flash Array
Introduction to the EMC XtremIO All-Flash Array
 
Yourprezi
YourpreziYourprezi
Yourprezi
 
RSA-Pivotal Security Big Data Reference Architecture
RSA-Pivotal Security Big Data Reference ArchitectureRSA-Pivotal Security Big Data Reference Architecture
RSA-Pivotal Security Big Data Reference Architecture
 
Take3 Capabilities
Take3 CapabilitiesTake3 Capabilities
Take3 Capabilities
 
Driving maximum value_from_mobile_technology_(en) for aerospace & defense ind...
Driving maximum value_from_mobile_technology_(en) for aerospace & defense ind...Driving maximum value_from_mobile_technology_(en) for aerospace & defense ind...
Driving maximum value_from_mobile_technology_(en) for aerospace & defense ind...
 
Informe de empleados
Informe de empleadosInforme de empleados
Informe de empleados
 
3 law of supply
3   law of supply3   law of supply
3 law of supply
 
White Paper: DB2 and FAST VP Testing and Best Practices
White Paper: DB2 and FAST VP Testing and Best Practices   White Paper: DB2 and FAST VP Testing and Best Practices
White Paper: DB2 and FAST VP Testing and Best Practices
 
Linguistic landscape
Linguistic landscapeLinguistic landscape
Linguistic landscape
 
Big Data: A CIO’s Cut Out and Keep Guide
Big Data: A CIO’s Cut Out and Keep Guide Big Data: A CIO’s Cut Out and Keep Guide
Big Data: A CIO’s Cut Out and Keep Guide
 
Formulario clientes
Formulario clientesFormulario clientes
Formulario clientes
 
EMC IT's Journey to Cloud : VIRTUAL DESKTOP
EMC IT's Journey to Cloud : VIRTUAL DESKTOPEMC IT's Journey to Cloud : VIRTUAL DESKTOP
EMC IT's Journey to Cloud : VIRTUAL DESKTOP
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

Subqueries For Superheroes