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

后端 未结 4 791
离开以前
离开以前 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:24

    select *
    from employees e
    join(
          select Round(avg(salary)) AvgSal,department_id,department_name as dept_name
          from employees join departments 
          using (department_id)
          group by department_id,department_name
    ) dd
    using(department_id)
    where e.salary > dd.AvgSal;
    

    another solution

    select * 
    from employees e, 
    (
     select 
        department_id, 
        avg(salary) avg_sal 
     from employees 
     group by department_id
    ) e1
    where e.department_id=e1.department_id 
    and e.salary > e1.avg_sal
    

提交回复
热议问题