Nested subquery and Co-related query
What is the difference between Nested Subquery and co-related query.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
TulasiRank #2,544 · 549 reputationPosted Oct 23, 2011, 8:36 PM EDT
Correlated subquery runs once for each row selected by the outer query. It contains a reference to a value from the row selected by the outer query.
Nested subquery runs only once for the entire nesting (outer) query. It does not contain any reference to the outer query row.
For example,
Correlated Subquery:
select e1.empname, e1.basicsal, e1.deptno from emp e1 where e1.basicsal = (select max(basicsal) from emp e2 where e2.deptno = e1.deptno)
Nested Subquery:
select empname, basicsal, deptno from emp where (deptno, basicsal) in (select deptno, max(basicsal) from emp group by deptno)Please mark the answer as Accepted if it helps you.
Pravin MoreRank #1,419 · 1.2K reputationPosted Oct 23, 2011, 8:33 PM EDT
Co-related sub query is one in which inner query is evaluated only once and from that result outer query is evaluated.
Nested query is one in which Inner query is evaluated for multiple times for gatting one row of that outer query.
ex. Query used with IN() clause is Co-related query.
Query used with = operator is Nested query.
Thanks,
Pravin.
Hemant KumarRank #1,202 · 1.5K reputationPosted Oct 25, 2011, 5:28 AM EDT
Ex: Select A.nID,A.sName from table1 A where A.sCity = (Select sCityName from table2 where nId = A.nID)Nested Query: There is no relationship between inner and outer query.
Ex: Select nID,sName from table1 where sCity in (Select distinct sCityName from table2)For more info...http://www.databasejournal.com/features/mssql/article.php/3485291/Using-a-Correlated-Subquery-in-a-T-SQL-Statement.htm
http://www.dba-sql-server.com/sql_server_tips/t_super_sql_411_subqueries.htm
http://vivekjohari.blogspot.com/2009/09/difference-between-subquery-nested.html