SELECT every employee that has a higher salary than the AVERAGE of his department

后端 未结 7 1346
臣服心动
臣服心动 2021-01-13 23:46

I have only 1 table named EMPLOYEE on my database with the 3 following collumns:

Employee_Name, Employee_Salary, Department_ID
7条回答
  •  花落未央
    2021-01-14 00:23

    Try using EXISTS() like:

    SELECT t1.Employee_Name, t1.Employee_Salary, t1.Department_ID
    FROM Employee t1
    WHERE EXISTS
    (
     SELECT t2.Department_ID, AVG(t2.Employee_Salary) as AvgSalary
     FROM Employee t2
     WHERE   t1.Department_ID = t2.Department_ID
     GROUP BY t2.Department_ID
     HAVING t1.Employee_Salary>AVG(t2.Employee_Salary)
    

    );

    See Fiddle Demo

提交回复
热议问题