Search
Write a publication
Pull to refresh
0
0
Send message

Больше нравится вариант с оконной функцией, компактно получилось)

/* 1 */
select *
from public.employees
where salary in (
    select max(salary)
    from public.employees
    group by department_id
);

/* 2 */
select *
from (
    select *, dense_rank() over(partition by department_id order by salary) as rank
    from public.employees
) as t
where t.rank = 1;

/* 3 */
select *
from public.employees as e
inner join (
    select department_id, max(salary) as max_salary
    from public.employees
    group by department_id
) as d on d.department_id = e.department_id and d.max_salary = e.salary;

Больше нравится вариант с оконной функцией, компактно получилось)

/* 1 */
select *
from public.employees
where salary in (
    select max(salary)
    from public.employees
    group by department_id
);

/* 2 */
select *
from (
    select *, dense_rank() over(partition by department_id order by salary) as rank
    from public.employees
) as t
where t.rank = 1;

/* 3 */
select *
from public.employees as e
inner join (
    select department_id, max(salary) as max_salary
    from public.employees
    group by department_id
) as d on d.department_id = e.department_id and d.max_salary = e.salary;

DataGrip пожалуй лучший инструмент для работы с базами

Information

Rating
Does not participate
Registered
Activity