Wednesday, June 29, 2016

Top 10 employees based on their salary

Get the top 10 employees based on their salary


SELECT ename, sal 
  FROM ( SELECT ename, sal, RANK() OVER (ORDER BY sal DESC) sal_rank
           FROM emp ) 
 WHERE sal_rank <= 10;

Select the employees making the top 10 salaries


SELECT ename, sal 
  FROM ( SELECT ename, sal, DENSE_RANK() OVER (ORDER BY sal DESC) sal_dense_rank
           FROM emp ) 
 WHERE sal_dense_rank <= 10;
For Oracle 8i and above, one can get the Top N rows using an inner-query with an ORDER BY clause:
SELECT *
  FROM (SELECT * FROM my_table ORDER BY col_name_1 DESC)
 WHERE ROWNUM < 10;

No comments:

Post a Comment