-- 1 вариант
select
DEPARTMENT_ID,
max(case when rnk = 1 then LAST_NAME else null end) as highest_paid,
max(case when rnk = 2 then LAST_NAME else null end) as second_paid,
max(case when rnk = 3 then LAST_NAME else null end) as third_highest
from (
select
DEPARTMENT_ID,
LAST_NAME,
dense_rank() over (partition by DEPARTMENT_ID order by salary desc) rnk
from employees
order by DEPARTMENT_ID
)t
where rnk <= 3
group by DEPARTMENT_ID
order by DEPARTMENT_ID;
-- 2 вариант
select
DEPARTMENT_ID,
listagg(case when rnk = 1 then LAST_NAME else null end, ', ') within group (order by LAST_NAME) as highest_paid,
listagg(case when rnk = 2 then LAST_NAME else null end, ', ') within group (order by LAST_NAME) as second_paid,
listagg(case when rnk = 3 then LAST_NAME else null end, ', ') within group (order by LAST_NAME) as third_highest
from (
select
DEPARTMENT_ID,
LAST_NAME,
dense_rank() over (partition by DEPARTMENT_ID order by salary desc) rnk
from employees
order by DEPARTMENT_ID
)t
where rnk <= 3
group by DEPARTMENT_ID
order by DEPARTMENT_ID