website logo
⌘K
🏠Homepage
πŸ‘‹Introduction
Key Concepts & Architecture
πŸš€Run Oxla in 2 minutes
πŸƒβ€β™‚οΈRun Oxla on S3
πŸ›«Multi-node Deployment
πŸ‘¨β€πŸ’»SQL Reference
SQL Queries
SQL Clauses
SQL Data Types
SQL Functions
Schema
Comment Support
🚨Error Handling
πŸ†šDifferences Between Oxla vs. PostgreSQL
πŸ“ˆPublic Metrics
Docs powered by
Archbee
SQL Reference
...
SQL Functions
Numeric Functions

GREATEST

17min

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:

Pgsql
|
GREATEST(value_1, [value_n])
ο»Ώ

Where:

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

Info:

  • 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 the GREATEST() function:

Case #1: Basic Usage

Consider the following example:

Pgsql
|
SELECT GREATEST(3,5,8,9,10);
ο»Ώ

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

Pgsql
|
greatest 
---------
     10
ο»Ώ

Case #2: String Comparison

String comparison is also supported, as shown below:

Pgsql
|
SELECT GREATEST('apple', 'banana', 'cherry');
ο»Ώ

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

Pgsql
|
greatest 
----------
 cherry
ο»Ώ

Case #3: Handling NULL Values

NULL values are ignored when determining the greatest value:

Pgsql
|
SELECT GREATEST (5,null,9);
ο»Ώ

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

Pgsql
|
least 
-------
     9
ο»Ώ

Case #4: Positive and Negative Numbers

Negative numbers can also be compared:

Pgsql
|
SELECT GREATEST (4,-4,-8,8);
ο»Ώ

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

Pgsql
|
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.

Pgsql
|
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:

Pgsql
|
SELECT * FROM Student;
ο»Ώ
Pgsql
|
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.

Pgsql
|
Select Student_name, GREATEST(Subject1, Subject2, Subject3, Subject4) AS Greatest_Mark
FROM Student;
ο»Ώ
Pgsql
|
student_name | greatest_mark 
--------------+---------------
 Sayan        |            92
 Nitin        |            91
 Aniket       |            95
 Abdur        |            90
 Sanjoy       |            90
ο»Ώ

ο»Ώ

Updated 31 Aug 2023
Did this page help you?
PREVIOUS
LEAST
NEXT
Aggregation Functions
Docs powered by
Archbee
TABLE OF CONTENTS
Overview
Syntax
Examples
Case #1: Basic Usage
Case #2: String Comparison
Case #3: Handling NULL Values
Case #4: Positive and Negative Numbers
Case #5: Using Table Data
Docs powered by
Archbee

Dedicated to data analytics that provides 10x faster execution of analytical queries than current state of the art solutions. We are launching SOON! Join the waiting list for more info.




Β©2023 Oxla