Skip to main content

Overview

The NTILE() function is a window function used to divide an ordered data set into a specified number of approximately equal groups or buckets. This function assigns each group a bucket number starting form one.

Syntax

The syntax for this function is as follows:

Parameters

  • bucket: positive integer or an expression that evaluates to a positive integer for each partition. It specifies the number of groups into which the data should be divided.

Restrictions

  • buckets: its value must be a positive integer. If it is a non-integer constant, it will be truncated to an integer.

Example

For the needs of this section we will use a simplified version of the film table from the Pagila database, that will contain only the title, length and rating columns.
The query below uses the NTILE() function to divide the films into four quartiles based on their length:
By running the code above, we will get the following output: