What is the difference between normal and corelated subqueries?
Answer Posted / arun joy
"Correlated" means it is linked back to the main query. For
example:
SELECT * FROM dept d
WHERE EXISTS
( SELECT 1 FROM emp
WHERE deptno = d.deptno );
This means: for each row in DEPT, go and check in EMP for a
row with the same deptno, and if you find one, report
success. Logically you are asking for the EXISTS check to
be repeated for every single row in DEPT (although the
optimizer may turn it into a hash join or similar if it
decides that will be more efficient). The fact that the
WHERE clause of the subquery refers back to the main query
makes it a correlated subquery.
Now if you'd just said:
SELECT * FROM dept d
WHERE EXISTS
( SELECT 1 FROM emp );
then Oracle can perform the subquery once only at the
start. Once it's confirmed that there is at least 1 record
in EMP then it knows not to re-run the subquery for each
row in DEPT.
Another non-correlated example:
SELECT * FROM dept d
WHERE d.deptno IN
( SELECT deptno
FROM emp
WHERE job = 'CLERK' );
Logically you are asking it to first find the set of
DEPTNOs in EMP that meet the condition, then use that to
retrieve the relevant rows from DEPT.
| Is This Answer Correct ? | 14 Yes | 1 No |
Post New Answer View All Answers
Differentiate between function and procedure in oracle.
How to install oracle database 10g xe?
How to test null values?
Why does oracle 9i treat an empty string as null?
What is private procedure oracle?
What privilege is needed for a user to create views in oracle?
Is there a function to split a string in plsql?
Explain the use of full option in exp command.
What is an external table?
What is the difference between 10g OEM and 11g OEM?
How do you store pictures in a database?
What is oracle host variable?
Explain a data segment?
How to define default values for formal parameters?
Can you create a synonym without having a table?