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
what is the different types of backups available in sql server? : Sql server database administration
Explain various data region available in ssrs with their use?
Can we add our custom code in ssis?
Do you think BCNF is better than 2NF & 3NF? Why?
Can you roll back the ddl statement in a trigger?
how can you check the level of fragmentation on a table? : Sql server administration
What is the full form of ddl?
How many types of local tables are there in sql server?
New concepts of sql server 2005 use in your project.
What does top operator do?
what is the difference between delete table and truncate table commands? : Sql server database administration
What does it mean if @@cursor_row returns a negative number?
Data is not being delivered to subscribers, what can be the possible reasons? : sql server replication
What is a data source or ds? : sql server analysis services, ssas
What happens if you are trying to access a schema not owned by you?