sajad torkamani

In a nutshell

The DENSE_RANK() window function assigns a unique rank to each district row within a result set.

Example

Given this students table:

namescore
Alice90
Bob85
Charlie85
David80

This query:

SELECT 
  name,
  score,
  DENSE_RANK() OVER (ORDER BY score DESC) as `rank`
FROM students;

Will produce this result:

namescorerank
Alice901
Bob852
Charlie852
David803

The order of ranking is determined by the ORDER BY cause within the DENSE_RANK() function.

Other notes

  • Unlike RANK(), DENSE_RANK() will give duplicate rows the same rank and not skip any rankings.
Tagged: MySQL