SQLite 3 reference · Window Functions

SQLite PERCENT_RANK() Function

Return a row's relative rank from 0 to 1: (rank - 1) / (rows in partition - 1).

SQLite 3 Since 3.25.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

percent_rank() OVER (PARTITION BY ... ORDER BY ...)

Return value

A real number from 0.0 to 1.0.

Examples

Relative positionTry it yourself
SELECT name, salary, round(percent_rank() OVER (ORDER BY salary), 2) AS pct
FROM employees
WHERE department = 'Engineering';

Output

name | salary | pct
Lucas Martin | 112000 | 0
Priya Nair | 115000 | 0.25
Jonas Berg | 140000 | 0.5
Mei Chen | 165000 | 0.75
Omar Haddad | 210000 | 1
Within a categoryTry it yourself
SELECT name, price, round(percent_rank() OVER (ORDER BY price), 2) AS cheaper_than_share
FROM products
WHERE category_id = 2;

Output

name | price | cheaper_than_share
Wireless Mouse | 24.99 | 0
USB-C Hub | 39.95 | 0.33
Mechanical Keyboard | 89 | 0.67
4K Monitor | 329 | 1

Version history

VersionChange
SQLite 3.25.0Added

In other languages

Was this page helpful?