10.6 Ranking Functions

The ranking functions compute the ordinal rank of a row within the window partition.

These functions can be used with or without partitioning and ordering. However, using them without ordering almost never makes sense.

The ranking functions can be used to create different type of incremental counters. Consider SUM(1) OVER (ORDER BY SALARY) as an example of what they can do, each of them differently. Following is an example query, also comparing with the SUM behavior.

  1. select
  2. id,
  3. salary,
  4. dense_rank() over (order by salary),
  5. rank() over (order by salary),
  6. row_number() over (order by salary),
  7. sum(1) over (order by salary)
  8. from employee
  9. order by salary;

Results

  1. id salary dense_rank rank row_number sum
  2. -- ------ ---------- ---- ---------- ---
  3. 3 8.00 1 1 1 1
  4. 4 9.00 2 2 2 2
  5. 1 10.00 3 3 3 4
  6. 5 10.00 3 3 4 4
  7. 2 12.00 4 5 5 5

The difference between DENSE_RANK and RANK is that there is a gap related to duplicate rows (relative to the window ordering) only in RANK. DENSE_RANK continues assigning sequential numbers after the duplicate salary. On the other hand, ROW_NUMBER always assigns sequential numbers, even when there are duplicate values.

10.6.1 CUME_DIST()

Available inDSQL, PSQL

Result typeDOUBLE PRECISION

Syntax

  1. CUME_DIST () OVER <window_name_or_spec>

The distribution function CUME_DIST computes the relative rank of a row within a window partition. CUME_DIST is calculated as the number of rows preceding or peer of the current row divided by the number of rows in the partition.

In other words, CUME_DIST() OVER <window_name_or_spec> is equivalent to COUNT(*) OVER <window_name_or_spec> / COUNT(*) OVER()

10.6.1.1 CUME_DIST Examples

  1. select
  2. id,
  3. salary,
  4. cume_dist() over (order by salary)
  5. from employee
  6. order by salary;

Result

  1. id salary cume_dist
  2. -- ------ ---------
  3. 3 8.00 0.2
  4. 4 9.00 0.4
  5. 1 10.00 0.8
  6. 5 10.00 0.8
  7. 2 12.00 1

10.6.2 DENSE_RANK()

Available inDSQL, PSQL

Result typeBIGINT

Syntax

  1. DENSE_RANK () OVER <window_name_or_spec>

Returns the rank of rows in a partition of a result set without ranking gaps. Rows with the same window_order values get the same rank within the partition window_partition, if specified. The dense rank of a row is equal to the number of different rank values in the partition preceding the current row, plus one.

10.6.2.1 DENSE_RANK Examples

  1. select
  2. id,
  3. salary,
  4. dense_rank() over (order by salary)
  5. from employee
  6. order by salary;

Result

  1. id salary dense_rank
  2. -- ------ ----------
  3. 3 8.00 1
  4. 4 9.00 2
  5. 1 10.00 3
  6. 5 10.00 3
  7. 2 12.00 4

10.6.3 NTILE()

Available inDSQL, PSQL

Result typeBIGINT

Syntax

  1. NTILE ( number_of_tiles ) OVER <window_name_or_spec>

Table 10.6.3.1 Arguments of NTILE

ArgumentDescription

number_of_tiles

Number of tiles (groups). Restricted to a positive integer literal, a named parameter (PSQL), or a positional parameter (DSQL).

NTILE distributes the rows of the current window partition into the specified number of tiles (groups).

10.6.3.1 NTILE Examples

  1. select
  2. id,
  3. salary,
  4. rank() over (order by salary),
  5. ntile(3) over (order by salary)
  6. from employee
  7. order by salary;

Result

  1. ID SALARY RANK NTILE
  2. == ====== ==== =====
  3. 3 8.00 1 1
  4. 4 9.00 2 1
  5. 1 10.00 3 2
  6. 5 10.00 3 2
  7. 2 12.00 5 3

10.6.4 PERCENT_RANK()

Available inDSQL, PSQL

Result typeDOUBLE PRECISION

Syntax

  1. PERCENT_RANK () OVER <window_name_or_spec>

The distribution function PERCENT_RANK computes the relative rank of a row within a window partition. PERCENT_RANK is calculated as the Section 10.6.5, RANK() minus 1 of the current row divided by the number of rows in the partition minus 1.

In other words, PERCENT_RANK() OVER <window_name_or_spec> is equivalent to (RANK() OVER <window_name_or_spec> - 1) / CAST(COUNT(*) OVER() - 1 AS DOUBLE PRECISION)

10.6.4.1 PERCENT_RANK Examples

  1. select
  2. id,
  3. salary,
  4. rank() over (order by salary),
  5. percent_rank() over (order by salary)
  6. from employee
  7. order by salary;

Result

  1. id salary rank percent_rank
  2. -- ------ ---- ------------
  3. 3 8.00 1 0
  4. 4 9.00 2 0.25
  5. 1 10.00 3 0.5
  6. 5 10.00 3 0.5
  7. 2 12.00 5 1

10.6.5 RANK()

Available inDSQL, PSQL

Result typeBIGINT

Syntax

  1. RANK () OVER <window_name_or_spec>

Returns the rank of each row in a partition of the result set. Rows with the same values of window-order get the same rank with in the partition _window-partition, if specified. The rank of a row is equal to the number of rank values in the partition preceding the current row, plus one.

10.6.5.1 RANK Examples

  1. select
  2. id,
  3. salary,
  4. rank() over (order by salary)
  5. from employee
  6. order by salary;

Result

  1. id salary rank
  2. -- ------ ----
  3. 3 8.00 1
  4. 4 9.00 2
  5. 1 10.00 3
  6. 5 10.00 3
  7. 2 12.00 5

See alsoSection 10.6.2, DENSE_RANK(), Section 10.6.6, ROW_NUMBER()

10.6.6 ROW_NUMBER()

Available inDSQL, PSQL

Result typeBIGINT

Syntax

  1. ROW_NUMBER () OVER <window_name_or_spec>

Returns the sequential row number in the partition of the result set, where 1 is the first row in each of the partitions.

10.6.6.1 ROW_NUMBER Examples

  1. select
  2. id,
  3. salary,
  4. row_number() over (order by salary)
  5. from employee
  6. order by salary;

Result

  1. id salary rank
  2. -- ------ ----
  3. 3 8.00 1
  4. 4 9.00 2
  5. 1 10.00 3
  6. 5 10.00 4
  7. 2 12.00 5

See alsoSection 10.6.2, DENSE_RANK(), Section 10.6.5, RANK()