‹ BACK

Correlated subquery

Correlated subquery

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.
SELECT
   employee_number,
   name,
   (SELECT AVG(salary) 
      FROM employees
      WHERE department = Bob.department) AS department_average
   FROM employees AS Bob
 group by employee_number,name ;

Read more.
sub-queries correlated-subquery synchronized-subquery

To check your knowledge the following tests are recommended:
Check your sql sql skills.