Overview
TheGREATEST() function extracts the greatest or largest value from a set of values. It needs at least one argument to work with, and if you mix different types, like a text and a number, it will return an error.
For example, comparing the greatest value among 4, “two”, and 9 would result in an error.
Syntax
The syntax for theGREATEST() function is as follows:
value_1: Represents the first value.value_n: Represents one or more additional values, separated by commas.
Info:
-
- The result will be
-
NULL values within the expressions are ignored. - The result will be
NULL if all expressions evaluate to NULL.Examples
Here are examples that illustrate the usage of theGREATEST() function:
Case #1: Basic Usage
Consider the following example:3, the smallest value among the provided values.
Case #2: String Comparison
String comparison is also supported, as shown below:'cherry', the greatest string according to the order.
Case #3: Handling NULL Values
NULL values are ignored when determining the greatest value:
9.
Case #4: Positive and Negative Numbers
Negative numbers can also be compared:8, the greatest value among the provided numbers.
Case #5: Using Table Data
TheGREATEST function can also be used to find the Greatest value between column data. For example, let’s create a table named Student that stores students’ names and scores.
SELECT statement to view all the records: