Home / SQL / Reference / Window functions

SQL NTILE

Split ordered rows into n roughly equal groups (quartiles, deciles).

Syntax

NTILE(n) OVER (ORDER BY …)

Example

Query (on the shop database used in the SQL course)

SELECT name, price, NTILE(4) OVER (ORDER BY price) AS price_quartile
FROM products;

Result

namepriceprice_quartile
A5 notebook8.51
Gel pens, pack of 1011.01
Green tea, 100 bags12.01
SQL Pocket Guide25.01
Wireless mouse29.02
Coffee beans 1 kg32.02
Python Crash Course (book)45.02
USB-C hub49.02
Desk lamp59.03
Deep Learning (book)89.03
Mechanical keyboard119.03
Noise-cancelling headphones249.03
27-inch monitor329.04
Ergonomic chair399.04
Standing desk549.04

15 rows

Related

Learn it step by step in the free lesson CTEs & Window Functions, with exercises that check your queries.