What is the difference between Correlated and Un-correlated Sub query?

When we write a subquery in such a way that inner subquery and outer main query are interdependent, then we call it s correlated Sub query. In this case, for executing every row of inner query, the outer query is also executed. The inner query needs data from the outer query for its execution.

E.g.
SELECT e.emp_name
FROM employee e
WHERE e.id = (SELECT d.emp_id
FROM dept d
WHERE d.dept_id = e.dept_id);

In a non-correlated subquery, inner subquery has no dependency on outer query.

Read the full book at www.amazon.com
Posted in SQL, SQL Interview Questions

Leave a Reply

Your email address will not be published. Required fields are marked *

*