Categories: TSQL

The GREATEST new T-SQL Function

If the title isn’t obvious enough, this blog post is about the new function GREATEST, and it’s sibling LEAST. But wait, how come there’s a new T-SQL function? There isn’t a new release of SQL Server! That’s right, those two new functions are released in Azure SQL DB, Azure SQL Managed Instance and Azure Synapse Analytics. In other words, cloud first, on-prem later. I learned about those functions when reading the blog post Your Top 10 Ideas for SQL Server by Brent Ozar, where he subtle tricks people in commenting about which features they want in SQL Server, and then he scolds them for not having submit a feature request 😉 Anyway, someone in the comments mentioned the discovery of the 2 new functions.

So what do they do exactly? You can look at GREATEST as a MAX function, but for columns instead of rows. Let’s demonstrate with an example. We got the following simple table:

SELECT 1 AS Col1,2 AS Col2,3 AS Col3
INTO #Test
UNION ALL
SELECT 6,4,5
UNION ALL
SELECT 10,-5,9;

SELECT *
FROM #Test;

To get the highest value out of the 3 columns for each row, we can now use the following statement:

SELECT GREATEST(Col1,Col2,Col3)
FROM #Test;

As you can see from the screenshot, Intellisense doesn’t recognize this great function (pun intended) yet. I really like this addition, as in ye olde times we had to write a lot of nested CASE/IIF functions to get the job done (or you could pivot/unpivot the data and use MAX if that’s your thing). And it only got more harder if more columns were involved. Also, it was one of the things that Oracle had, but SQL Server didn’t. So it gets easier to migrate PL-SQL to T-SQL 🙂

LEAST does the opposite of course:

The functions work with any comparable data type, such as dates:

And luckily, they ignore NULL values:

UPDATE: I’ve used “column” throughout this blog post, but “expressions” is more accurate. This can be a column, a subquery, a variable, a calculation and so on.


------------------------------------------------
Do you like this blog post? You can thank me by buying me a beer 🙂
Koen Verbeeck

Koen Verbeeck is a Microsoft Business Intelligence consultant at AE, helping clients to get insight in their data. Koen has a comprehensive knowledge of the SQL Server BI stack, with a particular love for Integration Services. He's also a speaker at various conferences.

Recent Posts

Free webinar – Tackling the Gaps and Islands Problem with T-SQL Window Functions

I'm hosting a free webinar at MSSQLTips.com at the 19th of December 2024, 6PM UTC.…

5 days ago

dataMinds Connect 2024 – Session Materials

The slides and scripts for my session "Tackling the Gaps & Islands Problem with T-SQL…

4 weeks ago

Connect to Power BI as a Guest User in another Tenant

Sometimes your Microsoft Entra ID account (formerly known as Azure Active Directory) is added as…

2 months ago

How to use a Script Activity in ADF as a Lookup

In Azure Data Factory (ADF, but also Synapse Pipelines and Fabric Pipelines), you have a…

4 months ago

Database Build Error – Incorrect syntax near DISTINCT

I wrote a piece of SQL that had some new T-SQL syntax in it: IS…

4 months ago

Speaking at dataMinds Connect 2024

I'm very excited to announce I've been selected as a speaker for dataMinds Connect 2024,…

5 months ago