GREATEST() - Finding the Greatest/Maximum Value

Q

How to find the greatest (maximum) value of a given list of values using the GREATEST() function?

✍: FYIcenter.com

A

GREATEST(val1, val2, ...) is a MySQL built-in function that returns the greatest (maximum) value of a given list of values. For example:

SELECT GREATEST(70, 89, 73, 99, 101, 110, 116, 101, 114);
  -- +---------------------------------------------------+
  -- | GREATEST(70, 89, 73, 99, 101, 110, 116, 101, 114) |
  -- +---------------------------------------------------+
  -- |                                               116 |
  -- +---------------------------------------------------+

SELECT GREATEST('Grape', 'Orange', 'Banana', 'Apple');
  -- +------------------------------------------------+
  -- | GREATEST('Grape', 'Orange', 'Banana', 'Apple') |
  -- +------------------------------------------------+
  -- | Orange                                         |
  -- +------------------------------------------------+

If the arguments comprise a mix of multiple data types, GREATEST() converts them to the same data type using the following rules:

  • If any argument is NULL, the result is NULL. No comparison is needed.
  • Boolean values of TRUE and FALSE are compared as integers of 1 and 0.
  • If all arguments are integers, they are compared as integers.
  • If all arguments are numbers with at least one argument is double precision, they are compared as double-precision values.
  • If all arguments are numbers with at least one argument is a DECIMAL value, they are compared as DECIMAL values.
  • If all arguments are numbers with single-precision, they are compared as single-precision values.
  • If the arguments comprise a mix of numbers and strings, they are compared as strings (character or byte sequences).
  • If any argument is a character sequence, the arguments are compared as character sequences.
  • In all other cases, the arguments are compared as binary sequences.

Here are examples with arguments of mix data types:

SELECT GREATEST(70, 89, 73, 99, 101, 'Banana', 'Apple');
  -- +--------------------------------------------------+
  -- | GREATEST(70, 89, 73, 99, 101, 'Banana', 'Apple') |
  -- +--------------------------------------------------+
  -- | Banana                                           |
  -- +--------------------------------------------------+

SELECT GREATEST(TRUE, 70, 89, NULL, 'Banana', 'Apple');
  -- +-------------------------------------------------+
  -- | GREATEST(TRUE, 70, 89, NULL, 'Banana', 'Apple') |
  -- +-------------------------------------------------+
  -- | NULL                                            |
  -- +-------------------------------------------------+

SELECT GREATEST(NOW(), 'Yesterday'), GREATEST('2023-12-16', 'Yesterday');
  -- +------------------------------+-------------------------------------+
  -- | GREATEST(NOW(), 'Yesterday') | GREATEST('2023-12-16', 'Yesterday') |
  -- +------------------------------+-------------------------------------+
  -- | 2023-12-16 18:19:27.000000   | Yesterday                           |
  -- +------------------------------+-------------------------------------+

SELECT GREATEST(NOW(), '2099-12-01', CURRENT_TIMESTAMP());
  -- +----------------------------------------------------+
  -- | GREATEST(NOW(), '2099-12-01', CURRENT_TIMESTAMP()) |
  -- +----------------------------------------------------+
  -- | 2099-12-01 00:00:00                                |
  -- +----------------------------------------------------+

SELECT GREATEST(JSON_ARRAY(70, 89), JSON_ARRAY('70', '89'));
  -- +------------------------------------------------------+
  -- | GREATEST(JSON_ARRAY(70, 89), JSON_ARRAY('70', '89')) |
  -- +------------------------------------------------------+
  -- | [70, 89]                                             |
  -- +------------------------------------------------------+

Reference information of the GREATEST() function:

GREATEST(val1, val2, ...): max
  Returns the greatest (maximum) value of a given list of values.

Arguments, return value and availability:
  val1, val2, ...: Two or more values to be examined.
  max: Return value. The maximum value of multiple values.
  Available since MySQL 4.0.

Related MySQL functions:

 

IF() - Conditional Value Selection

GET_LOCK() - Requesting User Defined Lock

MySQL Functions for Miscellaneous Purposes

⇑⇑ MySQL Function References

2023-12-17, 263🔥, 0💬