LEAST
Syntax
%%{init: {
'theme': 'base',
'flowchart': { 'padding': '7', 'nodeSpacing': '20', 'rankSpacing': '20' },
'themeVariables': {
'fontSize': '11px',
'fontFamily': 'Arial'
}
}}%%
flowchart LR
Start((START)) --> LEAST[LEAST]:::quoted
LEAST --> End((END))
Purpose
The LEAST SQL function returns the smallest of its arguments. It compares the values of one row, where the aggregate function MIN compares the values of a column over many rows.
The result is null as soon as one of the arguments is null, as the SQL:2023 standard (feature T054) and Oracle prescribe. The empty text has null semantics on Invantive UniversalSQL, so an empty text also makes the result null. PostgreSQL and SQL Server ignore null arguments instead; write least(coalesce(a, b), coalesce(b, a)) to get that behaviour.
The values are compared as the comparison operators > and < compare them: text is compared character by character on its code, so upper case sorts before lower case, and trailing spaces are ignored. Binary values are compared byte by byte. When two arguments are equal, the first one is returned.
Available from release 28.0.
See also GREATEST and COALESCE.
Parameters:
- Position 1 (
ANY): a value. - Position n (
ANY): more values, optional.
All arguments must have data types that can be compared with each other: all numbers, all texts, or all of one other data type such as dates. A number and a text cannot be mixed; convert one of them with to_number or to_char first. A mix of data types raises itgenttn521 before the first row is read, and a call without arguments raises itgenttn522.
Returns: the smallest value. Its data type is that of the arguments; for numbers of different types it is the widest of them, so least(1, 2.5) returns a decimal.
Examples
The following example retrieves the smallest number:
select least(3, 7, 5)
--------
3
The following example retrieves the smallest text; upper case sorts before lower case:
select least('apple', 'Banana', 'cherry')
--------
Banana
The following example returns null, since one of the arguments is null:
select least(null, 1)
--------
null