Correlated subquery
In a SQL database query, a correlated subquery (also known as a synchronized subquery) is a subquery (a query nested inside another query) that uses values from the outer query. This can have major impact on performance because the correlated subquery might get recomputed every time for each row of the outer query is processed. A correlated subquery can contain another correlated subquery. Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.
In a SQL database query, a correlated subquery (also known as a synchronized subquery) is a subquery (a query nested inside another query) that uses values from the outer query. This can have major impact on performance because the correlated subquery might get recomputed every time for each row of the outer query is processed. A correlated subquery can contain another correlated subquery. SELECT employee_number, name FROM employees emp WHERE salary > ( SELECT AVG(salary) FROM employees WHERE department = emp.department); In the above query the outer query is SELECT employee_number, name FROM employees emp WHERE salary > ... and the inner query (the correlated subquery) is SELECT AVG(salary) FROM employees WHERE department = emp.department In the above nested query the inner query has to be re-executed for each employee. (A sufficiently smart implementation may cache the inner query's result on a department-by-department basis, but even in the best case the inner query must be executed once per department.) SELECT employee_number, name, (SELECT AVG(salary) FROM employees WHERE department = emp.department) AS department_average FROM employees emp ==References==
Cite this
"Correlated subquery". 1773228567. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1342948176 JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy