PostgreSQL - PERCENT_RANK Function
Last Updated :
01 Aug, 2024
In PostgreSQL, the PERCENT_RANK() function is used to evaluate the relative ranking of a value within a given set of values. This function is particularly useful for statistical analysis and reporting, providing insights into how values compare within a dataset.
From this article, we can better understand the PERCENT_RANK Function in PostgreSQL.
Syntax
The syntax of the PERCENT_RANK() function:
PERCENT_RANK() OVER (
[PARTITION BY partition_expression, ... ]
ORDER BY sort_expression [ASC | DESC], ...
)
Parameters
Let's analyze the above syntax:
- PARTITION BY: The PARTITION BY is an optional clause used with the PERCENT_RANK() function to divide rows into multiple partitions. It defaults to a single set.
- ORDER BY: The ORDER BY clause is used to set the order in which the resultant query is returned.
Return Value
- PERCENT_RANK(): The PERCENT_RANK() function always returns value that is greater than 0 and less than or equal to 1.
PostgreSQL PERCENT_RANK Function Examples
Let us take a look at some of the examples of PERCENT_RANK Function in PostgreSQL from this article.
Example 1: Using PERCENT_RANK() without PARTITION BY
First, create two tables named 'products' and 'product_groups':
PostgreSQL
CREATE TABLE product_groups (
group_id serial PRIMARY KEY,
group_name VARCHAR (255) NOT NULL
);
CREATE TABLE products (
product_id serial PRIMARY KEY,
product_name VARCHAR (255) NOT NULL,
price DECIMAL (11, 2),
group_id INT NOT NULL,
FOREIGN KEY (group_id) REFERENCES product_groups (group_id)
);
INSERT INTO product_groups (group_name)
VALUES
('Smartphone'),
('Laptop'),
('Tablet');
INSERT INTO products (product_name, group_id, price)
VALUES
('Microsoft Lumia', 1, 200),
('HTC One', 1, 400),
('Nexus', 1, 500),
('iPhone', 1, 900),
('HP Elite', 2, 1200),
('Lenovo Thinkpad', 2, 700),
('Sony VAIO', 2, 700),
('Dell Vostro', 2, 800),
('iPad', 3, 700),
('Kindle Fire', 3, 150),
('Samsung Galaxy Tab', 3, 200);
The following statement uses the PERCENT_RANK() function to calculate the sales percentile of each employee in 2019.
Query:
SELECT
name,
amount,
PERCENT_RANK() OVER (
ORDER BY amount
)
FROM
sales_stats
WHERE
year = 2019;
Output:

Explanation: This query calculates the percentile rank of each sales amount for the year 2019. The ORDER BY clause orders the sales amounts, and PERCENT_RANK() assigns a percentile rank between 0 and 1 to each amount.
Example 2: Using PERCENT_RANK() with PARTITION BY
The below statement uses the PERCENT_RANK() function to calculate the sales amount percentile by sales employees in both 2018 and 2019.
Query:
SELECT
name,
amount,
PERCENT_RANK() OVER (
PARTITION BY year
ORDER BY amount
)
FROM
sales_stats;
Output:

Explanation: This query partitions the data by year, calculating the percentile rank of sales amounts within each year. The PARTITION BY year clause ensures that the ranking is calculated separately for each year, providing a relative ranking within each partition.
Important Points About PostgreSQL PERCENT_RANK Function
- The PERCENT_RANK() function calculates the relative rank of a value within a partition, providing a value between 0 and 1.
- When there are ties in the values being ranked, PERCENT_RANK() assigns the same percentile rank to those rows.
- The PERCENT_RANK() function does not consider NULL values in its calculations. Rows with NULL values in the ORDER BY clause are ranked last.
- PERCENT_RANK() can be combined with other window functions like 'ROW_NUMBER()', 'RANK()', and 'DENSE_RANK()' to provide a more comprehensive analysis.
Similar Reads
PostgreSQL - RANK Function The RANK() function in PostgreSQL is an advanced analytical tool used to assign a rank to each row within a specific partition in a result set. This function is especially beneficial for ranking items based on specified criteria, making it ideal for data analysis and reporting tasks like identifying
4 min read
PostgreSQL - DENSE_RANK Function In PostgreSQL, the DENSE_RANK() function is a powerful tool used to assign ranks to rows within a partition of a result set, ensuring there are no gaps in the ranking values. Unlike the RANK() function, which may skip rank numbers when there are ties, DENSE_RANK() always returns consecutive rank val
3 min read
PostgreSQL - NTILE() Function In PostgreSQL, the NTILE() function is a powerful tool used to divide ordered rows into a specified number of ranked buckets, which are essentially ranked groups. This function is crucial for data analysis and reporting, allowing users to efficiently distribute rows and analyze data in a structured
3 min read
PostgreSQL - ROW_NUMBER Function The PostgreSQL ROW_NUMBER function is a crucial part of window functions, enabling users to assign unique sequential integers to rows within a dataset. This function is invaluable for tasks such as ranking, pagination and identifying duplicates. In this article, we will provide PostgreSQL ROW_NUMBER
4 min read
PostgreSQL - LEAD Function In PostgreSQL, the LEAD() function is a powerful window function used to access a row that follows the current row at a specific physical offset. This function is generally employed to compare the value of the current row with the value of the next row following the current row.Let us better underst
3 min read