What is the difference between the SubQuery and Corelated SubQuery ?

Answer : The inner query is executed only once…

Subquery

  • The inner query is executed only once.
  • The inner query will get executed first and the output of the inner query used by the outer query.
  • The inner query is not dependent on outer query.
SELECT Employee_name, dept_no 
FROM Employee 
WHERE Employee_name IN (SELECT Employee_name FROM Customer);

Correlated SubQuery

  • The outer query will get executed first and for every row of outer query, inner query will get executed.
  • So the inner query will get executed as many times as number of rows in result of the outer query.
  • The outer query output can use the inner query output for comparison. This means inner query and outer query dependent on each other.
[pastacode lang=”sql” manual=”SELECT%20Employee_name%2Cdept_id%20%0AFROM%20Employee%0AWHERE%20Employee_name%20IN%20(SELECT%20Employee_name%20FROM%20dept%20WHERE%20Employee.dept_id%3Ddept.dept_id)%3B” message=”” highlight=”” provider=”manual”/]
Subquery and Corelated Subquery
Leave a Reply

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

You May Also Like