相關子查詢

相關(也稱為同步或協調)子查詢是巢狀查詢,它們引用其外部查詢的當前行:

SELECT EmployeeId
    FROM Employee AS eOuter
    WHERE Salary > (
       SELECT AVG(Salary)
       FROM Employee eInner
       WHERE eInner.DepartmentId = eOuter.DepartmentId
    )

子查詢 SELECT AVG(Salary) ... 是*相關的,*因為它從其外部查詢引用 Employee row eOuter