Difference between sub query and nested query ?

Answer Posted / sunil

A correlated Subquery runs for the rows selected from the outer query. It takes the value from the outer query
and execute the inner query for that value

example:

select * from emp e
where e.deptno in(select d.deptno from dept d
where e.deptno = d.deptno);

in this query emp table's deptno will be passed into the inner query(select deptno from dept d where e.deptno = d.deptno).
And the inner query will execute only for that value from the outer query.
That's why it is called correlated subquery

In Nested subquery the inner query runs only once and pass the result set to the outer query.

example

select * from emp e
where e.deptno in(select d.deptno from dept d);

Here the inner query (select d.deptno form dept d) will run first and fetches all the rows from the dept table
and the outer query will select only the records that has the matching deptno in the result set fetched by the
inner query. The outer query will act as a nesting query and that is why this is called nested subquery.

Here in correlated subquery, the outer query executes first and the inner query will execute second.

But in Nested subquery, the inner query executes first and the outer query executes second.

Hope this helps.
thanks to binosh who helped me to understand this concept before posting here...

Is This Answer Correct ?    5 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the parameters that we can pass through a stored procedure?

557


How to define a variable of a specific record type?

614


How to use windows user to connect to the server?

536


State all possible different index configurations a table can possibly have?

572


What is a snapshot in oracle database?

583






How to unlock the sample user account in oracle?

558


What is ceil and floor in oracle?

579


How do you find out from the RMAN catalog if a particular archive log has been backed-up?

1661


What is oracle datasource?

556


State and explain the different types of data models?

538


How to define a data source name (dsn) in odbc manager?

529


What is the scope of a local variable?

585


Explain the blob datatype?

620


What is meant by joins? List out the types of joins.

528


What happens if the update subquery returns multiple rows?

613