|
| 1 | +--- |
| 2 | +description: "Logical Functions - GREATEST (Transact-SQL)" |
| 3 | +title: "GREATEST (Transact-SQL) | Microsoft Docs" |
| 4 | +ms.custom: "" |
| 5 | +ms.date: "04/09/2021" |
| 6 | +ms.prod: sql |
| 7 | +ms.prod_service: "database-engine, sql-database" |
| 8 | +ms.reviewer: "" |
| 9 | +ms.technology: t-sql |
| 10 | +ms.topic: reference |
| 11 | +f1_keywords: |
| 12 | + - "GREATEST" |
| 13 | + - "GREATEST_TSQL" |
| 14 | +dev_langs: |
| 15 | + - "TSQL" |
| 16 | +helpviewer_keywords: |
| 17 | + - "GREATEST function" |
| 18 | +ms.assetid: 1c382c83-7500-4bae-bbdc-c1dbebd3d83f |
| 19 | +author: jmsteen |
| 20 | +ms.author: josteen |
| 21 | +--- |
| 22 | +# Logical Functions - GREATEST (Transact-SQL) |
| 23 | +[!INCLUDE [SQL Server Azure SQL Database ](../../includes/applies-to-version/sql-asdb.md)] |
| 24 | + |
| 25 | +**_NOTE:_** GREATEST is currently available for Azure SQL Database, Azure SQL Managed Instance and serverless SQL pools in Azure Synapse Analytics. |
| 26 | + |
| 27 | + This function returns the maximum value from a list of one or more expressions. |
| 28 | + |
| 29 | +  [Transact-SQL Syntax Conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md) |
| 30 | + |
| 31 | +## Syntax |
| 32 | + |
| 33 | +```syntaxsql |
| 34 | +GREATEST ( expression1 [ ,...expressionN ] ) |
| 35 | +``` |
| 36 | + |
| 37 | +## Arguments |
| 38 | + *expression1, expressionN* |
| 39 | + A list of comma-separated expressions of any comparable data type. The `GREATEST` function requires at least one argument and supports no more than 254 arguments. |
| 40 | + |
| 41 | + Each expression can be a constant, variable, column name or function, as well as any combination of arithmetic, bitwise and string operators. Aggregate functions and scalar subqueries are permitted. |
| 42 | + |
| 43 | +## Return Types |
| 44 | + Returns the data type with the highest precedence from the set of types passed to the function. For more information, see [Data Type Precedence (Transact-SQL)](../../t-sql/data-types/data-type-precedence-transact-sql.md). |
| 45 | + |
| 46 | + If all arguments have the same data type and the type is supported for comparison, `GREATEST` will return that type. |
| 47 | + |
| 48 | + Otherwise, the function will implicitly convert all arguments to the data type of the highest precedence before comparison and use this type as the return type. |
| 49 | + |
| 50 | + For numeric types, the scale of the return type will be the same as the highest precedence argument, or the largest scale if more than one argument is of the highest precedence data type. |
| 51 | + |
| 52 | +## Remarks |
| 53 | + All expressions in the list of arguments must be of a data type that is comparable and that can be implicitly converted to the data type of the argument with the highest precedence. |
| 54 | + |
| 55 | + Implicit conversion of all arguments to the highest precedence data type takes place before comparison. |
| 56 | + |
| 57 | + If implicit type conversion between the arguments is not supported, the function will fail and return an error. |
| 58 | + |
| 59 | + See [Data Type Conversion (Database Engine)](../../t-sql/data-types/data-type-conversion-database-engine.md) for further details on implicit and explicit conversion. |
| 60 | + |
| 61 | + If one or more arguments are not null, then null arguments will be ignored during comparison. If all arguments are null, then `GREATEST` will return null. |
| 62 | + |
| 63 | + Comparison of character arguments follows the rules of [Collation Precedence (Transact-SQL)](../../t-sql/statements/collation-precedence-transact-sql.md). |
| 64 | + |
| 65 | + The following types are **not** supported for comparison in `GREATEST`: **varchar(max), varbinary(max) or nvarchar(max) exceeding 8,000 bytes, cursor, geometry, geography, image, non-byte-ordered user-defined types, ntext, table, text** and **xml**. |
| 66 | + |
| 67 | + The varchar(max), varbinary(max) and nvarchar(max) data types are supported for arguments that are 8,000 bytes or below, and will be implicitly converted to varchar(n), varbinary(n) and nvarchar(n), respectively, prior to comparison. |
| 68 | + |
| 69 | + For example, varchar(max) can support up to 8,000 characters if using a single-byte encoding character set, and nvarchar(max) can support up to 4,000 byte-pairs (assuming UTF-16 character encoding). |
| 70 | + |
| 71 | +## Examples |
| 72 | + |
| 73 | +### A. Simple example |
| 74 | + |
| 75 | + The following example returns the maximum value from the list of constants that is provided. |
| 76 | + |
| 77 | + The scale of the return type is determined by that of the argument with the highest precedence data type. |
| 78 | + |
| 79 | +```sql |
| 80 | +SELECT GREATEST ( '6.62', 3.1415, N'7' ) AS Greatest; |
| 81 | +GO |
| 82 | +``` |
| 83 | + |
| 84 | + [!INCLUDE[ssResult](../../includes/ssresult-md.md)] |
| 85 | + |
| 86 | +``` |
| 87 | +Greatest |
| 88 | +-------- |
| 89 | + 7.0000 |
| 90 | +
|
| 91 | +(1 rows affected) |
| 92 | +``` |
| 93 | + |
| 94 | +### B. Simple example with character types |
| 95 | + |
| 96 | + The following example returns the maximum value from the list of character constants that is provided. |
| 97 | + |
| 98 | +```sql |
| 99 | +SELECT GREATEST ('Glacier', N'Joshua Tree', 'Mount Rainier') AS Greatest; |
| 100 | +GO |
| 101 | +``` |
| 102 | + |
| 103 | + [!INCLUDE[ssResult](../../includes/ssresult-md.md)] |
| 104 | + |
| 105 | +``` |
| 106 | +Greatest |
| 107 | +------------- |
| 108 | +Mount Rainier |
| 109 | +
|
| 110 | +(1 rows affected) |
| 111 | +``` |
| 112 | + |
| 113 | +### C. Simple example with table |
| 114 | + |
| 115 | + This example returns the maximum value from a list of column arguments and ignores `NULL` values during comparison. |
| 116 | + |
| 117 | +```sql |
| 118 | +USE AdventureWorks2019; |
| 119 | +GO |
| 120 | + |
| 121 | +SELECT sp.SalesQuota, sp.SalesYTD, sp.SalesLastYear |
| 122 | + , GREATEST(sp.SalesQuota, sp.SalesYTD, sp.SalesLastYear) AS Greatest |
| 123 | +FROM sales.SalesPerson AS sp |
| 124 | +WHERE sp.SalesYTD < 3000000; |
| 125 | +GO |
| 126 | + |
| 127 | +``` |
| 128 | + |
| 129 | + [!INCLUDE[ssResult](../../includes/ssresult-md.md)] |
| 130 | + |
| 131 | +``` |
| 132 | +SalesQuota SalesYTD SalesLastYear Greatest |
| 133 | +
|
| 134 | +--------------------- --------------------- --------------------- --------------------- |
| 135 | + NULL 559697.5639 .0000 559697.5639 |
| 136 | + 250000.0000 1453719.4653 1620276.8966 1620276.8966 |
| 137 | + 300000.0000 2315185.6110 1849640.9418 2315185.6110 |
| 138 | + 250000.0000 1352577.1325 1927059.1780 1927059.1780 |
| 139 | + 250000.0000 2458535.6169 2073505.9999 2458535.6169 |
| 140 | + 250000.0000 2604540.7172 2038234.6549 2604540.7172 |
| 141 | + 250000.0000 1573012.9383 1371635.3158 1573012.9383 |
| 142 | + 300000.0000 1576562.1966 .0000 1576562.1966 |
| 143 | + NULL 172524.4512 .0000 172524.4512 |
| 144 | + 250000.0000 1421810.9242 2278548.9776 2278548.9776 |
| 145 | + NULL 519905.9320 .0000 519905.9320 |
| 146 | + 250000.0000 1827066.7118 1307949.7917 1827066.7118 |
| 147 | +
|
| 148 | +(12 rows affected) |
| 149 | + |
| 150 | +``` |
| 151 | +### D. Using `GREATEST` with local variables |
| 152 | + |
| 153 | + This example uses `GREATEST` to determine the maximum value of a list of local variables within the predicate of a `WHERE` clause. |
| 154 | + |
| 155 | +```sql |
| 156 | +CREATE TABLE studies ( |
| 157 | + Variable varchar(10) NOT NULL, |
| 158 | + Correlation decimal(4, 3) NULL |
| 159 | +); |
| 160 | + |
| 161 | +INSERT INTO studies VALUES ('Var1', 0.2), ('Var2', 0.825), ('Var3', 0.61); |
| 162 | +GO |
| 163 | + |
| 164 | +DECLARE @PredictionA DECIMAL(2,1) = 0.7; |
| 165 | +DECLARE @PredictionB DECIMAL(3,1) = 0.65; |
| 166 | + |
| 167 | +SELECT Variable, Correlation |
| 168 | +FROM studies |
| 169 | +WHERE Correlation > GREATEST(@PredictionA, @PredictionB); |
| 170 | +GO |
| 171 | +``` |
| 172 | + |
| 173 | + [!INCLUDE[ssResult](../../includes/ssresult-md.md)] |
| 174 | + |
| 175 | +``` |
| 176 | +Variable Correlation |
| 177 | +---------- ----------- |
| 178 | +Var2 .825 |
| 179 | +
|
| 180 | +(1 rows affected) |
| 181 | +``` |
| 182 | + |
| 183 | +### E. Using `GREATEST` with columns, constants and variables |
| 184 | + |
| 185 | + This example uses `GREATEST` to determine the maximum value of a list that includes columns, constants and variables. |
| 186 | + |
| 187 | +```sql |
| 188 | +CREATE TABLE products ( |
| 189 | + prod_id int IDENTITY(1,1), |
| 190 | + listprice smallmoney NULL |
| 191 | +); |
| 192 | + |
| 193 | +INSERT INTO products VALUES (14.99), (49.99), (24.99); |
| 194 | +GO |
| 195 | + |
| 196 | +DECLARE @PriceX smallmoney = 19.99; |
| 197 | + |
| 198 | +SELECT GREATEST(listprice, 0, @PriceX) as GreatestPrice |
| 199 | +FROM products; |
| 200 | +GO |
| 201 | +``` |
| 202 | + |
| 203 | + [!INCLUDE[ssResult](../../includes/ssresult-md.md)] |
| 204 | + |
| 205 | +``` |
| 206 | +GreatestPrice |
| 207 | +------------- |
| 208 | + 19.9900 |
| 209 | + 49.9900 |
| 210 | + 24.9900 |
| 211 | +
|
| 212 | +(3 rows affected) |
| 213 | +``` |
| 214 | + |
| 215 | + |
| 216 | +## See Also |
| 217 | + [LEAST (Transact-SQL)](../../t-sql/functions/logical-functions-least-transact-sql.md) |
| 218 | + [MAX (Transact-SQL)](../../t-sql/functions/max-transact-sql.md) |
| 219 | + [MIN (Transact-SQL)](../../t-sql/functions/min-transact-sql.md) |
| 220 | + [CASE (Transact-SQL)](../../t-sql/language-elements/case-transact-sql.md) |
| 221 | + [CHOOSE (Transact-SQL)](../../t-sql/functions/logical-functions-choose-transact-sql.md) |
| 222 | + |
| 223 | + |
0 commit comments