tags:

views:

290

answers:

4

how to find top three highest salary in emp table in oracle(sql)

+2  A: 

Something like the following should do it.

SELECT  Name, Salary
FROM 
    (
    SELECT  Name, Salary
    FROM         emp 
    ORDER BY Salary desc
    )
WHERE rownum <= 3
ORDER BY Salary ;
Martin Smith
Simple answer to a stupid question.
amphetamachine
+1  A: 

How about marking this as homework?

Christian Severin
-1: this should be a comment.
dbemerlin
Would homework be specific to a particular RDBMS or would it demand portable SQL?
Martin Smith
@dbemerlin He can't comment until he gets 50 rep!
Martin Smith
Actually just seen the OP's last 3 questions and it does seem like homework day!
Martin Smith
@dbemerlin: You are correct, this should be a comment. Now if you would kindly show me how I can add a comment to another's question with a reputation of less than 50, I'd be much obliged.
Christian Severin
A: 

SELECT *FROM ( SELECT *FROM emp ORDER BY Salary desc ) WHERE rownum <= 3 ORDER BY Salary ;

Aamir
+3  A: 

You can try.

   SELECT * FROM 
     (
      SELECT EMPLOYEE, LAST_NAME, SALARY,
      RANK() OVER (ORDER BY SALARY DESC) EMPRANK
      FROM emp
     )
    WHERE emprank <= 3;

This will give correct output even if there are two employees with same maximun salary

Bharat