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

CTE vs Temp Tables vs Subquery - SQL.pdf

  • 1.
  • 2.
    Complex queries means multiplesteps. Subqueries, temporary tables, and common table expressions (CTEs) can help. But what are they? WHY YOU SHOULD KNOW THIS...
  • 3.
    WHAT IT IS Aquery inside a query In the FROM, WHERE, or HAVING clause
  • 4.
    Subquery runs Results ofthe subquery replace the subquery Outer query executes HOW IT WORKS
  • 5.
  • 6.
    PROs Simplest option forfiltering 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 temporaryresult 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 Writequery with results you want stored Use the temp table like you would any table HOW IT WORKS
  • 9.
    Temp table needsa ; Create temp table first AN EXAMPLE Use the temp table in following queries
  • 10.
    ANOTHER EXAMPLE Same syntaxfor each temp table Use the temp table in following queries Create temp table first
  • 11.
    PROs Act like regulartables 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 resultset 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 theCTE in following queries Create CTE using WITH clause No ; after CTE, closed with parenthesis
  • 15.
    ANOTHER EXAMPLE Create first CTE using WITHclause Following CTEs are separated by a comma
  • 16.
    PROs Improve readability bybreaking 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, andCTEs all have similar functionality You can often use whichever one you prefer Each has pros and cons WHAT IT ALL MEANS