Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the rank-math domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u763016847/domains/golinuxhub.com/public_html/wp-includes/functions.php on line 6114
what is the difference between sub-query and co-related sub-query? - GoLinuxHub

what is the difference between sub-query and co-related sub-query?

Sub-query

The inner query execute first, the outer query deprnds on inner query output.

Correlated sub-query

The outer query execute first, the inner query deprnds on outer query result.

A correlated sub-query is where the sub-query part makes reference to values from the outer query.
------------------------------------------------------------------------------------------------------------------------------

SELECT emp1.emp_id, emp1.last_name, emp1.job_id, emp1.department_id from employee emp1

WHERE EXISTS (SELECT ‘X’ FROM employee emp2 WHERE emp2.manager_id = emp1.emp_ID);

Is a correlated sub-query because of the reference to emp1.emp_ID in the sub- query. The sub-query is evaluated once for each row processed by the outer query.

------------------------------------------------------------------------------------------------------------------------------

SELECT e1.employee_number, e1.name FROM employee e1

WHERE e1.salary > (SELECT avg(e2.salary) FROM e2.employee);

Is not a correlated sub-query because the sub-query makes no reference to the values of the outer part of the query.