查询从SQL Server中的表中检索前两行

时间:2010-03-15 04:08:09

标签: sql sql-server tsql

我有一个名为Employee的表,其中包含以下字段:

  • 的EmpID
  • 薪酬
  • 名称

我希望获得薪水最高的前两名员工。我该如何撰写此查询?

5 个答案:

答案 0 :(得分:4)

SQL Server 2000 +:

  SELECT TOP 2
         e.*
    FROM EMPLOYEE e
ORDER BY e.salary DESC

MySQL& Postgres的:

  SELECT e.*
    FROM EMPLOYEE e
ORDER BY e.salary DESC
   LIMIT 2

甲骨文:

SELECT x.*
  FROM (SELECT e.*,
               ROWNUM as rn
          FROM EMPLOYEE e
      ORDER BY e.salary DESC) x
 WHERE x.rn <= 2

答案 1 :(得分:1)

试试这个..

SELECT * from Employee  order by Salary  desc limit 2 ;

答案 2 :(得分:0)

SELECT TO 2 * FROM Employee ORDER BY Salary DESC;

答案 3 :(得分:0)

你应该写这样的东西。

SELECT TOP 2 EmpID,Salary,Name from Employee ORDER BY Salary

答案 4 :(得分:0)

又一个解决方案:

With NumberedItems As
    (
    Select EmpId, Salary, Name
        , Row_Number() Over ( Order By Salary Desc ) As SalaryRank 
    From Employee
    )
Select EmpId, Salary, Name
From NumberedItems
Where SalaryRank <= 2