PostgreSQL - NTILE() Function
Last Updated :
06 Aug, 2024
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 manner.
Let us get a better understanding of the NTILE Function in PostgreSQL from this article.
NTILE() Function in PostgreSQL
The NTILE() function partitions data into a specified number of buckets. Each bucket contains a roughly equal number of rows. This function is particularly useful for creating quantiles, percentiles, and other ranked groupings.
Syntax
The syntax of the NTILE() looks like below:
NTILE(buckets) OVER (
[PARTITION BY partition_expression, ... ]
[ORDER BY sort_expression [ASC | DESC], ...]
)
Parameters
Let's analyze the above syntax:
- buckets: The number of ranked groups. This can be a positive integer or an expression that evaluates to a positive integer.
- PARTITION BY: An optional clause that distributes rows into partitions.
- ORDER BY: This clause sorts rows within each partition.
PostgreSQL NTILE() Function Examples
Let's look into some practical examples to understand how the NTILE() function works in PostgreSQL.
Example 1: Distributing Rows into Buckets
First, create a table named 'sales_stats' that stores theĀ sales revenue by employees:
PostgreSQL
CREATE TABLE sales_stats(
name VARCHAR(100) NOT NULL,
year SMALLINT NOT NULL CHECK (year > 0),
amount DECIMAL(10, 2) CHECK (amount >= 0),
PRIMARY KEY (name, year)
);
INSERT INTO
sales_stats(name, year, amount)
VALUES
('Raju kumar', 2018, 120000),
('Alibaba', 2018, 110000),
('Gabbar Singh', 2018, 150000),
('Kadar Khan', 2018, 30000),
('Amrish Puri', 2018, 200000),
('Raju kumar', 2019, 150000),
('Alibaba', 2019, 130000),
('Gabbar Singh', 2019, 180000),
('Kadar Khan', 2019, 25000),
('Amrish Puri', 2019, 270000);
Now, use the NTILE() function to distribute rows into 3 buckets for the year 2019:
Query:
SELECT
name,
amount,
NTILE(3) OVER(
ORDER BY amount
)
FROM
sales_stats
WHERE
year = 2019;
Output:

Explanation: The NTILE() function divides the rows into three buckets based on the 'amount' column for the year 2019.
Example 2: Dividing Rows into Partitions and Buckets
The below query uses the NTILE() function to divide rows in the 'sales_stats' table into two partitions and 3 buckets for each.
Query:
SELECT
name,
amount,
NTILE(3) OVER(
PARTITION BY year
ORDER BY amount
)
FROM
sales_stats;
Output:

Explanation: 'PARTITION BY year'
d
ivides the rows into partitions based on the year. The 'ORDER BY amount'
Sorts the rows within each partition by the amount
column and the NTILE(3)
distributes rows into three buckets within each partition.
Important Points About the PostgreSQL NTILE() Function
- NTILE() allows flexible grouping of data into buckets based on specified criteria.
- The function can partition data using the PARTITION BY clause, allowing for more granular analysis.
- The buckets parameter must be a positive integer or an expression that evaluates to a positive integer.
Similar Reads
PostgreSQL - NTH_VALUE Function The PostgreSQL NTH_VALUE() function is an essential tool in advanced SQL queries for analytical purposes. It allows us to retrieve the value from the nth row in an ordered set within a specified window. This functionality is invaluable when we need to pinpoint specific data points, such as the nth h
5 min read
PostgreSQL - FORMAT Function The PostgreSQL format() function is a powerful tool for string formatting by allowing developers to insert variables into strings using format specifiers like %s, %I, and %L. This function is especially useful for building dynamic SQL queries and ensuring proper formatting of identifiers. It simplif
3 min read
PostgreSQL - INSERT PostgreSQL INSERT statement is one of the fundamental SQL commands used to add new rows to a specified table within a PostgreSQL database. This command allows users to insert data efficiently, whether for a single record or multiple records at once. With the PostgreSQL INSERT INTO clause, we can spe
4 min read
PostgreSQL - CURRENT_TIME Function The PostgreSQL CURRENT_TIME function returns the current time and the current time zone. This function is handy when you need to work with time-sensitive data in your database applications.Let us better understand the CURRENT_TIME Function in PostgreSQL from this article.SyntaxCURRENT_TIME(precision
2 min read
PostgreSQL - CURRENT_DATE Function The PostgreSQL CURRENT_DATE function is used to retrieve the current date. Itâs a simple and effective way to ensure your database operations are using the correct date, particularly for applications that need timestamp records.Let us better understand the CURRENT_DATE Function in PostgreSQL from th
2 min read
PostgreSQL - User Defined Functions PostgreSQL, one of the most powerful open-source relational database management systems (RDBMS), provides a strong feature set for creating and utilizing user-defined functions (UDFs). By using user-defined functions, we can enhance the modularity, maintainability, and performance of our database ap
5 min read