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
Syntax
ntile(N) OVER (PARTITION BY ... ORDER BY ...)
Parameters
| Parameter | Description |
|---|---|
N |
The number of groups. |
Return value
An integer from 1 to N.
Examples
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
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
| Version | Change |
|---|---|
| SQLite 3.25.0 | Added |
In other languages
NTILE()· PostgreSQL
Related
ROW_NUMBER()· Number the rows of a window 1, 2, 3, … in the order given by the window's ORDER BY. Ties get different numbers.PERCENT_RANK()· Return a row's relative rank from 0 to 1: (rank - 1) / (rows in partition - 1).- SQL Window Functions · tutorial