Skip to main content

greatest

1. GREATEST Function

The GREATEST function in H2 database is used to determine the greatest (maximum) value among the given expressions or values.

2. Syntax

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

GREATEST(value1, value2, ...)

Arguments

  • value1, value2, ...: The expressions or values to be compared and the greatest value is returned.

Return

  • The GREATEST function returns the greatest value among the given expressions or values.

3. Notes

  • The GREATEST function in H2 database can take multiple arguments, and it compares them to find the maximum value.
  • The values can be of any compatible data type, such as numeric, string, or date/time.
  • If any of the arguments is NULL, the GREATEST function will return NULL.
  • The GREATEST function can be useful when you need to determine the maximum value among a set of values or expressions.

4. Examples

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

Example 1 - Finding the greatest value among two numbers:

SELECT GREATEST(10, 15) AS greatest_value;

Output:

greatest_value
--------------
15

Example 2 - Finding the greatest value among three columns:

CREATE TABLE products (
id INT PRIMARY KEY,
name VARCHAR(50),
price FLOAT,
discount_price FLOAT
);

INSERT INTO products VALUES (1, 'Product A', 25.99, 19.99),
(2, 'Product B', 29.99, 24.99),
(3, 'Product C', 15.99, 14.99);

SELECT id, name, GREATEST(price, discount_price) AS greatest_price
FROM products;

Output:

id |   name    | greatest_price
---+-----------+---------------
1 | Product A | 25.99
2 | Product B | 29.99
3 | Product C | 15.99
  • least - Determine the smallest (minimum) value among the given expressions or values.