forked from srikanthpragada/plsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
displays_dept_details.sql
56 lines (45 loc) · 1.96 KB
/
displays_dept_details.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
set serveroutput on
declare
cursor deptcur is
select department_id, department_name, trunc(avg(salary)) avgsalary
from employees join departments using (department_id)
group by department_id,department_name;
v_name employees.first_name%type;
v_exp number(2);
begin
for deptrec in deptcur
loop
-- get employee with highest salary in the dept
select first_name into v_name
from (
select *
from employees
where salary = (select max(salary) from employees
where department_id = deptrec.department_id )
and department_id = deptrec.department_id
order by employee_id )
where rownum = 1;
-- experience of hod
select trunc(months_between(sysdate, hire_date) / 12) into v_exp
from employees
where employee_id = ( select manager_id from departments
where department_id = deptrec.department_id);
dbms_output.put_line( rpad(deptrec.department_name,30) ||
rpad(v_name,20) ||
rpad(to_char(v_exp),5) ||
to_char(deptrec.avgsalary) );
end loop;
end;
Output
=========
Finance Daniel 14 7483
Shipping Adam 12 3679
Public Relations Hermann 15 10000
Purchasing Den 14 4466
Executive Steven 13 11666
Administration Jennifer 13 4400
Accounting Shelley 15 10154
Human Resources Susan 15 6500
Marketing Michael 13 9500
IT Smith 11 5896
Sales John 12 8714