Reputation: 16545
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
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
Reputation: 684
You should write something like this.
SELECT TOP 2 EmpID,Salary,Name FROM Employee ORDER BY Salary
Upvotes: 0
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
Reputation: 5335
Try this ..
SELECT * from Employee order by Salary desc limit 2 ;
Upvotes: 1