diff --git a/salary_stats.sql b/salary_stats.sql index 373620e..fa3e124 100644 --- a/salary_stats.sql +++ b/salary_stats.sql @@ -1,25 +1,19 @@ -- Display no. of employees with salary > avg, < avg and = avg set serveroutput on declare - v_avg_salary employees.salary%type; v_high number(2); v_same number(2); v_low number(2); begin - select avg(salary) into v_avg_salary - from employees; - select count(*) into v_high - from employees - where salary > v_avg_salary; - - select count(*) into v_same - from employees - where salary = v_avg_salary; - - select count(*) into v_low - from employees - where salary < v_avg_salary; + select count(case when salary > avg_salary then 1 end), --more than average + count(case when salary = avg_salary then 1 end), --the same as average + count(case when salary < avg_salary then 1 end), --less than average + into v_high, v_same, v_low + from ( + select salary, avg(salary) over (partition by 1) avg_salary + from employees + ); dbms_output.put_line('Higher than avg : ' || v_high); dbms_output.put_line('Same as avg : ' || v_same);