209 views
How to display all departments including Accounting? What will be in max_salary for Accounting?
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 СБЕР
- How was SQL query optimization carried out? What motivates query optimization and how is it done?
- What is the @Transactional annotation and how does it work under the hood?
- What is the application of linked list arrays in the implementation of the HashMap data structure?
- What scope levels exist in the Spring Framework?
- How are the equals and hashCode methods related in Java, and why is it important to implement them correctly together?
Similar questions
- -- 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.
- -- 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, MAX(e.salary) AS max_salary FROM Department d JOIN Employee e ON e.dep_id = d.id GROUP BY d.id Employee id | name | salary | dep_id 1 | Ivanov | 90 | 1 2 | Petrov | 150 | 2 3 | Sidorov| 110 | 1 4 | Kuznetsov| 150 | 2 5 | Sokolov| 80 | 2 Department id | name 1 | AHO 2 | IT 3 | Accounting
- The company LLC "Romashka" is represented by the tables employee(id, department_id, name, salary) and department (id, name). List the departments where the difference between the maximum and minimum employee salaries is more than 50 thousand.
- [name] asked: What are your salary expectations?
- [name] asked: What salary are you considering?