if I run the code like that I get the result I need, but I also need to add the name column, and once I add it, the result changes
select department_id, max(salary)
from employees e1
where salary <
(select max(salary)
from employees e2
where e2.department_id=e1.department_id)
group by department_id
order by department_id;
I don't have your tables so I'll use Scott's
EMP. This is its contents:This is what you don't want:
OK, let's correlate some subqueries, then. Return employees whose salary is
So: