NTILE() Function in SQL Server

09 Sep, 2020
NTILE() function in SQL Server is a window function that distributes rows of an ordered partition into a pre-defined number of roughly equal groups. It assigns each group a number_expression ranging from 1. NTILE() function assigns a number_expression for every row in a group, to which the row belongs.
Syntax :
NTILE(number_expression) OVER (
   [PARTITION BY partition_expression ]
   ORDER BY sort_expression [ASC | DESC]
)

Parameters of syntax in detail :
number_expressionThe number_expression is the integer into which the rows are divided.
PARTITION BY clauseThe PARTITION BY is optional, it differs the rows of a result set into partitions where the NTILE() function is used.
ORDER BY clauseThe ORDER BY clause defines the order of rows in each partition where the NTILE() is used.
When number of rows isn’t divisible by the number_expression, the NTILE() function results the groups of two sizes with the difference by one. The larger groups always come ahead the smaller group within the order specified by the ORDER BY within the OVER() clause. Also, when the all of rows is divisible by the number_expression, the function divides evenly the rows among number_expression.
Example :Let us create a table named geeks_demo :
CREATE TABLE geeks_demo (
ID INT NOT NULL );
INSERT INTO geeks_demo(ID)
VALUES(1), (2), (3), (4), (5), (6), (7), (8), (9), (10);

Now,
SELECT * 
FROM geeks_demo;

Use NTILE() function to divide above rows into 3 groups :SELECT ID,
NTILE (3) OVER (
ORDER BY ID
) Group_number
FROM geeks_demo; Output :IDGroup_number112131415262728393103Use the NTILE() function to distribute rows into 5 groups :SELECT ID,
NTILE (3) OVER (
ORDER BY ID
) Group_number

FROM geeks_demo; Output :IDGroup_number112132425363748495105If someone try to run use the NTILE() function without number_expression :SELECT ID,
NTILE () OVER (
ORDER BY ID
) Group_number

FROM geeks_demo; Output :It will throw below error :The function 'NTILE' takes exactly 1 argument(s). 

Use NTILE() function to divide above rows into 3 groups :SELECT ID,
NTILE (3) OVER (
ORDER BY ID
) Group_number
FROM geeks_demo; Output :IDGroup_number112131415262728393103
SELECT ID,
NTILE (3) OVER (
ORDER BY ID
) Group_number
FROM geeks_demo; 
Output :
Use the NTILE() function to distribute rows into 5 groups :SELECT ID,
NTILE (3) OVER (
ORDER BY ID
) Group_number

FROM geeks_demo; Output :IDGroup_number112132425363748495105
SELECT ID,
NTILE (3) OVER (
ORDER BY ID
) Group_number

FROM geeks_demo; 
Output :
If someone try to run use the NTILE() function without number_expression :SELECT ID,
NTILE () OVER (
ORDER BY ID
) Group_number

FROM geeks_demo; Output :It will throw below error :The function 'NTILE' takes exactly 1 argument(s). 

SELECT ID,
NTILE () OVER (
ORDER BY ID
) Group_number

FROM geeks_demo; 
Output :It will throw below error :
The function 'NTILE' takes exactly 1 argument(s). 

DBMS-SQL
SQL-Server
SQL
SQL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Update Multiple Columns in Single Update Statement in SQL?
Window functions in SQL
SQL Query to Find the Name of a Person Whose Name Starts with Specific Letter
How to Select All Records from One Table That Do Not Exist in Another Table in SQL?
What is Temporary Table in SQL?
SQL using Python
SQL | Subquery
SQL Query to Convert VARCHAR to INT
SQL Query to Delete Duplicate Rows
SQL | DROP, TRUNCATE