Problem
Finding the maximum or minimum value among provided values or column values is a common task that database developers face. This article will look at solving this problem using the GREATEST and LEAST functions in Snowflake and SQL Server.
Solution
The GREATEST and LEAST functions are available in almost all popular database management systems to get the maximum or minimum value among provided values. In Snowflake, for example, these functions are called conditional expression functions.
LEAST and GREATEST for Snowflake
The following code gets the maximum value among the provided numbers:
SELECT GREATEST(10, 5, 8, 20, 15) AS max_value;

The LEAST function returns the minimum value:
SELECT LEAST(10, 5, 8, 20, 15) AS min_value;

If we have NULLs in the provided values’ list, Snowflake’s GREATEST and LEAST functions will return NULL:
SELECT LEAST(10, 5, 8, 20, 15) AS min_value;
SELECT GREATEST(10, 5, 8, 20, 15, NULL) AS max_value;


It is worth mentioning that these functions are available in PostgreSQL, Oracle, and MySQL as well.
LEAST and GREATEST for SQL Server
Interestingly, these functions became available only since SQL Server 2022 and are called logical functions. If we try to use these functions in SQL Server 2019 with the latest cumulative update installed, we will receive an error. As of the time of writing this article, the latest cumulative update for SQL Server 2019 was CU22, which is installed in our SQL Server 2019 instance.

Let’s check the version in SQL Server Management Studio (SSMS) and then run the logical functions:
SELECT @@VERSION

SELECT GREATEST(10, 5, 8, 20, 15) AS max_value;
SELECT LEAST(10, 5, 8, 20, 15) AS min_value;


Here is a SQL Server 2022 instance.
SELECT @@VERSION

SELECT GREATEST(10, 5, 8, 20, 15) AS max_value;
SELECT LEAST(10, 5, 8, 20, 15) AS min_value;

Unlike the corresponding functions in Snowflake, discussed logical functions in SQL Server ignore NULLs and return the minimum or maximum values regardless of the existing NULLs in the list. If all values are NULL, the functions return NULL:
SELECT GREATEST(10, 5, 8, 20, 15, NULL) AS max_value;
SELECT LEAST(10, 5, 8, 20, 15, NULL) AS min_value;
SELECT LEAST(NULL, NULL) AS min_value;

LEAST and GREATEST for Other SQL Server Versions
We discussed the topic of finding the maximum/minimum value from multiple columns in SQL Server before in this article: Find MAX value from multiple columns in a SQL Server table (mssqltips.com). These methods are still correct for the SQL Server 2019 and lower versions.
Let’s compare the most efficient method described in the previous article with applying the GREATEST function in SQL Server 2022.
Let’s create the same test environment:
IF (OBJECT_ID('tempdb..##TestTable') IS NOT NULL)
DROP TABLE ##TestTable
CREATE TABLE ##TestTable
(
ID INT IDENTITY(1,1) PRIMARY KEY,
Name NVARCHAR(40),
UpdateByApp1Date DATETIME,
UpdateByApp2Date DATETIME,
UpdateByApp3Date DATETIME
)
INSERT INTO ##TestTable(Name, UpdateByApp1Date, UpdateByApp2Date, UpdateByApp3Date )
VALUES('ABC', '2015-08-05','2015-08-04', '2015-08-06'),
('NewCopmany', '2014-07-05','2012-12-09', '2015-08-14'),
('MyCompany', '2015-03-05','2015-01-14', '2015-07-26')
Now, we will include the actual execution plan in SSMS and will run both queries:
--Finding maximum value among columns using GREATEST
SELECT GREATEST(UpdateByApp1Date,UpdateByApp2Date,UpdateByApp3Date) AS LastUpdateDate
FROM ##TestTable
--Fastest method of finding maximum value among columns from the previous article
SELECT
ID,
(SELECT MAX(LastUpdateDate)
FROM (VALUES (UpdateByApp1Date),(UpdateByApp2Date),(UpdateByApp3Date)) AS UpdateDate(LastUpdateDate))
AS LastUpdateDate
FROM ##TestTable

Looking at the execution plans, we can see that the performance of both queries is the same:

However, the code is more compact and easier to understand in case of using the GREATEST function.
Conclusion
The GREATEST and LEAST functions provide a compact way to find maximum and minimum values from the list and are available in popular database management systems. In SQL Server, they are available starting with SQL Server 2022. However, the same problem can be solved using alternative methods in older versions of SQL Server.
Next Steps
For additional information, please follow the links below:
- GREATEST (Transact-SQL) – SQL Server | Microsoft Learn
- GREATEST | Snowflake Documentation
- Find MAX value from multiple columns in a SQL Server table (mssqltips.com)
- PostgreSQL: Documentation: 15: 9.18. Conditional Expressions
- GREATEST (oracle.com)
- MySQL :: MySQL 8.0 Reference Manual :: 12.4.2 Comparison Functions and Operators

Sergey Gigoyan (LinkedIn) is a Senior Technical Architect specializing in data and databases with more than 15 years of experience. Sergey focuses on modern data architectures, database design and development, performance tuning and optimization, high availability solutions, BI development and DW design. He has worked with SQL Server, Oracle, and PostgreSQL databases, as well as cloud-based data solutions (AWS and Azure). Sergey also has extensive experience with modern data stacks such as Snowflake and dbt.
Sergey’s experience spans various industries. He had the privilege of working with IT giants such as Oracle as a Principal Data Engineer and BlackBerry as well as innovative startups. He helped deliver complex database solutions and advanced data strategies.
Sergey is also the author of “Building a Successful Career in IT – How I Did It” where he provides actionable advice on thriving in the ever-evolving IT industry.
- MSSQLTips Awards: Champion (100+ tips) – 2024 | Author of the Year – 2020



Hi William,
That is not a function it is just a name that is given.
You can make it anything you want UpdateDate or UDate, etc..
SELECT
ID,
(SELECT MAX(LastUpdateDate)
FROM (VALUES (UpdateByApp1Date),(UpdateByApp2Date),(UpdateByApp3Date)) AS UDate(LastUpdateDate) )
AS LastUpdateDate
FROM ##TestTable
I’m talking about the UpdateDate() method. I’m not disputing the Values() method.
Where did the UpdateDate(LastUpdateDate) come from and where is that in Table constructors?
William Bailey,
It’s not a function. The following article can be helpful:
https://www.mssqltips.com/sqlservertip/4067/find-max-value-from-multiple-columns-in-a-sql-server-table/
Also, checking the “Table Value Constructor” syntax can be useful:
https://learn.microsoft.com/en-us/sql/t-sql/queries/table-value-constructor-transact-sql?view=sql-server-ver16
Thanks,
Sergey Gigoyan
Stupid question:
Where did this method come from; UpdateDate(LastUpdateDate).
As long as I’ve been using Sql Server I’ve never run across UpdateDate function in T-SQL. I don’t see it referenced or defined anywhere before it’s being used, which is adding to my confusion.