Subqueries
a query within a query.
When do you need to use a subquery?
You need to use a subquery when you have the need to manipulate an existing table to "pseudo-create" a table that is then used as a part of a larger query.
Combine data from multiple tables into a single result
Create a flexible view of tables stitched together using a âkeyâ
Build an output to use in a later part of the query
Subquery Plan: What happens under the hood

Subquery Basics
Subquery: Placement
WITH: This subquery is used when youâd like to âpseudo-createâ a table from an existing table and visually scope the temporary table at the top of the larger query.
A Common Table Expression (CTE) in SQL allows you to define a temporary result, such as a table, to then be referenced in a later part of the query.
Nested: This subquery is used when youâd like the temporary table to act as a filter within the larger query, which implies that it often sits within the
WHEREclause.Inline: This subquery is used in the same fashion as the
WITHuse case above. However, instead of the temporary table sitting on top of the larger query, itâs embedded within theFROMclause.Scalar: This subquery is used when youâd like to generate a scalar value to be used as a benchmark of some sort.
Nested & Scalar subqueries often do not require aliases the way
WITH& Inline subqueries do.
When a user wants to create a version of an existing table to be used in a larger query
When a user wants to filter an output using a condition met from another table.
Inline subqueries create a âpseudo tableâ that aggregates or manipulates an existing table to be used in a larger query.
It selects only one column or expression and returns one row, used in the select clause of the main query
If a scalar subquery does not find a match, it returns a NULL.
If a scalar subquery finds multiple matches, it returns an ERROR.
For example, when youâd like to calculate the average salary across an entire organization to compare to individual employee salaries. Because itâs often a single value that is generated and used as a benchmark, the scalar subquery often sits within the SELECT clause.
Advantages:
Readability:
WITHandNestedsubqueries are most advantageous for readability.Performance:
Scalarsubqueries are advantageous for performance and are often used on smaller datasets.
Subquery: Dependencies
Simple Subquery: The inner subquery is completely independent of the larger query.
Correlated Subquery: The inner subquery is dependent on the larger query.
When to use Correlated Query ?
When the value of the inner query is dependent on a value outputted from the main query (e.g., the filter statement constantly changes).
In the example above, youâll notice that the value of the inner query -- average GPA -- keeps adjusting depending on the university the student goes to. THAT is a great use case for the correlated subquery.
The inner query or subquery references the outer query.
The inner query doesn't depend on the outer query and can run as a stand-alone query.
The outer query executes before the inner query.
The inner query executes before the outer query.
Last updated
Was this helpful?