PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References
PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References

PostgreSQL LEAST() Function



The PostgreSQL LEAST() function returns the smallest value in a list of expressions. The expressions must all be convertible to a common data type, which will be the type of the result. NULL values in the list are ignored. The result will be NULL only if all the expressions evaluate to NULL.

Syntax

LEAST(expr1, expr2, ... expr_n)

Parameters

expr1, expr2, ... expr_n Required. Specify the list of expressions to be evaluated.

Return Value

Returns the smallest value in a list of expressions.

Example 1:

The example below shows the usage of LEAST() function.

SELECT LEAST(20, 30, 60, 10);
Result: 10

SELECT LEAST('20', '30', '60', '10');
Result: '10'

SELECT LEAST('D', 'G', 'X', 'A');
Result: 'A'

SELECT LEAST('Alpha', 'Beta', 'Delta', 'Gamma');
Result: 'Alpha'

SELECT LEAST('Alpha1', 'Alpha2', 'Alpha3', 'Alpha4');
Result: 'Alpha1'

SELECT LEAST(20, 30, 60, 10, NULL);
Result: 10

Example 2:

Consider a database table called Sample with the following records:

Dataxyz
Data 110041
Data 2201542
Data 3303043
Data 4404544
Data 5506045

To get the smallest value, when values of column x, column y and column z are compared, the following query can be used:

SELECT *, LEAST(x, y, z) AS LEAST_Value FROM Sample;

This will produce the result as shown below:

DataxyzLEAST_Value
Data 1100410
Data 220154215
Data 330304330
Data 440454440
Data 550604545

❮ PostgreSQL Functions