A database is a collection of structured data stored in an organized manner. PostgreSQL is a relational database management system that employs SQL for fundamental operations such as creating tables, inserting data into tables, modifying the records, and deleting the existing data from the tables. Understanding SQL keywords is crucial for being skilled in SQL. One such window function is NTILE()
which can help in grouping the records according to specified ranks.
The NTILE()
function divides the result set into specified groups called buckets or tiles. This window function assigns a bucket number to each row based on the specified number within the function.
The following is the syntax for NTILE()
:
NTILE ( [expression] )OVER ( [PARTITION BY clause][ORDER BY clause] )
The NTILE([expression])
represents the window function with the expression value.
We create partitions by defining the column/condition in PARTITION BY clause
. This clause becomes optional if there’s no need to create partitions. The window function will treat the dataset as a single window in such cases.
The ORDER BY clause
specifies the order within each partition.
Find more details on the
PARTITION BY
clause and theORDER BY
clause in their own respective Answers.
Let's understand the NTILE()
window function through coding examples.
Let's suppose we have the following data on the student's exam results. We're using the SELECT
query to view the records in the Exam_result
table.
SELECT * FROM Exam_result;
We can place each subject's data in two buckets/groups using the NTILE()
function. The coding example is:
SELECT *,NTILE(2) OVER(PARTITION BY subject_name ORDER BY marks DESC) AS rank_tileFROM Exam_resultORDER BY subject_name, rank_tile;
The explanation of the code is given below:
Line 1: We use the SELECT
statement to read all the data.
Line 2: NTILE()
function divides the result set into two equal groups (buckets) based on the marks
column, within each subject_name
partition, assigning a rank to each row within the partition. The rank is stored in the rank_tile
column.
Line 3: We use the Exam_result
table for data.
Line 4: The ORDER BY
clause orders the rows first by subject_name
and then by rank_tile
.
This query divides the students into two buckets per subject_name
based on their marks
, assigning a rank to each student within each subject_name
group. Students with higher marks in each subject are assigned to the first bucket (rank 1), while students with lower marks are assigned to the second bucket (rank 2).
We can place all subject's data in two buckets/groups using the NTILE()
function. The coding example is:
SELECT *,NTILE(2) OVER(ORDER BY marks DESC) AS rank_tileFROM Exam_resultORDER BY rank_tile;
The explanation of the code is given below:
Line 1: We use the SELECT
statement to read all the data.
Line 2: NTILE()
function divides the result set into two equal groups (buckets) based on the marks
column, assigning a rank to each row in the result set. The rank is stored in the rank_tile
column.
Line 3: We use the Exam_result
table for data.
Line 4: The ORDER BY
clause orders the rows by rank_tile
.
This query divides all students into two buckets based on their marks
, assigning a rank to each student across all subjects. Students with higher marks are assigned to the first bucket (rank 1), while students with lower marks are assigned to the second bucket (rank 2).
The NTILE()
window function takes an integer expression and assigns a bucket number to each row based on the specified number within the function. An interesting fact about window functions in SQL is we can use arithmetic operations within the window function. For example, it will divide all the values into two buckets if we use any expression like 8/4
or 1+1
. The rest of the calculation depends on the functions written in the OVER()
clause.
Free Resources