Skip to content

WIDTH_BUCKET

WIDTH_BUCKET(expr, min_value, max_value, num_buckets)

Section titled “WIDTH_BUCKET(expr, min_value, max_value, num_buckets)”
  • expr: This is an expression that specifies the input value to be sorted into a bucket.
  • min_value: This parameter specifies the minimum value of the range that the bucketing algorithm should consider.
  • max_value: This signifies the maximum value of the range for the bucketing algorithm.
  • num_buckets: This parameter denotes the number of equal width buckets to be created within the provided range.
SELECT WIDTH_BUCKET(salary, 1000, 9000, 8) AS bucket, COUNT(*)
FROM employees
GROUP BY WIDTH_BUCKET(salary, 1000, 9000, 8)
ORDER BY bucket;
| bucket | count |
|--------|-------|
| 1 | 5 |
| 2 | 8 |
| 3 | 12 |
| 4 | 7 |
| 5 | 9 |
| 6 | 4 |
| 7 | 3 |
| 8 | 7 |

In this example, WIDTH_BUCKET function distributes the range of salary values into 8 buckets. The range is from 1000 to 9000. Then, it counts the number of salary values within each bucket. The result is a frequency distribution of salary ranges.