forked from srikanthpragada/plsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deptname_highest_exp_employee.sql
68 lines (59 loc) · 1.81 KB
/
deptname_highest_exp_employee.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
57
58
59
60
61
62
63
64
65
66
67
68
set serveroutput on
declare
cursor deptcur is
select department_id, department_name
from departments
where department_id in (select department_id from employees)
order by 2;
v_deptid departments.department_id%type;
v_deptname departments.department_name%type;
v_name employees.first_name%type;
begin
open deptcur;
loop
fetch deptcur into v_deptid, v_deptname;
exit when deptcur%notfound;
begin
-- get employee with hightest experience in this dept
select first_name into v_name
from employees
where department_id = v_deptid
and hire_date =
(select min(hire_date) from employees
where department_id = v_deptid );
dbms_output.put_line(v_deptname || ' ' || v_name);
exception
when too_many_rows then
dbms_output.put_line(v_deptname || ' is having more than one employee');
end;
end loop;
close deptcur;
end;
With Cursor For Loop
====================
set serveroutput on
declare
cursor deptcur is
select department_id, department_name
from departments
where department_id in (select department_id from employees)
order by 2;
v_name employees.first_name%type;
begin
for deptrec in deptcur
loop
begin
-- get employee with hightest experience in this dept
select first_name into v_name
from employees
where department_id = deptrec.department_id
and hire_date =
(select min(hire_date) from employees
where department_id = deptrec.department_id );
dbms_output.put_line(deptrec.department_name || ' ' || v_name);
exception
when too_many_rows then
dbms_output.put_line(deptrec.department_name || ' is having more than one employee');
end;
end loop;
end;