I have a Table with rows like one below

Image

I want to have Highest paid employee in Each Department

EmpName      Designation         Salary
Phillipe           Analyst                    3150
Scott              Clerk                        2500
Mac                Manager                  5000

Query 1(Oracle Specific)

SELECT EmpName, Designation, Salary
  FROM (SELECT EmpName, Designation, Salary,
               RANK() OVER (PARTITION BY Designation ORDER BY Salary DESC) AS rn
          FROM Employees)
 WHERE rn = 1
 ORDER BY Designation;

Query 2

 SELECT EmpName, Designation, Salary
   FROM Employees
  WHERE (Designation, Salary) IN (SELECT Designation, MAX(Salary)
                                   FROM Employees
                                  GROUP BY Designation)
 ORDER BY Designation;

Query 3(Oracle Specific)

SELECT max(EmpName) keep (dense_rank last order by salary) AS EmpName,
       Designation, max(Salary) keep (dense_rank last order by salary)  AS Salary
  FROM Employees
 GROUP BY Designation;

Query 4(Oracle Specific)

SELECT max(EmpName) keep (dense_rank last order by salary) AS EmpName,
       Designation, max(Salary) keep (dense_rank last order by salary)  AS Salary
  FROM Employees
 GROUP BY Designation;

Leave a reply