Introduction
Ranking functions
Selecting n-th row
Revision

Instruction

That's correct! The last function we'll introduce in this section is NTILE(X). It distributes the rows into a specific number of groups, provided as X. For instance:

SELECT
  name,
  genre,
  editor_rating,
  NTILE(3) OVER (ORDER BY editor_rating DESC)
FROM game;

In the above example, we create three groups with NTILE(3) that are divided based on the values in the column editor_rating. The "best" games will be put in group 1, "average" games in group 2, "worst" games in group 3. See the picture below:

[Part4_graphic]

Note that if the number of rows is not divisible by the number of groups, some groups will have one more element than other groups, with larger groups coming first.

Exercise

We want to divide games into 4 groups with regard to their size, with biggest games coming first. For each game, show its name, genre, size and the group it belongs to.

Stuck? Here's a hint!

You'll need to use NTILE(4) OVER (ORDER BY size DESC).

Console

Code editor

Result

TableConsole