WIDTH_BUCKET enables you to construct a histogram range divided into intervals of identical size. The function returns the bucket number into which the value of an expression falls.

When needed, WIDTH_BUCKET creates an underflow bucket numbered 0 and an overflow bucket numbered num_buckets+1. These buckets handle values outside the specified range and are helpful in checking the reasonableness of the end points.

Return Value



   (expr, min_value, max_value, num_buckets)


expr is the expression for which the histogram is being created. This expression must evaluate to a numeric or datetime value or to a value. If expr evaluates to null, then the function returns null.

min_value and max_value are expressions for the end points of the acceptable range for expr. Both of these expressions must evaluate to numeric or datetime values, and neither can evaluate to null.

num_buckets is an expression for the number of buckets. This expression must evaluate to a positive integer.


WIDTH_BUCKET(13, 0, 20, 4) returns the value 3. It creates four buckets from 0 to 20 and sorts the value 13 into bucket 3.

WIDTH_BUCKET(-5, 0, 20, 4) returns the value 0. The value -5 is below the beginning of the range.