SQL Server NTILE() Function
Last Updated :
17 May, 2024
SQL NTILE() function is a window function that distributes rows of an ordered partition into a pre-defined number of roughly equal groups.
NTILE() Function in SQL Server
The NTILE() function in SQL server is used to distribute rows of an ordered partition into a specified number of approximately equal groups, or buckets.
It assigns each group a number_expression ranging from one. NTILE() function assigns a number_expression for every row in a group, to which the row belongs.
Syntax
The NTILE() function syntax is:
NTILE(number_expression) OVER ( [PARTITION BY partition_expression ] ORDER BY sort_expression [ASC | DESC])
Parameters :
- number_expression The number_expression is the integer into which the rows are divided.
- PARTITION BY clause The PARTITION BY is optional, it differs the rows of a result set into partitions where the NTILE() function is used.
- ORDER BY clause The ORDER BY clause defines the order of rows in each partition where the NTILE() is used.
When a number of rows aren't divisible by the number_expression, the NTILE() function results the groups of two sizes with a difference by one. The larger groups always come ahead of the smaller group within the order specified by the ORDER BY within the OVER() clause. Also, when the all of rows are divisible by the number_expression, the function divides evenly the rows among number_expression.
SQL NTILE() Function Example
Let's look at some examples of NTILE() function in SQL Server to understand it better.
First we will create a table named 'geeks_demo'
Query:
CREATE TABLE geeks_demo (
ID INT NOT NULL );
INSERT INTO geeks_demo(ID)
VALUES(1), (2), (3), (4), (5), (6), (7), (8), (9), (10);
Output:
Use NTILE() function to divide above rows into 3 groups
Query:
SELECT ID,
NTILE (3) OVER (
ORDER BY ID
) Group_number
FROM geeks_demo;
Output :
ID | Group_number |
---|
1 | 1 |
2 | 1 |
3 | 1 |
4 | 1 |
5 | 2 |
6 | 2 |
7 | 2 |
8 | 3 |
9 | 3 |
10 | 3 |
Use the NTILE() function to distribute rows into 5 groups
Query:
SELECT ID,
NTILE (5) OVER (
ORDER BY ID
) Group_number
FROM geeks_demo;
Output :
ID | Group_number |
---|
1 | 1 |
2 | 1 |
3 | 2 |
4 | 2 |
5 | 3 |
6 | 3 |
7 | 4 |
8 | 4 |
9 | 5 |
10 | 5 |
Using the NTILE() function without number_expression Example
SELECT ID,
NTILE () OVER (
ORDER BY ID
) Group_number
FROM geeks_demo;
Output: It will throw the below error:
The function 'NTILE' takes exactly 1 argument(s).
Important Points About SQL Server NTILE() Function
- The NTILE() function is a window function that distributes rows of an ordered partition into a specified number of approximately equal groups, or buckets.
- It assigns each group a bucket number starting from one.
- If the total of rows is divisible by the
buckets
, the function divides evenly the rows among buckets. - If the number of rows is not divisible by the
buckets
, the NTILE() function returns groups of two sizes with the difference by one. - Proper indexing on columns used in the
ORDER BY
clause within the OVER()
partition can improve performance when using NTILE() on large datasets - It is useful for creating histograms, analyzing rankings, or dividing data to allow parallel processing.
Similar Reads
LTRIM() Function in SQL Server
The LTRIM() function in SQL Server removes all the space characters found on the left-hand side of the string. It removes the leading spaces from a string, SyntaxThe LTRIM function for SQL Server syntax is: LTRIM(string, [trim_string]) Parameter: string - The string from which the leading space char
2 min read
RTRIM() Function in SQL Server
The RTRIM() function removes trailing spaces from a string. Syntax : RTRIM(input_string) Parameter : RTRIM() function accepts single-parameter like input_string. input_string : It is an expression of character or binary data. It can be a literal string, variable, or column. Returns - It returns a st
1 min read
LOWER() function in SQL Server
LOWER() : This function in SQL Server helps to convert all the letters of the given string to lowercase. If the given string contains characters other than alphabets, then they will remain unchanged by this function. Syntax : LOWER( str ) Parameters : str - The string which will be converted to lowe
2 min read
SQL Server RAND() Function
The RAND() function in SQL Server generates pseudo-random floating-point numbers within the range of 0 (inclusive) and 1 (exclusive). It is a function that can produce different random values each time it is called unless a specific seed is provided which results in a repeatable sequence of numbers.
3 min read
SQL Server ISNULL() Function
The ISNULL() function in SQL Server is a powerful tool for handling NULL values in our database queries. It allows us to replace NULL values with a specified replacement value, ensuring that your queries return meaningful results even when data is missing.In this article, We will learn about the SQL
3 min read
SQL Server GETDATE() Function
SQL Server GETDATE() function returns the current date and time of the database system in a 'YYYY-MM-DD hh:mm:ss.mmm' format. Note: GETDATE() is similar to other date and time functions like CURRENT_TIMESTAMP, SYSDATETIME(), and GETUTCDATE(), but differs primarily in terms of the time zone and preci
2 min read
DAY() Function in SQL Server
DAY() function : This function in SQL Server is used to return the day of the month i.e, from 1st to 31st for date stated. Features : This function is used to find the day of the month for a date specified. This function comes under Date Functions. This function accepts only one parameter i.e, date.
2 min read
STR() Function in SQL Server
The STR() function converts a numeric value to a character value. Syntax : STR(float_expression [, length [, decimal]]) Parameter : This method accepts three parameters as mentioned above and described below : float_expression : It is a numeric expression that evaluates to an approximate number with
1 min read
RANK() Function in SQL Server
The RANK() function is a powerful window function in SQL Server used to assign a rank to each row within a result set. It is particularly useful when we need to assign a rank to a group of rows based on some sorting criteria and want to differentiate between rows that have the same values. Unlike ot
5 min read
REPLICATE() Function in SQL Server
REPLICATE() function : This function in SQL Server is used to repeat the specified string for a given number of times. Features : This function is used to return the stated string for a given number of times. This function accepts only strings and integers as parameter. This function returns an erro
2 min read