Skip to content

LEAST

This function returns the smallest value from a list of expr[1...N].

Syntax

sql
LEAST(expr1 [, expr2, ..., exprN] )

Arguments

expr: All expr must either have the same or a compatible data type.

Usage Instructions

  • If any of the argument values are NULL, NULL is returned.
  • If expr are strings, they are compared using nonpadded comparison semantics.
  • By default, binary comparisons are performed.
  • The string comparison is based on the numeric codes of the characters in the database character set and is performed for entire strings treated as a sequence of bytes, not character by character.

Examples

sql
select col1, col2, col3, least(col1, col2, col3)
        from 
               (           select 7  as col1, 3  as col2, 4   as col3       
                union all  select 9  as col1, 1  as col2, 9   as col3
                union all  select -1 as col1, -6 as col2, -99 as col3
                union all  select 0  as col1, NULL as col2, 100   as col3);
txt
+------+------+------+-------------------------+
| COL1 | COL2 | COL3 | LEAST(COL1, COL2, COL3) |
+------+------+------+-------------------------+
| 7    | 3    | 4    | 3                       |
| 9    | 1    | 9    | 1                       |
| -1   | -6   | -99  | -99                     |
| 0    | NULL | 100  | NULL                    |
+------+------+------+-------------------------+
sql
select col1, col2, col3, least(col1, col2, col3)
        from 
               (           select '7.77'  as col1, 3  as col2, 4   as col3         
                union all  select '9'  as col1, 1.33  as col2, 9   as col3
                union all  select '-1' as col1, -6 as col2, -99 as col3
                union all  select '7.77'  as col1, 1.3333 as col2, 100   as col3);
txt
+------+--------------------+------+-------------------------+
| COL1 | COL2               | COL3 | LEAST(COL1, COL2, COL3) |
+------+--------------------+------+-------------------------+
| 7.77 | 3                  | 4    | 3                       |
| 9    | 1.33               | 9    | 1.33                    |
| -1   | -6                 | -99  | -1                      |
| 7.77 | 1.3333000000000002 | 100  | 1.333                   |
+------+--------------------+------+-------------------------+
sql
select col1, col2, col3, least(col1, col2, col3)
        from 
               (           select 'Alpha'  as col1, 'Betta'  as col2, 'Gamma'      as col3         
                union all  select 'A'      as col1, 'B'      as col2, 'C'          as col3
                union all  select 'A'      as col1, ' B'     as col2, 'C'          as col3
                union all  select 'C'      as col1, 'B'      as col2, 'A'          as col3
                union all  select 'Long'   as col1, 'Longer' as col2, 'Long(very)' as col3);
txt
+-------+--------+------------+-------------------------+
| COL1  | COL2   | COL3       | LEAST(COL1, COL2, COL3) |
+-------+--------+------------+-------------------------+
| Alpha | Betta  | Gamma      | Alpha                   |
| A     | B      | C          | A                       |
| A     | B      | C          | B                       |
| C     | B      | A          | A                       |
| Long  | Longer | Long(very) | Long                    |
+-------+--------+------------+-------------------------+