1. 程式人生 > >相關子查詢與非相關子查詢

相關子查詢與非相關子查詢

--非相關子查詢
SELECT EMPNO, LASTNAME 
    FROM EMPLOYEE 
    WHERE WORKDEPT = 'A00' 
     AND SALARY > (SELECT AVG(SALARY) 
              FROM EMPLOYEE 
              WHERE WORKDEPT = 'A00') 

--相關子查詢
SELECT E1.EMPNO, E1.LASTNAME, E1.WORKDEPT 
    FROM EMPLOYEE E1 
    WHERE SALARY > (SELECT AVG(SALARY) 
              FROM EMPLOYEE E2 

              WHERE E2.WORKDEPT = E1.WORKDEPT)