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 is10000.
Return value
MIN_N: Returns an ARRAY containing thensmallest values from the expression, sorted in ascending order.MAX_N: Returns an ARRAY containing thenlargest values from the expression, sorted in ascending order.
- 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
-
Create a table
scores. -
Insert values into this table.
-
Get the 3 smallest and 3 largest scores.
-
Get the smallest and largest scores by subject.