0% found this document useful (0 votes)
26 views17 pages

Subqueries Vs Temp Tables

The document explains the differences between subqueries, temporary tables, and common table expressions (CTEs) in SQL. Each method has its own advantages and disadvantages, with subqueries being simple but less reusable, temporary tables allowing for intermediate results but disappearing after the session, and CTEs improving readability but limited to single queries. Understanding these concepts is essential for effectively handling complex queries in SQL.

Uploaded by

Fousseni Diarra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views17 pages

Subqueries Vs Temp Tables

The document explains the differences between subqueries, temporary tables, and common table expressions (CTEs) in SQL. Each method has its own advantages and disadvantages, with subqueries being simple but less reusable, temporary tables allowing for intermediate results but disappearing after the session, and CTEs improving readability but limited to single queries. Understanding these concepts is essential for effectively handling complex queries in SQL.

Uploaded by

Fousseni Diarra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

VS

VS

WHAT’S THE DIFFERENCE?


WHY YOU
SHOULD
KNOW THIS...

Complex queries means


multiple steps. Subqueries,
temporary tables, and
common table expressions
(CTEs) can help. But what
are they?
WHAT IT IS

A query inside a query


In the FROM, WHERE,
or HAVING clause
HOW IT WORKS

Subquery runs
Results of the subquery
replace the subquery
Outer query executes
AN EXAMPLE

Subquery in
parenthesis

Subquery needs an alias


PROS & CONS

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
WHAT IT IS

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
HOW IT WORKS

CREATE TEMPORARY
TABLE statement
Write query with results
you want stored
Use the temp table like
you would any table
AN EXAMPLE

Create temp table first

Temp table needs a ;

Use the temp table in


following queries
ANOTHER EXAMPLE

Create temp table first

Same syntax for


each temp table

Use the temp


table in following
queries
PROS & CONS

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
WHAT IT IS

A named temporary
result set
Used in SELECT, INSERT,
UPDATE, or DELETE
Exists for the duration of
a single query
HOW IT WORKS

Open with WITH ( )


clause
Write query whose result
set you want to use
Select data from the CTE
in a following query
AN EXAMPLE

Create CTE using WITH clause

No ; after CTE, closed


with parenthesis

Use the CTE in


following queries
ANOTHER
EXAMPLE

Create first
CTE using
WITH clause
Following CTEs are
separated by a comma
PROS & CONS

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)
WHAT IT ALL
MEANS

Subqueries, temp tables,


and CTEs all have similar
functionality
You can often use
whichever one you
prefer
Each has pros and cons

You might also like