Department Highest Salary

Write an SQL query to find the department highest salary

Example

Table: Employee                             Table: Department
+----+-------+--------+--------------+      +----+----------+
| Id | Name  | Salary | DepartmentId |      | Id | Name     |
+----+-------+--------+--------------+      +----+----------+
1  | Ram   | 70000  | 1            |      | 1  | IT       |
2  | Ramu  | 90000  | 1            |      | 2  | Sales    |
3  | Henry | 80000  | 2            |      +----+----------+
4  | Sam   | 60000  | 2            |
5  | Geeta | 90000  | 1            |
+----+-------+--------+--------------+
Result: 
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Geeta    | 90000  |
| IT         | Ramu     | 90000  |
| Sales      | Henry    | 80000  |
+------------+----------+--------+

Approach: 

Mysql

Select d.Name as Department , e.Name as Employee , e.Salary as Salary 
from Employee e, Department d
where d.id=e.DepartmentId  and (e.Salary,e.DepartmentId )
in (select max(Salary), DepartmentId  from Employee group by DepartmentId );

Oracle

 Select d.Name as Department , e.Name as Employee , e.Salary as Salary 
 from Employee e, Department d
 where d.id=e.DepartmentId  and (e.Salary,e.DepartmentId )
in (select max(Salary), DepartmentId  from Employee group by DepartmentId );


No comments:

Post a Comment