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
Syntax
percent_rank() OVER (PARTITION BY ... ORDER BY ...)
Return value
A real number from 0.0 to 1.0.
Examples
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
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
| Version | Change |
|---|---|
| SQLite 3.25.0 | Added |
In other languages
PERCENT_RANK()· PostgreSQL
Related
CUME_DIST()· Return the cumulative distribution: the share of rows ordered at or before the current row, from just above 0 to 1.RANK()· Rank rows by the window's ORDER BY, giving ties the same rank and leaving gaps after them (1, 2, 2, 4).- SQL Window Functions · tutorial