sql - Two aggregation functions group by -


i'm trying print department names have sum of salaries bigger average sum on departments.

enter image description here

select d.department_name, sum(e.salary) departments d, employees e d.department_id = e.department_id group d.department_name having sum(e.salary) > (select avg(sum(salary)) employees); 

in second select, after have group avg(sum(salary))?

you need repeat first query in condition. can done with clause.

with dept_sums (select d.department_name, sum(e.salary) sum_salary departments d, employees e d.department_id = e.department_id group d.department_name)  select * dept_sums d_s_1 d_s_1.sum_salary > (select avg(sum_salary) dept_sums d_s_2);