Senior
-- There are tables with the following structure Employee id (PK) name salary dep_id(FK department.id) Department id (PK) name -- write a query that returns a list of all department names with the maximum salary in each department select d.name, MAX(e.salary) from Employee e join Department d on e.dep_id = d.id group by d.name
sobes.tech AI
Answer from AI
The query you provided correctly returns a list of departments with the maximum salary in each:
select d.name, MAX(e.salary) as max_salary
from Employee e
join Department d on e.dep_id = d.id
group by d.name
If you also want to include departments with no employees, you should use LEFT JOIN:
select d.name, MAX(e.salary) as max_salary
from Department d
left join Employee e on e.dep_id = d.id
group by d.name
In this case, for departments without employees, max_salary will be NULL.