Skip to content
Senior209 views

How to display all departments including Accounting? What will be in max_salary for Accounting?

Practice

Asked at

AI answer

Use LEFT JOIN to retain departments even if they have no employees:

SELECT d.name, MAX(e.salary) AS max_salary
FROM departments d
LEFT JOIN employees e ON e.department_id = d.id
GROUP BY d.id, d.name;

For the “Accounting” department with no employees, MAX(e.salary) will return NULL, not 0: the aggregate ignores NULL, and there is no salary value for rows with no matches.

Also asked at СБЕР

Similar questions