How to get 1st, 2nd, 3rd, 4th, nth topmost salary from an Employee table
The following solution is for getting 6th highest salary from Employee table ,
SELECT TOP1 salary
FROM(
SELECT DISTINCT TOP6 salary
FROMemployee
ORDER BYsalaryDESC)a
ORDER BYsalary
You can change and use it for getting nth highest salary from Employee table as follows
SELECT TOP1 salary
FROM(
SELECT DISTINCT TOPn salary
FROMemployee
ORDER BYsalaryDESC)a
ORDER BYsalary
where n > 1 (n is always greater than one)
Same example converted in SQL Server 2005 to work with Database AdventureWorks.
USEAdventureWorks;
GOSELECT TOP1 Rate
FROM(
SELECT DISTINCT TOP4 Rate
FROMHumanResources.EmployeePayHistory
ORDER BYRateDESC)A
ORDER BYRate
GO
本文介绍了一种使用SQL查询特定排名薪资的方法。通过构建特定的SQL语句,可以有效地从Employee表中获取如第6高或第n高的薪资。文中还提供了一个在SQL Server 2005环境下针对AdventureWorks数据库的具体示例。

被折叠的 条评论
为什么被折叠?



