Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions salary_stats.sql
Original file line number Diff line number Diff line change
@@ -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);
Expand Down