A correlated subquery is a subquery that refers to a column of the outer query, so it cannot run on its own. Conceptually MySQL evaluates it once for each row the outer query processes, passing in that row's values. It is the natural tool for questions like "which employees earn more than the average of their own department".
This guide explains how a correlated subquery executes, shows the common patterns, and covers the performance trade-offs and rewrites that matter on large tables.
The examples use an employees table with a department column. Run it on MySQL 8.4 or newer.
An uncorrelated subquery has no reference to the outer query, so MySQL can compute it once. A correlated subquery uses an outer column inside its own WHERE clause.
The inner query mentions e.department, which belongs to the outer row. Sales averages 60,000 and Tech averages 100,000, so Ravi and Zoya are the only people above their own department's average. Aliases such as e and x are what make the reference unambiguous when both queries read the same table.
The logical model is a loop.
If the outer table has a million rows, the naive plan runs the inner query a million times. The real optimizer is often smarter, but you should assume the cost scales with the outer row count multiplied by the cost of one inner lookup. An index on the correlated column, here department, makes each inner run cheap and often decides whether the query is fast or unusable.
EXISTS is almost always correlated, and it only checks whether the inner query returns any row.
MySQL can stop scanning as soon as it finds one matching row, which is why EXISTS often performs well even on big tables.
You can also compute an extra value per row, such as the highest salary in the employee's department.
This runs the inner query per row too. It is easy to read but not always efficient.
When a correlated subquery is too slow, there are two common rewrites. Check each one with EXPLAIN or EXPLAIN ANALYZE on your own data.
Aggregate once per department, then join.
This computes each department average a single time, instead of once per employee.
MySQL 8.0 and later supports window functions, which express the same idea without a second pass over the table in your query text.
For most "compare to a group value" questions, this is the clearest modern form.
You can use the pattern to update rows based on related data.
MySQL does not allow reading the target table directly in a subquery of the same statement, error 1093. Wrapping the read in a derived table, as above, works around it. Test such statements inside a transaction first.
NULL values in the correlated column never match with =, so those rows get NULL results.An uncorrelated subquery is independent and can be evaluated once. A correlated subquery uses a column from the outer query and conceptually runs once per outer row.
No. With a good index on the correlated columns, or with EXISTS stopping at the first match, they can be fast. They become slow on large tables without indexes.
For comparisons against a group aggregate, a window function is usually shorter and easier to read. Measure both on your data, because the optimizer treats them differently.
Yes, a nested subquery can reference columns of any enclosing query. Keep it shallow, since deep nesting is hard to maintain.
A correlated subquery refers to the outer query's columns and is evaluated per outer row in the logical model. It suits per-group comparisons and existence checks. Index the correlated columns, and consider a derived table join or a window function when performance matters. Always alias tables clearly so the correlation is the one you intend.