Find top Nth employee from each department in terms of
salary?

Answer Posted / gopi muluka

Execute below query against Adventureworks database

WITH CTE AS(
SELECT D.Name AS Department,E.EmployeeID,(RANK() OVER
(PARTITION BY D.Name ORDER BY MAX(Rate) DESC)) AS
EmployeeRank, MAX(Rate) AS HourlyRate
FROM [AdventureWorks].[HumanResources].[Employee] E
INNER JOIN HumanResources.EmployeePayHistory PH
ON E.[EmployeeID]=PH.[EmployeeID]
INNER JOIN HumanResources.EmployeeDepartmentHistory DH
ON E.EmployeeID=DH.EmployeeID
AND DH.EndDate IS NULL
INNER JOIN HumanResources.Department D
ON DH.DepartmentID=D.DepartmentID
GROUP BY D.Name,E.EmployeeID
)
SELECT * FROM CTE WHERE EmployeeRank=2
ORDER BY Department,EmployeeRank,EmployeeID

Is This Answer Correct ?    1 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What samples and sample databases are provided by microsoft?

581


What functions can a view be used to performed?

630


What is sql server database?

506


What are the different types of upgrades that can be performed in sql server?

697


What are the three different part of rdl file explain them?

172






Do you know what are the differences between lost updates and uncommitted dependencies?

555


Can you insert NULL in unique column?

642


What is the difference between update lock and exclusive lock?

504


What is the default Port No on which SQL Server listens?

634


How to create a store procedure with encryption?

529


How to scale out a federation by Sql statement?

92


What is the optimization being performed in oracle and SQL Server?

637


What stored by the tempdb ? : sql server database administration

573


What is dirty read?

635


Explain transaction server consistency?

532