SlideShare a Scribd company logo
WHAT’S THE DIFFERENCE?
VS
VS
Complex queries means
multiple steps. Subqueries,
temporary tables, and
common table expressions
(CTEs) can help. But what
are they?
WHY YOU
SHOULD
KNOW THIS...
WHAT IT IS
A query inside a query
In the FROM, WHERE,
or HAVING clause
Subquery runs
Results of the subquery
replace the subquery
Outer query executes
HOW IT WORKS
Subquery in
parenthesis
Subquery needs an alias
AN EXAMPLE
PROs
Simplest option for filtering or
performing calculations on data
within a query
Often have good performance
CONs
Can make complex queries
harder to read and understand
Not reusable in the same query
PROS & CONS
Stores a temporary result
set that you can reuse
Exists within a single
MySQL session
Can only be used by the
person who created it
WHAT IT IS
CREATE TEMPORARY
TABLE statement
Write query with results
you want stored
Use the temp table like
you would any table
HOW IT WORKS
Temp table needs a ;
Create temp table first
AN EXAMPLE
Use the temp table in
following queries
ANOTHER EXAMPLE
Same syntax for
each temp table
Use the temp
table in following
queries
Create temp table first
PROs
Act like regular tables for storing
intermediate results
Can be accessed by multiple
queries within the same session
Useful for complex data
manipulation with multiple steps
CONs
Disappear after the session ends
Data is physically stored
PROS & CONS
A named temporary
result set
Used in SELECT, INSERT,
UPDATE, or DELETE
Exists for the duration of
a single query
WHAT IT IS
Open with WITH ( )
clause
Write query whose result
set you want to use
Select data from the CTE
in a following query
HOW IT WORKS
AN EXAMPLE
Use the CTE in
following queries
Create CTE using WITH clause
No ; after CTE, closed
with parenthesis
ANOTHER
EXAMPLE
Create first
CTE using
WITH clause
Following CTEs are
separated by a comma
PROs
Improve readability by breaking
complex queries into named
result sets
Can be reused multiple times
within a single query
Support recursive queries
CONs
Limited to a single query (not
accessible by other queries in the
session)
PROS & CONS
Subqueries, temp tables,
and CTEs all have similar
functionality
You can often use
whichever one you
prefer
Each has pros and cons
WHAT IT ALL
MEANS

More Related Content

Similar to CTE vs Temp Tables vs Subquery - SQL.pdf (20)

DOCX
mysql ....question and answer by muhammed thanveer melayi
Muhammed Thanveer M
 
PPTX
Sql killedserver
ColdFusionConference
 
PPTX
My SQL Skills Killed the Server
devObjective
 
PDF
Apache TAJO
Asis Mohanty
 
PDF
Oracle Database Administration I (1Z0-082) Exam Dumps 2024.pdf
SkillCertProExams
 
PDF
How to Fine-Tune Performance Using Amazon Redshift
AWS Germany
 
PPTX
Accelerating Database Performance Using Compression
Joseph D'Antoni
 
PPTX
Database optimization
EsraaAlattar1
 
PPTX
Stored procedure tunning
Virendra Yaduvanshi
 
PPTX
Reduce Cost by Tuning Queries on Azure DBaaS
Datavail
 
PPT
Chapter16
gourab87
 
PPTX
CTEs in SQL.pptx
Select Distinct Limited
 
PPTX
Cognos Performance Inteligencia de negfocios
ericklozada6
 
PPT
Overview of query evaluation
avniS
 
PPTX
Windows azure table storage – deep dive
Sundararajan Subramanian
 
PPT
Understanding System Performance
Teradata
 
PPTX
Subqueries, Backups, Users and Privileges
Ashwin Dinoriya
 
PPTX
T sql語法之 cte 20140214
LearningTech
 
mysql ....question and answer by muhammed thanveer melayi
Muhammed Thanveer M
 
Sql killedserver
ColdFusionConference
 
My SQL Skills Killed the Server
devObjective
 
Apache TAJO
Asis Mohanty
 
Oracle Database Administration I (1Z0-082) Exam Dumps 2024.pdf
SkillCertProExams
 
How to Fine-Tune Performance Using Amazon Redshift
AWS Germany
 
Accelerating Database Performance Using Compression
Joseph D'Antoni
 
Database optimization
EsraaAlattar1
 
Stored procedure tunning
Virendra Yaduvanshi
 
Reduce Cost by Tuning Queries on Azure DBaaS
Datavail
 
Chapter16
gourab87
 
