DENSE_RANK() Function

The DENSE_RANK() function operates similarly to RANK(), but it differs in how it handles ties. Specifically, DENSE_RANK() does not leave gaps in the ranking sequence. If two employees have the same salary, they will receive the same rank, and the next employee in line will receive the subsequent rank, effectively creating a contiguous sequence. For example:

sqlCopy codeSELECT employee_id, salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS dense_salary_rank FROM employees;

In this query, the result will show a continuous ranking of employees based on their salaries. If two employees are tied for the highest salary, they both receive rank 1, and the next highest salary receives rank 2. This function is beneficial in situations where a continuous ranking is preferred, such as in competitions or when evaluating performance metrics without gaps.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *