Problem
The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id.
1 | |
The Department table holds all departments of the company.
1 | |
Write a SQL query to find employees who have the highest salary in each of the departments. For the above tables, your SQL query should return the following rows (order of rows does not matter).
1 | |
Explanation:
Max and Jim both have the highest salary in the IT department and Henry has the highest salary in the Sales department.
Explanation
-
We can
LEFT JOINtwo tables. -
In a subquery, use
MAXandGROUP BY Employee.DepartmentIdto get the maximum salary and DepartmentId, then first table’s salary and second table’sDepartment.idINthat subquery’s max salary and DepartmentId. -
Select all the required columns from that first table.
Solution
1 | |