Correlated subqueries are used for row-by-row processing. Each subquery is executed once for every row of the outer
query.
A correlated subquery is evaluated once for each row processed by the parent statement. The parent statement can be a SELECT, UPDATE, or DELETE statement.
SELECT column1, column2, .... FROM table1 outer WHERE column1 operator [SELECT column1, column2 FROM table2 WHERE expr1 = outer.expr2];
A correlated subquery is one way of reading every row in a table and comparing values in each row against related data. It is used whenever a subquery must return a different result or set of results for each candidate row considered by the main query. In other words, you can use a correlated subquery to answer a multipart question whose answer depends on the value in each row processed by the parent statement.
Nested Subqueries Versus Correlated Subqueries :
With a normal nested subquery, the inner SELECT query runs first and executes once, returning values to be used by the main query. A correlated subquery, however, executes once for
each candidate row considered by the outer query. In other words, the inner query is driven by the outer query.
NOTE : You can also use the ANY and ALL operator in a correlated subquery.
EXAMPLE of Correlated Subqueries : Find all the employees who earn more than the average salary in their department.
SELECT last_name, salary, department_id FROM employees outer WHERE salary > [SELECT AVG[salary] FROM employees WHERE department_id = outer.department_id];
Other use of correlation are in UPDATE and DELETE
CORRELATED UPDATE :
UPDATE table1 alias1 SET column = [SELECT expression FROM table2 alias2 WHERE alias1.column = alias2.column];
Use a correlated subquery to update rows in one table based on rows from another table.
CORRELATED DELETE :
DELETE FROM table1 alias1 WHERE column1 operator [SELECT expression FROM table2 alias2 WHERE alias1.column = alias2.column];
Use a correlated subquery to delete rows in one table based on the rows from another table.
Using the EXISTS Operator :
The EXISTS operator tests for existence of rows in the results set of the subquery. If a subquery row value is found the condition is flagged TRUE and the search does not continue in the inner query, and if it is not found then the condition is flagged FALSE and the
search continues in the inner query.
EXAMPLE of using EXIST operator :
Find employees who have at least one person reporting to them.
SELECT employee_id, last_name, job_id, department_id FROM employees outer WHERE EXISTS [ SELECT ’X’ FROM employees WHERE manager_id = outer.employee_id];
OUTPUT :
EXAMPLE of using NOT EXIST operator :
Find all departments that do not have any employees.
SELECT department_id, department_name FROM departments d WHERE NOT EXISTS [SELECT ’X’ FROM employees WHERE department_id = d.department_id];
OUTPUT
:
13.2.13.7 Correlated Subqueries
A correlated subquery is a subquery that contains a reference to a table that also appears in the outer query. For example:
SELECT * FROM t1
WHERE column1 = ANY [SELECT column1 FROM t2
WHERE t2.column2 = t1.column2];
Notice that the subquery contains a reference to a column of t1
, even though the subquery's FROM
clause does not mention a table t1
. So, MySQL looks outside the subquery, and finds t1
in the outer
query.
Suppose that table t1
contains a row where column1 = 5
and column2 =
6
; meanwhile, table t2
contains a row where column1 = 5
and column2 =
7
. The simple expression ... WHERE column1 =
ANY [SELECT column1 FROM t2]
would be TRUE
, but in this example, the WHERE
clause within the subquery is FALSE
[because [5,6]
is not equal to [5,7]
], so the expression as a whole is FALSE
.
Scoping rule: MySQL evaluates from inside to outside. For example:
SELECT column1 FROM t1 AS x
WHERE x.column1 = [SELECT column1 FROM t2 AS x
WHERE x.column1 = [SELECT column1 FROM t3
WHERE x.column2 = t3.column1]];
In this
statement, x.column2
must be a column in table t2
because SELECT column1
FROM t2 AS x ...
renames t2
. It is not a column in table t1
because SELECT column1 FROM t1 ...
is an outer query that is farther out.
Beginning with MySQL 8.0.24, the optimizer can transform a correlated scalar subquery to a derived table when the subquery_to_derived
flag of the
optimizer_switch
variable is enabled. Consider the query shown here:
SELECT * FROM t1
WHERE [ SELECT a FROM t2
WHERE t2.a=t1.a ] > 0;
To avoid materializing several times for a given derived table, we can instead materialize—once—a derived table which adds a grouping on the join column from the table referenced in the inner query [t2.a
] and then an outer join on the lifted predicate [t1.a =
derived.a
] in order to select the correct
group to match up with the outer row. [If the subquery already has an explicit grouping, the extra grouping is added to the end of the grouping list.] The query previously shown can thus be rewritten like this:
SELECT t1.* FROM t1
LEFT OUTER JOIN
[SELECT a, COUNT[*] AS ct FROM t2 GROUP BY a] AS derived
ON t1.a = derived.a
AND
REJECT_IF[
[ct > 1],
"ERROR 1242 [21000]: Subquery returns more than 1 row"
]
WHERE derived.a > 0;
In the rewritten query, REJECT_IF[]
represents an internal function which tests a given condition [here, the comparison ct > 1
] and raises a given error [in this case,
ER_SUBQUERY_NO_1_ROW
] if the condition is true. This reflects the cardinality check that the optimizer performs as part of evaluating the JOIN
or WHERE
clause, prior to evaluating any lifted predicate, which is done only if the subquery does not return more than one row.
This type of transformation can be performed, provided the following conditions are met:
The subquery can be part of a
SELECT
list,WHERE
condition, orHAVING
condition, but cannot be part of aJOIN
condition, and cannot contain aLIMIT
orOFFSET
clause. In addition, the subquery cannot contain any set operations such asUNION
.The
WHERE
clause may contain one or more predicates, combined withAND
. If theWHERE
clause contains anOR
clause, it cannot be transformed. At least one of theWHERE
clause predicates must be eligible for transformation, and none of them may reject transformation.To be eligible for transformation, a
WHERE
clause predicate must be an equality predicate in which each operand should be a simple column reference. No other predicates—including other comparison predicates—are eligible for transformation. The predicate must employ the equality operator=
for making the comparison; the null-safe≪=>
operator is not supported in this context.A
WHERE
clause predicate that contains only inner references is not eligible for transformation, since it can be evaluated before the grouping. AWHERE
clause predicate that contains only outer references is eligible for transformation, even though it can be lifted up to the outer query block. This is made possible by adding a cardinality check without grouping in the derived table.To be eligible, a
WHERE
clause predicate must have one operand that contains only inner references and one operand that contains only outer references. If the predicate is not eligible due to this rule, transformation of the query is rejected.A correlated column can be present only in the subquery's
WHERE
clause [and not in theSELECT
list, aJOIN
orORDER BY
clause, aGROUP BY
list, or aHAVING
clause]. Nor can there be any correlated column inside a derived table in the subquery'sFROM
list.A correlated column can not be contained in an aggregate function's list of arguments.
A correlated column must be resolved in the query block directly containing the subquery being considered for transformation.
A correlated column cannot be present in a nested scalar subquery in the
WHERE
clause.The subquery cannot contain any window functions, and must not contain any aggregate function which aggregates in a query block outer to the subquery. A
COUNT[]
aggregate function, if contained in theSELECT
list element of the subquery, must be at the topmost level, and cannot be part of an expression.
See also Section 13.2.13.8, “Derived Tables”.