# Source: https://docs.oxla.com/sql-reference/sql-functions/math-functions/greatest.md

> ## Documentation Index
> Fetch the complete documentation index at: https://docs.oxla.com/llms.txt
> Use this file to discover all available pages before exploring further.

# GREATEST

## Overview

The `GREATEST()` 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 the `GREATEST()` function is as follows:

```sql  theme={null}
GREATEST(value_1, [value_n])
```

Where:

* `value_1`: Represents the first value.
* `value_n`: Represents one or more additional values, separated by commas.

<Note>**Info:**<br /> -`NULL` values within the expressions are ignored. <br /> - The result will be `NULL` if all expressions evaluate to `NULL`.</Note>

## Examples

Here are examples that illustrate the usage of the `GREATEST()` function:

### Case #1: Basic Usage

Consider the following example:

```sql  theme={null}
SELECT GREATEST(3,5,8,9,10);
```

The query will return `3`, the smallest value among the provided values.

```sql  theme={null}
greatest 
---------
     10
```

### Case #2: String Comparison

String comparison is also supported, as shown below:

```sql  theme={null}
SELECT GREATEST('apple', 'banana', 'cherry');
```

In this case, the result will be `'cherry'`, the greatest string according to the order.

```sql  theme={null}
greatest 
----------
 cherry
```

### Case #3: Handling NULL Values

`NULL` values are ignored when determining the greatest value:

```sql  theme={null}
SELECT GREATEST (5,null,9);
```

The result will be the greatest non-NULL value, which is `9`.

```sql  theme={null}
least 
-------
     9
```

### Case #4: Positive and Negative Numbers

Negative numbers can also be compared:

```sql  theme={null}
SELECT GREATEST (4,-4,-8,8);
```

This query will return `8`, the greatest value among the provided numbers.

```sql  theme={null}
least 
-------
    8
```

### Case #5: Using Table Data

The `GREATEST` 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.

```sql  theme={null}
CREATE TABLE Student(
    Student_name TEXT,
    Student_Class TEXT,
    Subject1 INT,
    Subject2 INT,
    Subject3 INT,
    Subject4 INT
);

INSERT INTO  
    Student(Student_name, Student_Class, Subject1, Subject2, Subject3, Subject4)
VALUES
    ('Sayan', 'Junior', 81, 90, 86, 92 ),
    ('Nitin', 'Junior', 90, 84, 88, 91 ),
    ('Aniket', 'Senior', 81, 80, 87, 95 ),
    ('Abdur', 'Junior', 85, 90, 80, 90  ),
    ('Sanjoy', 'Senio', 88, 82, 84, 90 );
```

Use the `SELECT` statement to view all the records:

```sql  theme={null}
SELECT * FROM Student;
```

```sql  theme={null}
student_name | student_class | subject1 | subject2 | subject3 | subject4 
--------------+---------------+----------+----------+----------+----------
 Sayan        | Junior        |       81 |       90 |       86 |       92
 Nitin        | Junior        |       90 |       84 |       88 |       91
 Aniket       | Senior        |       81 |       80 |       87 |       95
 Abdur        | Junior        |       85 |       90 |       80 |       90
 Sanjoy       | Senio         |       88 |       82 |       84 |       90
```

Now, we will find the greatest marks for every student in all subjects.

```sql  theme={null}
Select Student_name, GREATEST(Subject1, Subject2, Subject3, Subject4) AS Greatest_Mark
FROM Student;
```

```sql  theme={null}
student_name | greatest_mark 
--------------+---------------
 Sayan        |            92
 Nitin        |            91
 Aniket       |            95
 Abdur        |            90
 Sanjoy       |            90
```