想要在 SQL 服务器 table 中打印第三高的薪水和名字吗?

Want to print 3rd highest salary and name along with it in SQL Server table?

CREATE TABLE tbl_Employees  
(  
     Id int PRIMARY KEY NOT NULL IDENTITY(1,1),  
     FirstName varchar(50),  
     LastName varchar(20),  
     Location varchar(20),  
     Gender varchar(50),  
     Salary int,
     Age int
)  

INSERT INTO tbl_Employees VALUES ('Chittaranjan', 'Swain','Odisha', 'Male', 80000, 23)  
INSERT INTO tbl_Employees VALUES ('Chandin', 'Swain', 'Pune','Female', 76000, 34)  
INSERT INTO tbl_Employees VALUES ('Mitu', 'Pradhan','Delhi', 'Male', 55000, 20)  
INSERT INTO tbl_Employees VALUES ('Jeni', 'Swain','Chennai', 'Female', 76000, 34)  
INSERT INTO tbl_Employees VALUES ('Adyashree', 'Swain','UK', 'Female', 49000, 36)  
INSERT INTO tbl_Employees VALUES ('Ram', 'Kumar','US', 'Male', 39000, 45)  
INSERT INTO tbl_Employees VALUES ('Jitendra', 'Gouad','Hydrabad', 'Male', 35000, 35)  
INSERT INTO tbl_Employees VALUES ('Dibas', 'Hembram','Bangalore', 'Male', 55000, 56)  

我想找出第3高的薪水并打印出来。但除此之外,我还想打印那个人的名字

SELECT 
    MIN(Salary) AS minimum
FROM
    (SELECT DISTINCT TOP 3 Salary
     FROM tbl_Employees
     ORDER BY Salary DESC) AS a

到现在为止我只能找出第三高的薪水,但我也想要那个人的名字。

window 函数 row_number() over() 应该可以解决问题

注意:如果要查看平局,请使用 dense_rank()

Select *
 From  (
         Select *
               ,RN = row_number() over (order by salary desc)
             From tbl_Employees  
       ) A
 Where RN = 3

OFFSET这里好像是你想要的:

SELECT FirstName,
       LastName,
       Salary
FROM dbo.tbl_Employees E
ORDER BY Salary DESC
OFFSET 2 ROWS FETCH NEXT 1 ROW ONLY;

DENSE_RANK() 函数在没有 GAP 的情况下序列化位置。如果两个值在同一位置,则此值之后的第三个值位置将为 2。但如果使用 RANK() 函数,它将为 3。因此会产生间隙。

-- SQL Server
SELECT *
FROM (SELECT *
           , DENSE_RANK() OVER (ORDER BY salary DESC) sl_no
      FROM tbl_Employees) t
WHERE t.sl_no = 3 

请找这个urlhttps://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=25e3f32ddb04691d99518251f5b074c7