0
0
SQLquery~10 mins

Correlated subquery execution model in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Correlated subquery execution model
Start Outer Query Row
Evaluate Correlated Subquery
Use Outer Row Value in Subquery
Subquery Returns Result
Combine Subquery Result with Outer Row
Move to Next Outer Query Row
Repeat
End when no more outer rows
For each row in the outer query, the correlated subquery runs using values from that row, then returns a result combined with the outer row.
Execution Sample
SQL
SELECT e.name, e.salary
FROM employees e
WHERE e.salary > (
  SELECT AVG(salary)
  FROM employees
  WHERE department = e.department
);
Find employees whose salary is above the average salary of their own department.
Execution Table
StepOuter Row (e.name, e.department, e.salary)Subquery ConditionSubquery Result (AVG salary)Outer Condition (e.salary > AVG)Action
1(Alice, Sales, 5000)department = 'Sales'45005000 > 4500 = TrueInclude Alice
2(Bob, Sales, 4000)department = 'Sales'45004000 > 4500 = FalseExclude Bob
3(Carol, HR, 6000)department = 'HR'55006000 > 5500 = TrueInclude Carol
4(Dave, HR, 5000)department = 'HR'55005000 > 5500 = FalseExclude Dave
5(Eve, IT, 7000)department = 'IT'70007000 > 7000 = FalseExclude Eve
6No more rows---Stop execution
💡 No more outer rows to process, query ends.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
e.nameN/AAliceBobCarolDaveEveN/A
e.departmentN/ASalesSalesHRHRITN/A
e.salaryN/A50004000600050007000N/A
Subquery AVGN/A45004500550055007000N/A
Outer Condition ResultN/ATrueFalseTrueFalseFalseN/A
Key Moments - 3 Insights
Why does the subquery run multiple times instead of just once?
Because the subquery depends on the current outer row's department value, it must run for each outer row to get the correct average for that department (see execution_table rows 1-5).
What happens if the subquery returns no rows for a department?
The AVG function returns NULL, so the outer condition comparing salary > NULL becomes false, excluding that outer row (not shown in this example but important to know).
Why is Eve excluded even though her salary equals the average?
The condition uses > (greater than), so equality does not satisfy it. Eve's salary equals the average, so the condition is false (see execution_table row 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the subquery AVG salary result when processing Carol's row?
A4500
B6000
C5500
D7000
💡 Hint
Check execution_table row 3 under 'Subquery Result (AVG salary)'.
At which step does the outer condition become false for the first time?
AStep 2
BStep 3
CStep 1
DStep 5
💡 Hint
Look at 'Outer Condition Result' column in variable_tracker after each step.
If the condition changed to >= instead of >, which employee would be included additionally?
ADave
BEve
CBob
DNo change
💡 Hint
Check execution_table row 5 where Eve's salary equals the average but was excluded due to > condition.
Concept Snapshot
Correlated Subquery:
- Runs once per outer query row
- Uses outer row values inside subquery
- Returns result combined with outer row
- Useful for row-by-row comparisons
- Can be slower due to repeated subquery execution
Full Transcript
A correlated subquery runs once for each row of the outer query. It uses values from the current outer row inside the subquery condition. For example, to find employees earning more than their department's average salary, the subquery calculates the average salary for the department of the current employee. This means the subquery runs multiple times, once per employee. The outer query then compares the employee's salary to this average. If the condition is true, the employee is included in the result. This process repeats until all outer rows are processed. Understanding this step-by-step helps beginners see why correlated subqueries can be slower but are powerful for row-specific filtering.