Skip to main content
Returns the n smallest or largest values from an expression as an array. These functions are supported from v4.0.

Syntax

Parameters

  • expr: An expression of any sortable type (STRING, BOOLEAN, DATE, DATETIME, or numeric type).
  • n: An INTEGER literal greater than 0. The maximum value is 10000.

Return value

  • MIN_N: Returns an ARRAY containing the n smallest values from the expression, sorted in ascending order.
  • MAX_N: Returns an ARRAY containing the n largest values from the expression, sorted in ascending order.
The array elements have the same type as the input expression.
  • If the number of distinct values is less than n, returns all values.
  • NULL values are ignored.
  • If all values are NULL, returns an empty array.

Examples

Basic usage with MIN_N and MAX_N

  1. Create a table scores.
  2. Insert values into this table.
  3. Get the 3 smallest and 3 largest scores.
  4. Get the smallest and largest scores by subject.

Using with string values

Edge case: n larger than number of values

Using with date values

keyword

MIN_N, MAX_N