Skip to main content

least

1. LEAST Function

The LEAST function in H2 database is used to find the smallest value among the given set of expressions or column values.

2. Syntax

The syntax for the LEAST function in H2 database is as follows:

LEAST(value1, value2, ...)

Arguments

  • value1, value2, ...: The values or expressions to compare. These can be of any compatible data type.

Return

  • The LEAST function returns the smallest value among the provided values.

3. Notes

  • The LEAST function in H2 database compares the given values and returns the minimum value.
  • The LEAST function can take any number of arguments, but at least two arguments are required.
  • The data types of the arguments should be compatible for proper comparison.
  • If the arguments include null values, the LEAST function will return null.
  • If the arguments are of different types, H2 database will attempt to convert them to a common type for comparison.

4. Examples

Here are a few examples demonstrating the usage of the LEAST function in H2 database:

Example 1 - Finding the smallest value among two numbers:

SELECT LEAST(5, 8) AS smallest_value;

Output:

smallest_value
--------------
5

Example 2 - Finding the smallest value among multiple columns:

CREATE TABLE values (
id INT PRIMARY KEY,
val1 INT,
val2 INT,
val3 INT
);

INSERT INTO values VALUES (1, 10, 20, 30), (2, 15, 5, 25), (3, 40, 30, 20);

SELECT id, val1, val2, val3, LEAST(val1, val2, val3) AS smallest_value
FROM values;

Output:

id | val1 | val2 | val3 | smallest_value
---+------+------+------+----------------
1 | 10 | 20 | 30 | 10
2 | 15 | 5 | 25 | 5
3 | 40 | 30 | 20 | 20
  • greatest - Find the greatest value among the given set of expressions or column values.