CTEs in SQL.pptx
Select Distinct Limited
 
Cognos Performance Inteligencia de negfocios
ericklozada6
 
Overview of query evaluation
avniS
 
Windows azure table storage – deep dive
Sundararajan Subramanian
 
Understanding System Performance
Teradata
 
Subqueries, Backups, Users and Privileges
Ashwin Dinoriya
 
T sql語法之 cte 20140214
LearningTech
 

More from Debanjan Paul (13)

PDF
324788733-Manufacturing-Domain-Overview.pdf
Debanjan Paul
 
DOCX
FINANCE ERP Data Migration & Deployment.docx
Debanjan Paul
 
PDF
TOGAF 10 Shift to digital Product w205.pdf
Debanjan Paul
 
PDF
My financial organiser_editable
Debanjan Paul
 
DOC
145554520 banquet-operation-manual
Debanjan Paul
 
DOC
221881638 45758870-bubble-tea-cafe-business-plan
Debanjan Paul
 
PDF
Jd sr. solution architect
Debanjan Paul
 
PDF
My financial organiser_editable
Debanjan Paul
 
DOCX
Case study robin hood on 25.04.2014
Debanjan Paul
 
PPTX
Tek partners managed services solutions
Debanjan Paul
 
PDF
Network upgrade
Debanjan Paul
 
PDF
Quality
Debanjan Paul
 
PDF
BTECH_MARKSHEET
Debanjan Paul
 
324788733-Manufacturing-Domain-Overview.pdf
Debanjan Paul
 
FINANCE ERP Data Migration & Deployment.docx
Debanjan Paul
 
TOGAF 10 Shift to digital Product w205.pdf
Debanjan Paul
 
My financial organiser_editable
Debanjan Paul
 
145554520 banquet-operation-manual
Debanjan Paul
 
221881638 45758870-bubble-tea-cafe-business-plan
Debanjan Paul
 
Jd sr. solution architect
Debanjan Paul
 
My financial organiser_editable
Debanjan Paul
 
Case study robin hood on 25.04.2014
Debanjan Paul
 
Tek partners managed services solutions
Debanjan Paul
 
Network upgrade
Debanjan Paul
 
Quality
Debanjan Paul
 
BTECH_MARKSHEET
Debanjan Paul
 
Ad

Recently uploaded (20)

PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
Biography of Daniel Podor.pdf
Daniel Podor
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Ad

CTE vs Temp Tables vs Subquery - SQL.pdf

  • 2. Complex queries means multiple steps. Subqueries, temporary tables, and common table expressions (CTEs) can help. But what are they? WHY YOU SHOULD KNOW THIS...
  • 3. WHAT IT IS A query inside a query In the FROM, WHERE, or HAVING clause
  • 4. Subquery runs Results of the subquery replace the subquery Outer query executes HOW IT WORKS
  • 6. PROs Simplest option for filtering or performing calculations on data within a query Often have good performance CONs Can make complex queries harder to read and understand Not reusable in the same query PROS & CONS
  • 7. Stores a temporary result set that you can reuse Exists within a single MySQL session Can only be used by the person who created it WHAT IT IS
  • 8. CREATE TEMPORARY TABLE statement Write query with results you want stored Use the temp table like you would any table HOW IT WORKS
  • 9. Temp table needs a ; Create temp table first AN EXAMPLE Use the temp table in following queries
  • 10. ANOTHER EXAMPLE Same syntax for each temp table Use the temp table in following queries Create temp table first
  • 11. PROs Act like regular tables for storing intermediate results Can be accessed by multiple queries within the same session Useful for complex data manipulation with multiple steps CONs Disappear after the session ends Data is physically stored PROS & CONS
  • 12. A named temporary result set Used in SELECT, INSERT, UPDATE, or DELETE Exists for the duration of a single query WHAT IT IS
  • 13. Open with WITH ( ) clause Write query whose result set you want to use Select data from the CTE in a following query HOW IT WORKS
  • 14. AN EXAMPLE Use the CTE in following queries Create CTE using WITH clause No ; after CTE, closed with parenthesis
  • 15. ANOTHER EXAMPLE Create first CTE using WITH clause Following CTEs are separated by a comma
  • 16. PROs Improve readability by breaking complex queries into named result sets Can be reused multiple times within a single query Support recursive queries CONs Limited to a single query (not accessible by other queries in the session) PROS & CONS
  • 17. Subqueries, temp tables, and CTEs all have similar functionality You can often use whichever one you prefer Each has pros and cons WHAT IT ALL MEANS