Skip to main content
Version: V1.0.0

DENSE_RANK

Syntax

DENSE_RANK() OVER([ query_partition_clause ] order_by_clause)

Description

DENSE_RANK calculates the rank of each row in a specified column, which is determined by the order_by_clause.

The rank is a continuous integer. For rows with the same rank, the rank value is the same.

Examples

CREATE TABLE employees(LAST_NAME CHAR(10), SALARY DECIMAL, JOB_ID CHAR(32));
INSERT INTO employees VALUES('JIM', 2000, 'CLEANER');
INSERT INTO employees VALUES('MIKE', 12000, 'ENGINEERING');
INSERT INTO employees VALUES('LILY', 13000, 'ENGINEERING');
INSERT INTO employees VALUES('IRIS', 11000, 'ENGINEERING');
INSERT INTO employees VALUES('TOM', 11000, 'ENGINEERING');
COMMIT;
Query OK, 0 rows affected (0.000 sec)

SELECT LAST_NAME, RANK() OVER(PARTITION BY JOB_ID ORDER BY SALARY) RANK, DENSE_RANK() OVER(PARTITION BY JOB_ID ORDER BY SALARY) DENSE_RANK, PERCENT_RANK() OVER(PARTITION BY JOB_ID ORDER BY SALARY) PERCENT_RANK FROM employees;
+-----------+------+------------+----------------------------------+
| LAST_NAME | RANK | DENSE_RANK | PERCENT_RANK |
+-----------+------+------------+----------------------------------+
| JIM | 1 | 1 | 0.000000000000000000000000000000 |
| TOM | 1 | 1 | 0.000000000000000000000000000000 |
| IRIS | 1 | 1 | 0.000000000000000000000000000000 |
| MIKE | 3 | 2 | 0.666666666666666666666666666667 |
| LILY | 4 | 3 | 1.000000000000000000000000000000 |
+-----------+------+------------+----------------------------------+
5 rows in set (0.002 sec)