PERCENT_RANK

Applies to: ✅ Data 360 SQL ✅ Tableau Hyper API

Returns the relative rank of the current row within its partition, as a value between 0 and 1.

Syntax 

1percent_rank() OVER ( [ PARTITION BY <expression> [, ...] ] ORDER BY <expression> [, ...] )

Arguments 

percent_rank takes no arguments. It depends on the ORDER BY clause within OVER to determine row order. For full details on the OVER clause and window frames, see Window Functions. For details on naming a window definition with a WINDOW clause, see SELECT.

Returns 

Returns a double precision value calculated as (rank - 1) / (total partition rows - 1). percent_rank computes the fraction of partition rows that are less than the current row under the assumption that the current row doesn’t exist in the partition.

Considerations 

  • Requires ORDER BY in the OVER clause. Without ORDER BY, all rows are treated as peers and the result isn’t meaningful.
  • The result range is 0 to 1. The row with the lowest ORDER BY value in each partition (or one of the tied lowest rows) gets a percent_rank of 0.
  • Rows with equal ORDER BY values are peers and receive the same percent_rank.

Examples 

Percent Rank by Group 

Calculate each employee’s relative salary rank within their department.

1SELECT depname, empno, salary, percent_rank() OVER (PARTITION BY depname ORDER BY salary)
2FROM empsalary;

The lowest-salaried employee in each department returns a percent_rank of 0, and the highest-salaried employee returns a percent_rank of 1 (or a value close to 1 when there are ties).

Percent Rank Across All Rows 

Omit PARTITION BY to calculate the relative rank across the entire result set.

1SELECT empno, salary, percent_rank() OVER (ORDER BY salary)
2FROM empsalary;

Related Documentation