# GREATEST

> For the complete documentation index, see [llms.txt](https://docs.redpanda.com/llms.txt). Component-specific: [cloud-data-platform-full.txt](https://docs.redpanda.com/cloud-data-platform-full.txt)

---
title: GREATEST
latest-operator-version: v26.1.4
latest-console-tag: v3.7.3
latest-connect-version: 4.93.0
latest-redpanda-tag: v26.1.9
docname: sql/sql-functions/math-functions/greatest
page-component-name: cloud-data-platform
page-version: master
page-component-version: master
page-component-title: Cloud
page-relative-src-path: sql/sql-functions/math-functions/greatest.adoc
page-edit-url: https://github.com/redpanda-data/cloud-docs/edit/main/modules/reference/pages/sql/sql-functions/math-functions/greatest.adoc
description: The `GREATEST` function returns the largest value from a set of values.
page-topic-type: reference
page-git-created-date: "2026-05-26"
page-git-modified-date: "2026-05-26"
---

<!-- Source: https://docs.redpanda.com/cloud-data-platform/reference/sql/sql-functions/math-functions/greatest.md -->

The `GREATEST` function returns the greatest value from a set of values. The arguments must be of compatible types. For example, comparing a text value with a number returns an error.

## [](#syntax)Syntax

The syntax for this function is:

```sql
GREATEST(value_1, [value_n])
```

Where:

-   `value_1`: Represents the first value.

-   `value_n`: Represents one or more additional values, separated by commas.


> 📝 **NOTE**
>
> -   `NULL` values within the expressions are ignored.
>
> -   The result is `NULL` if all expressions evaluate to `NULL`.

## [](#examples)Examples

### [](#basic-usage)Basic usage

```sql
SELECT GREATEST(3,5,8,9,10);
```

The query returns `10`, the greatest value among the provided values.

```sql
greatest
---------
     10
```

### [](#string-comparison)String comparison

String comparison is also supported:

```sql
SELECT GREATEST('apple', 'banana', 'cherry');
```

The query returns `'cherry'`, the greatest string in lexicographic order.

```sql
greatest
----------
 cherry
```

### [](#handle-null-values)Handle NULL values

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

```sql
SELECT GREATEST (5,null,9);
```

The query returns `9`, the greatest non-NULL value.

```sql
greatest
----------
        9
```

### [](#positive-and-negative-numbers)Positive and negative numbers

Negative numbers can also be compared:

```sql
SELECT GREATEST (4,-4,-8,8);
```

The query returns `8`, the greatest value among the provided numbers.

```sql
greatest
----------
        8
```

### [](#use-table-data)Use table data

You can also use `GREATEST` to find the greatest value across columns. For example, create a table named `Student` that stores student names and scores.

```sql
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', 'Senior', 88, 82, 84, 90 );
```

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

```sql
SELECT * FROM Student;
```

```sql
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       | Senior        |       88 |       82 |       84 |       90
```

Find the greatest mark for each student across all subjects:

```sql
Select Student_name, GREATEST(Subject1, Subject2, Subject3, Subject4) AS Greatest_Mark
FROM Student;
```

```sql
student_name | greatest_mark
--------------+---------------
 Sayan        |            92
 Nitin        |            91
 Aniket       |            95
 Abdur        |            90
 Sanjoy       |            90
```