0
0
MySQLquery~10 mins

Correlated subqueries in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Correlated subqueries
Start Outer Query Row
Execute Subquery Using Outer Row Value
Subquery Returns Result
Use Subquery Result in Outer Query Condition
Decide to Include Outer Row
Move to Next Outer Query Row or End
For each row in the outer query, the subquery runs using that row's data, then the outer query uses the subquery result to decide if the row qualifies.
Execution Sample
MySQL
SELECT e1.name
FROM employees e1
WHERE e1.salary > (
  SELECT AVG(e2.salary)
  FROM employees e2
  WHERE e2.department = e1.department
);
Find employees whose salary is greater than the average salary in their own department.
Execution Table
StepOuter Row (e1.name, e1.salary, e1.department)Subquery Condition (e2.department = e1.department)Subquery Result (AVG salary)Outer Condition (e1.salary > AVG)Include Row in Result
1(Alice, 7000, Sales)e2.department = 'Sales'60007000 > 6000 = TrueYes
2(Bob, 5000, Sales)e2.department = 'Sales'60005000 > 6000 = FalseNo
3(Charlie, 8000, HR)e2.department = 'HR'75008000 > 7500 = TrueYes
4(Diana, 7500, HR)e2.department = 'HR'75007500 > 7500 = FalseNo
5(Eve, 9000, IT)e2.department = 'IT'85009000 > 8500 = TrueYes
6No more rows---End
💡 All outer rows processed; query ends.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
e1.nameNoneAliceBobCharlieDianaEveNone
e1.salaryNone70005000800075009000None
e1.departmentNoneSalesSalesHRHRITNone
Subquery AVG salaryNone60006000750075008500None
Include Row?NoneYesNoYesNoYesNone
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 (e1.department), it must run for each outer row to get the correct average salary for that department, as shown in the execution_table rows 1 to 5.
What happens if the subquery returns NULL or no rows?
If the subquery returns NULL, the comparison in the outer query (e1.salary > NULL) evaluates to false, so that outer row is excluded. This is not shown in this example but is important to remember.
How does the outer query decide to include a row?
It compares the outer row's salary to the subquery's average salary result. If the outer salary is greater, the row is included (see execution_table column 'Include Row in Result').
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the subquery average salary when processing Diana's row?
A6000
B7500
C8500
DNone
💡 Hint
Check the 'Subquery Result (AVG salary)' column at step 4 in the execution_table.
At which step does the outer condition become false for the first time?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Outer Condition (e1.salary > AVG)' column in the execution_table and find the first 'False'.
If Bob's salary was 6500 instead of 5000, how would the 'Include Row in Result' change at step 2?
AChange to Yes
BNo change, still No
CChange to NULL
DQuery would error
💡 Hint
Compare Bob's new salary 6500 to the subquery average 6000 at step 2 in the execution_table.
Concept Snapshot
Correlated subqueries run once per outer row using that row's data.
Syntax: Outer query references outer row in subquery condition.
Subquery result affects outer row filtering or selection.
Useful for comparisons within groups or related data.
Runs slower than simple subqueries because of repetition.
Full Transcript
Correlated subqueries execute the subquery for each row of the outer query using that row's values. For example, to find employees earning more than their department's average salary, the subquery calculates the average salary for the current employee's department. The outer query then compares the employee's salary to this average. This process repeats for each employee row. The execution table shows each step: the outer row, the subquery condition using the outer row's department, the average salary result, the comparison, and whether the row is included. Variables like e1.name, e1.salary, and e1.department change as we move through rows. Key points include understanding why the subquery runs multiple times and how the outer query uses the subquery result to filter rows. Visual quiz questions help reinforce these ideas by referencing specific steps and values in the execution table.