min

Returns the smallest non-NULL value, or up to n such values.

Syntax

min(<expr>)
min(<expr>, n => <n>)

Arguments

ArgumentTypeDescription
<expr>Any type except anyValues to compare. Cast an any expression to the intended comparison type first.
nintOptional non-negative constant. Number of values to return as an array. Must not be NULL.

Returns

Without n, returns the same type as <expr>, or NULL for empty or all-NULL input.

With n, returns array of up to n non-NULL values in ascending order. Keeps duplicates. Returns [] if n is 0 or there are no non-NULL values.

Examples

Return the fastest duration and the three fastest durations per service. Values are milliseconds.

VALUES
    ('checkout', 95), ('checkout', 160), ('checkout', 480), ('checkout', 1200),
    ('catalog', 25), ('catalog', 40), ('catalog', 75), ('catalog', 140)
SELECT $0 AS service, $1 AS latency_ms
GROUP BY service
AGGREGATE min(latency_ms) AS fastest_ms, min(latency_ms, n => 3) AS top_three_ms
ORDER BY service
+----------+------------+--------------+
| service  | fastest_ms | top_three_ms |
+----------+------------+--------------+
| catalog  | 25         | [25,40,75]   |
| checkout | 95         | [95,160,480] |
+----------+------------+--------------+

Use min_by to return details from the selected request.