How to find Top 5 Salaries
Three ways we can do it
To find top 5 salaries:
Method-1
WITH RANK_TBL AS(SELECT LASTNAME, SALARY,RANK() OVER (ORDER BY SALARY DESC) AS RANK_NUM FROM EMP)
SELECT * FROM RANK_TBL
WHERE RANK_NUM < 6;
What is Rank Function?
The RANK function orders and ranks a result. In this example, the result in being ranked by SALARY in descending sequence, so the highest salary has a rank of 1. When there is a tie, all rows with the value receive the same rank and an appropriate number of ranks are "skipped". In this example, since there were 2 salary values in second place, the value of 3 is skipped.
The result set may be ordered using an ORDER BY for the entire SELECT, and this order need not be the same as the column being ranked. When this is done, 2 sorts may need to be performed to achieve the desired result
Method-2
WITH RANK_TBL AS(SELECT LASTNAME, SALARY,DENSE_RANK() OVER (ORDER BY SALARY DESC) AS RANK_NUM FROM EMP)
SELECT * FROM RANK_TBL
WHERE RANK_NUM < 6;
What is dense rank ?
The DENSE_RANK function orders and ranks a result. In this example, the result in being ranked by SALARY in descending sequence, so the highest salary has a rank of 1. When there is a tie, all rows with the value receive the same rank and no ranks are "skipped". In this example, there were 2 salary values for first, then the next value becomes rank = 2.
The result set may be ordered using an ORDER BY for the entire SELECT, and this order need not be the same as the column being ranked. When this is done, 2 sorts may need to be performed to achieve the desired result.
Method-3
Quota Queries
SELECT LASTNAME, SALARYFROM EMP E
WHERE 5 > (SELECT COUNT(*)FROM EMP E1WHERE E1.SALARY > E.SALARY)ORDER BY SALARY DESC
What is Quota Query?
Quota query is the name given to the general class of problems seeking to return a ranked list of the highest or lowest values of some column or columns.
To find top 5 salaries:
Method-1
WITH RANK_TBL AS(SELECT LASTNAME, SALARY,RANK() OVER (ORDER BY SALARY DESC) AS RANK_NUM FROM EMP)
SELECT * FROM RANK_TBL
WHERE RANK_NUM < 6;
What is Rank Function?
The RANK function orders and ranks a result. In this example, the result in being ranked by SALARY in descending sequence, so the highest salary has a rank of 1. When there is a tie, all rows with the value receive the same rank and an appropriate number of ranks are "skipped". In this example, since there were 2 salary values in second place, the value of 3 is skipped.
The result set may be ordered using an ORDER BY for the entire SELECT, and this order need not be the same as the column being ranked. When this is done, 2 sorts may need to be performed to achieve the desired result
Method-2
WITH RANK_TBL AS(SELECT LASTNAME, SALARY,DENSE_RANK() OVER (ORDER BY SALARY DESC) AS RANK_NUM FROM EMP)
SELECT * FROM RANK_TBL
WHERE RANK_NUM < 6;
What is dense rank ?
The DENSE_RANK function orders and ranks a result. In this example, the result in being ranked by SALARY in descending sequence, so the highest salary has a rank of 1. When there is a tie, all rows with the value receive the same rank and no ranks are "skipped". In this example, there were 2 salary values for first, then the next value becomes rank = 2.
The result set may be ordered using an ORDER BY for the entire SELECT, and this order need not be the same as the column being ranked. When this is done, 2 sorts may need to be performed to achieve the desired result.
Method-3
Quota Queries
SELECT LASTNAME, SALARYFROM EMP E
WHERE 5 > (SELECT COUNT(*)FROM EMP E1WHERE E1.SALARY > E.SALARY)ORDER BY SALARY DESC
What is Quota Query?
Quota query is the name given to the general class of problems seeking to return a ranked list of the highest or lowest values of some column or columns.
Comments
Post a Comment