Embedd_0913
Embedd_0913

Reputation: 16545

Query for retrieving top two rows from a table in SQL Server

I have a table called Employee with the following fields:

I want to get top two employees with maximum salary. How do I write this query ?

Upvotes: 3

Views: 6986

Answers (5)

Thomas
Thomas

Reputation: 64635

Yet another solution:

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

Upvotes: 0

wonde
wonde

Reputation: 684

You should write something like this.

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

Upvotes: 0

OMG Ponies
OMG Ponies

Reputation: 332521

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

Oracle:

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

Upvotes: 4

uvita
uvita

Reputation: 4124

SELECT TOP 2 * FROM Employee ORDER BY Salary DESC;

Upvotes: 0

Pavunkumar
Pavunkumar

Reputation: 5335

Try this ..

SELECT * from Employee  order by Salary  desc limit 2 ;

Upvotes: 1

Related Questions