Correlated sub-queries
https://www.youtube.com/watch?v=SM9cDMxAeK4
Where inner query will be executed for each row of outer query ,
Difference between sub Query/Inner query and correlated nested sub-query
Lest see by real example
EMPLOYEE table
|
EmployeeId
|
NAME
|
Salary
|
DepartmentId
|
|
1
|
Sandeep
|
|
2
|
|
2
|
|
|
2
|
|
3
|
|
|
1
|
|
4
|
|
|
6
|
Requirement 1:
FIND EMPLOYEE NAME, WHOSE SALARY IS GREATER THAN THE AVG SALARY OF ALL THE EMPLOYEE
Requirement 2:
FIND EMPLOYEE NAME, WHOSE SALARY IS GREATER THAN THE AVG SALARY OF HIS DEPARTMENT.
QUERY for Requirement-1
select NAME from EMPLOYEE where salary > (select avg(salary) from EMPLOYEE).
Here the inner query is executed only once, resulted in average salary from all the employee, and then outer query is executed once.
QUERY for Requirement -2
select ename from EMPLOYEE AS E where salary > (select avg(salary) from employee where salary.employeId = E.employeeId).
Here you can see, for each row of outer query the inner query will be executed.
Comments
Post a Comment