Difference between Subquery and Correlated Subquery

前端 未结 7 834
Happy的楠姐
Happy的楠姐 2020-11-28 05:10

Is the following piece of SQL Query a normal query or a Correlated Subquery ??

SELECT UserID,
       FirstName,
       LastName,
       DOB,
       GFName,
          


        
7条回答
  •  孤独总比滥情好
    2020-11-28 05:41

    A subquery is a select statement that is embedded in a clause of another select statement.

    EX:

    select ename, sal 
    from emp  where sal > (select sal 
                           from emp where ename ='FORD');
    

    A Correlated subquery is a subquery that is evaluated once for each row processed by the outer query or main query. Execute the Inner query based on the value fetched by the Outer query all the values returned by the main query are matched. The INNER Query is driven by the OUTER Query.

    Ex:

    select empno,sal,deptid 
    from emp e 
    where sal=(select avg(sal) 
               from emp where deptid=e.deptid);
    

    DIFFERENCE

    The inner query executes first and finds a value, the outer query executes once using the value from the inner query (subquery)

    Fetch by the outer query, execute the inner query using the value of the outer query, use the values resulting from the inner query to qualify or disqualify the outer query (correlated)

    For more information : http://www.oraclegeneration.com/2014/01/sql-interview-questions.html

提交回复
热议问题