NTILE()
NTILE() is a SQL function in the Window Functions category. Divides the result set into `num_buckets` (roughly) equal groups. Useful for percentile analysis. The syntax is NTILE(num_buckets) OVER (ORDER BY column). It accepts num_buckets. It returns bucket number (1 to num_buckets). A typical example: SELECT name, salary, NTILE(4) OVER (ORDER BY salary DESC) AS salary_quartile FROM employees; -- Divides employees into 4 equal salary groups: -- 1 = top 25%, 2 = 25-50%, 3 = 50-75%, 4 = bottom 25% A close relative is ROW_NUMBER(), which assigns a unique sequential integer to each row within its partition (1, 2, 3...). A close relative is RANK(), which similar to ROW_NUMBER but gives the same rank to equal values. Leaves gaps in ranking. A close relative is DENSE_RANK(), which like RANK but without gaps. Equal values get the same rank, and the next number follows sequentially. More about this category: Window/analytical functions — ROW_NUMBER, RANK, DENSE_RANK, LAG, LEAD, NTILE.