Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

GREATEST and LEAST functions

Focus mode
GREATEST and LEAST functions - Amazon Redshift

Returns the largest or smallest value from a list of any number of expressions.

Syntax

GREATEST (value [, ...]) LEAST (value [, ...])

Parameters

expression_list

A comma-separated list of expressions, such as column names. The expressions must all be convertible to a common data type. NULL values in the list are ignored. If all of the expressions evaluate to NULL, the result is NULL.

Returns

Returns the greatest (for GREATEST) or least (for LEAST) value from the provided list of expressions.

Example

The following example returns the highest value alphabetically for firstname or lastname.

select firstname, lastname, greatest(firstname,lastname) from users where userid < 10 order by 3; firstname | lastname | greatest -----------+-----------+----------- Lars | Ratliff | Ratliff Reagan | Hodge | Reagan Colton | Roy | Roy Barry | Roy | Roy Tamekah | Juarez | Tamekah Rafael | Taylor | Taylor Victor | Hernandez | Victor Vladimir | Humphrey | Vladimir Mufutau | Watkins | Watkins (9 rows)

On this page

PrivacySite termsCookie preferences
© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved.