Tuesday, 14 February 2012

Co-related Sub Query

=>    If Sub Query references values of Outer Query then it is called as "Co-related Sub Query".
=>    In the co-related Sub QUery, execution starts from Outer Query and Inner Query will be executed as many times as no of records exists in the table.

    Examples:
    1. Select the TOP 3 Maximum salaries ?
    =>    Select distinct sal from emp e1
            where 3 > ( Select count( distinct sal ) from emp e2 where e1.sal < e2.sal ) Order By sal Desc;
   
    2. Select the TOP 3 Minimum salaries ?
    =>    Select distinct sal from emp e1
            where 3 > ( Select count( distinct sal ) from emp e2 where e1.sal > e2.sal );
   

No comments:

Post a Comment