Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Percentile and Distribution Functions | Ranking and Analytic Window Functions
Window Functions in SQL

bookPercentile and Distribution Functions

Understanding how to divide data into meaningful groups and calculate percentiles is essential in business analytics. Two powerful SQL window functions that help you with this are NTILE and percent_rank. These functions allow you to segment data, such as employee performance scores, into quantiles or calculate where a specific value falls within a distribution. Businesses often use these techniques to identify top performers, segment customers, or analyze sales performance across different groups.

123456
SELECT employee_id, score, NTILE(4) OVER (ORDER BY score DESC) AS performance_quartile FROM performance;
copy

The query above demonstrates how to use NTILE(4) to assign each performance score to a quartile based on descending order. This means the highest scores are grouped into the first quartile, and the lowest into the fourth. This kind of grouping is invaluable for quickly identifying which employees are in the top 25% of performers, as well as those who may need additional support or training.

Beyond dividing data into groups, you may want to know the relative standing of a value within the entire dataset. The percent_rank and cume_dist window functions are designed for this purpose. percent_rank calculates the percentile rank of each row, showing the relative position of a row within the result set. The value ranges from 0 (lowest) to 1 (highest), excluding the very first row, which always has a rank of 0. For example, if you want to see how each employee's score compares to others, you could use percent_rank() as shown below.

123456
SELECT employee_id, score, percent_rank() OVER (ORDER BY score) AS percentile_rank FROM performance;
copy

Beyond dividing data into groups, you may want to know the relative standing of a value within the entire dataset. The percent_rank and cume_dist window functions are designed for this purpose. percent_rank calculates the percentile rank of each row, showing the relative position of a row within the result set. The value ranges from 0 (lowest) to 1 (highest), excluding the very first row, which always has a rank of 0. If you want to see how each employee's score compares to others, you could use percent_rank() as shown below.

The cume_dist() function is similar but returns the cumulative distribution, showing the proportion of rows with a value less than or equal to the current row. Both functions are commonly used for performance reviews, grading, and customer segmentation, enabling you to make data-driven decisions based on relative standing rather than absolute values.

1. What does NTILE(4) do in a window function?

2. Which function would you use to calculate the percentile rank of a value?

3. Fill in the blank: Write the SQL statement to calculate the percentile rank of each score in the performance table.

question mark

What does NTILE(4) do in a window function?

Select the correct answer

question mark

Which function would you use to calculate the percentile rank of a value?

Select the correct answer

question-icon

Fill in the blank: Write the SQL statement to calculate the percentile rank of each score in the performance table.

OVER (ORDER BY score) FROM performance;
0
0.3333333333333333
0.6666666666666666
1
...

Натисніть або перетягніть елементи та заповніть пропуски

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 3

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

bookPercentile and Distribution Functions

Свайпніть щоб показати меню

Understanding how to divide data into meaningful groups and calculate percentiles is essential in business analytics. Two powerful SQL window functions that help you with this are NTILE and percent_rank. These functions allow you to segment data, such as employee performance scores, into quantiles or calculate where a specific value falls within a distribution. Businesses often use these techniques to identify top performers, segment customers, or analyze sales performance across different groups.

123456
SELECT employee_id, score, NTILE(4) OVER (ORDER BY score DESC) AS performance_quartile FROM performance;
copy

The query above demonstrates how to use NTILE(4) to assign each performance score to a quartile based on descending order. This means the highest scores are grouped into the first quartile, and the lowest into the fourth. This kind of grouping is invaluable for quickly identifying which employees are in the top 25% of performers, as well as those who may need additional support or training.

Beyond dividing data into groups, you may want to know the relative standing of a value within the entire dataset. The percent_rank and cume_dist window functions are designed for this purpose. percent_rank calculates the percentile rank of each row, showing the relative position of a row within the result set. The value ranges from 0 (lowest) to 1 (highest), excluding the very first row, which always has a rank of 0. For example, if you want to see how each employee's score compares to others, you could use percent_rank() as shown below.

123456
SELECT employee_id, score, percent_rank() OVER (ORDER BY score) AS percentile_rank FROM performance;
copy

Beyond dividing data into groups, you may want to know the relative standing of a value within the entire dataset. The percent_rank and cume_dist window functions are designed for this purpose. percent_rank calculates the percentile rank of each row, showing the relative position of a row within the result set. The value ranges from 0 (lowest) to 1 (highest), excluding the very first row, which always has a rank of 0. If you want to see how each employee's score compares to others, you could use percent_rank() as shown below.

The cume_dist() function is similar but returns the cumulative distribution, showing the proportion of rows with a value less than or equal to the current row. Both functions are commonly used for performance reviews, grading, and customer segmentation, enabling you to make data-driven decisions based on relative standing rather than absolute values.

1. What does NTILE(4) do in a window function?

2. Which function would you use to calculate the percentile rank of a value?

3. Fill in the blank: Write the SQL statement to calculate the percentile rank of each score in the performance table.

question mark

What does NTILE(4) do in a window function?

Select the correct answer

question mark

Which function would you use to calculate the percentile rank of a value?

Select the correct answer

question-icon

Fill in the blank: Write the SQL statement to calculate the percentile rank of each score in the performance table.

OVER (ORDER BY score) FROM performance;
0
0.3333333333333333
0.6666666666666666
1
...

Натисніть або перетягніть елементи та заповніть пропуски

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 3
some-alt