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.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
SQL snippets
Cosmo
Sign Up Now
or Subscribe for future quizzes