Sobes.tech
Senior

-- There are tables with the 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, COALESCE(MAX(e.salary), 0) from Department d left join Employee e on e.dep_id = d.id group by d.name

sobes.tech AI

Answer from AI

The query that returns a list of all department names with the maximum salary in each department can be written as:

SELECT d.name, COALESCE(MAX(e.salary), 0) AS max_salary
FROM Department d
LEFT JOIN Employee e ON e.dep_id = d.id
GROUP BY d.name

Explanation:

  • Using LEFT JOIN to include all departments, even if there are no employees.
  • The MAX(e.salary) function finds the highest salary in the department.
  • COALESCE replaces NULL (if there are no employees) with 0.
  • Grouping by department name.

This corresponds to the query provided in the question and correctly solves the task.

-- There are tables with the structure Employee id… - sobes.tech