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
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
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
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