What is the order of execution process of subquery in SQL?
What is the order of execution process of subquery in SQL?
1339
18-Jan-2024
Updated on 07-Jan-2025
Khushi Singh
07-Jan-2025Order of Execution Process for Subqueries in SQL
Non-Correlated Subqueries:
The subquery runs before producing a result set to be utilized by the outer query.
Example: Subquery located in the
WHEREorHAVINGclauses.Correlated Subqueries:
The outer query executes first, and the subquery runs for each row of the outer query.
Example:
Subqueries in
FROMClause (Inline Views):The subquery executes as a temporary table, then the outer query processes its result.
Subqueries in
SELECTClause:The outer query executes, and the subquery runs for each row.
Summary:
Non-Correlated: Subquery runs first.
Correlated: Outer query begins first and then comes the subquery and it works row wise.
FROM Clause: Subquery is treated as a temporary table that is formally known as derived table.
SELECT Clause: Subquery occurs for each record of the outer query on the database.