Oracle show all employees with greater than average salary of their department

后端 未结 4 802
离开以前
离开以前 2020-12-11 23:02

I am writing a query to find employees who earn greater than the average salary within their department. I need to display the employee ID, salary, department id, and averag

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-11 23:26

    More efficient to use analytics:

    select employee_id, salary, department_id, avg_sal
    from
    (
      SELECT employee_id, salary, department_id, 
        round(avg(salary) over (partition by department_id), 2) avg_sal
      from emp
    )
    where salary > avg_sal
    order by avg_sal desc
    

提交回复
热议问题