Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a table called Employee with the following fields:

  • EmpID
  • Salary
  • Name

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

like image 416
Embedd_0913 Avatar asked Feb 10 '26 16:02

Embedd_0913


2 Answers

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
  • Oracle: ROW_NUMBER vs ROWNUM
like image 169
OMG Ponies Avatar answered Feb 13 '26 09:02

OMG Ponies


Try this ..

SELECT * from Employee  order by Salary  desc limit 2 ;
like image 35
Pavunkumar Avatar answered Feb 13 '26 09:02

Pavunkumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!