Skip to content
Surf Wiki
Save to docs
technology/databases

From Surf Wiki (app.surf) — the open knowledge base

Correlated subquery

Computer query in SQL


Summary

Computer query in SQL

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.

Examples

Correlated subqueries in the WHERE clause

Here is an example for a typical correlated subquery. In this example, the objective is to find all employees whose salary is above average for their department.

SQL
 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

SQL
 SELECT employee_number, name
   FROM employees emp
  WHERE salary > ...

and the inner query (the correlated subquery) is

SQL
 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.)

Correlated subqueries in the SELECT clause

Correlated subqueries may appear elsewhere besides the WHERE clause; for example, this query uses a correlated subquery in the SELECT clause to print the entire list of employees alongside the average salary for each employee's department. Again, because the subquery is correlated with a column of the outer query, it must be re-executed for each row of the result.

SQL
 SELECT employee_number,
        name,
        (SELECT AVG(salary) 
           FROM employees
          WHERE department = emp.department) AS department_average
   FROM employees emp

Correlated subqueries in the FROM clause

It is generally meaningless to have a correlated subquery in the FROM clause because the table in the FROM clause is needed to evaluate the outer query, but the correlated subquery in the FROM clause can't be evaluated before the outer query is evaluated, causing a chicken-and-egg problem. Specifically, MariaDB lists this as a limitation in its documentation.

However, in some database systems, it is allowed to use correlated subqueries while joining in the FROM clause, referencing the tables listed before the join using a specified keyword, producing a number of rows in the correlated subquery and joining it to the table on the left. For example, in PostgreSQL, adding the keyword LATERAL before the right-hand subquery,{{cite web | access-date = 2023-01-21 | access-date = 2020-12-24

Computation of correlated subqueries

A commonly used computational method for a correlated subquery is to rewrite it into an equivalent flat query{{cite journal | doi-access= free | access-date = 2023-01-21 | access-date = 2023-01-21 | access-date = 2023-01-21 | access-date = 2023-01-21

References

References

  1. (2015). "Unnesting Arbitrary Queries". Gesellschaft für Informatik, Bonn.
  2. (2025). "Improving Unnesting of Complex Queries". Gesellschaft für Informatik, Bonn.
Wikipedia Source

This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page.

Want to explore this topic further?

Ask Mako anything about Correlated subquery — get instant answers, deeper analysis, and related topics.

Research with Mako

Free with your Surf account

Content sourced from Wikipedia, available under CC BY-SA 4.0.

This content may have been generated or modified by AI. CloudSurf Software LLC is not responsible for the accuracy, completeness, or reliability of AI-generated content. Always verify important information from primary sources.

Report