SQLite 3 reference · Window Functions

SQLite NTILE() Function

Split the rows of a window into N groups of nearly equal size and return each row's group number.

SQLite 3 Since 3.25.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

ntile(N) OVER (PARTITION BY ... ORDER BY ...)

Parameters

ParameterDescription
N The number of groups.

Return value

An integer from 1 to N.

Examples

Split into thirdsTry it yourself
SELECT name, price, ntile(3) OVER (ORDER BY price) AS price_third
FROM products;

Output

name | price | price_third
Rubber Duck | 5.5 | 1
Coffee Mug | 12 | 1
Wireless Mouse | 24.99 | 1
SQL for Beginners | 29.99 | 1
Clean Code | 37.5 | 2
USB-C Hub | 39.95 | 2
The Pragmatic Programmer | 42 | 2
Desk Lamp | 45 | 2
Standing Desk Mat | 59.9 | 3
Mechanical Keyboard | 89 | 3
Robot Kit | 120 | 3
4K Monitor | 329 | 3
Group sizesTry it yourself
SELECT quartile, count(*) AS people
FROM (SELECT ntile(4) OVER (ORDER BY salary DESC) AS quartile FROM employees)
GROUP BY quartile;

Output

quartile | people
1 | 3
2 | 3
3 | 2
4 | 2

Version history

VersionChange
SQLite 3.25.0Added

In other languages

Was this page helpful?