Skip to content

Commit 89f4609

Browse files
Merge pull request #35749 from rwestMSFT/rw-1105-fix-10213
Update MAX and MIN functions to point to GREATEST and LEAST (PR 10213)
2 parents 075907f + 44492ba commit 89f4609

2 files changed

Lines changed: 319 additions & 273 deletions

File tree

Lines changed: 145 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
---
22
title: "MAX (Transact-SQL)"
3-
description: "MAX (Transact-SQL)"
3+
description: MAX returns the maximum of all values of the specified expression in a group.
44
author: markingmyname
55
ms.author: maghan
6-
ms.date: "08/23/2017"
6+
ms.reviewer: randolphwest
7+
ms.date: 11/18/2025
78
ms.service: sql
89
ms.subservice: t-sql
910
ms.topic: reference
@@ -15,127 +16,152 @@ helpviewer_keywords:
1516
- "values [SQL Server], maximum"
1617
- "maximum values [SQL Server]"
1718
dev_langs:
18-
- "TSQL"
19-
monikerRange: ">= aps-pdw-2016 || = azuresqldb-current || = azure-sqldw-latest || >= sql-server-2016 || >= sql-server-linux-2017 || = azuresqldb-mi-current||=fabric"
19+
- TSQL
20+
monikerRange: ">=aps-pdw-2016 || =azuresqldb-current || =azure-sqldw-latest || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current || =fabric"
2021
---
2122
# MAX (Transact-SQL)
23+
2224
[!INCLUDE [sql-asdb-asdbmi-asa-pdw-fabricse-fabricdw](../../includes/applies-to-version/sql-asdb-asdbmi-asa-pdw-fabricse-fabricdw.md)]
2325

24-
Returns the maximum value in the expression.
25-
26-
:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: [Transact-SQL syntax conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
27-
28-
## Syntax
29-
30-
```syntaxsql
31-
-- Aggregation Function Syntax
32-
MAX( [ ALL | DISTINCT ] expression )
33-
34-
-- Analytic Function Syntax
35-
MAX ([ ALL ] expression) OVER ( <partition_by_clause> [ <order_by_clause> ] )
36-
```
37-
26+
Returns the maximum of all values of the specified expression in a group. Can be followed by the [OVER](../queries/select-over-clause-transact-sql.md) clause.
27+
28+
> [!NOTE]
29+
> To get the maximum of all values of multiple expressions inline, see [Logical functions - GREATEST](logical-functions-greatest-transact-sql.md).
30+
31+
:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: [Transact-SQL syntax conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
32+
33+
## Syntax
34+
35+
Aggregation function syntax:
36+
37+
```syntaxsql
38+
MAX ( [ ALL | DISTINCT ] expression )
39+
```
40+
41+
Analytic function syntax:
42+
43+
```syntaxsql
44+
MAX ( [ ALL ] expression) OVER ( [ <partition_by_clause> ] [ <order_by_clause> ] )
45+
```
46+
3847
## Arguments
39-
**ALL**
40-
Applies the aggregate function to all values. ALL is the default.
41-
42-
DISTINCT
43-
Specifies that each unique value is considered. DISTINCT is not meaningful with MAX and is available for ISO compatibility only.
44-
45-
*expression*
46-
Is a constant, column name, or function, and any combination of arithmetic, bitwise, and string operators. MAX can be used with **numeric**, **character**, **uniqueidentifier**, and **datetime** columns, but not with **bit** columns. Aggregate functions and subqueries are not permitted.
47-
48-
For more information, see [Expressions &#40;Transact-SQL&#41;](../../t-sql/language-elements/expressions-transact-sql.md).
49-
50-
OVER **(** _partition\_by\_clause_ [ _order\_by\_clause_ ] **)**
51-
*partition_by_clause* divides the result set produced by the FROM clause into partitions to which the function is applied. If not specified, the function treats all rows of the query result set as a single group. *order_by_clause* determines the logical order in which the operation is performed. *_partition\_by\_clause_* is required. For more information, see [OVER Clause &#40;Transact-SQL&#41;](../../t-sql/queries/select-over-clause-transact-sql.md).
52-
53-
## Return Types
54-
Returns a value same as *expression*.
55-
56-
## Remarks
57-
MAX ignores any null values.
58-
59-
MAX returns NULL when there is no row to select.
60-
61-
For character columns, MAX finds the highest value in the collating sequence.
62-
63-
MAX is a deterministic function when used without the OVER and ORDER BY clauses. It is nondeterministic when specified with the OVER and ORDER BY clauses. For more information, see [Deterministic and Nondeterministic Functions](../../relational-databases/user-defined-functions/deterministic-and-nondeterministic-functions.md).
64-
65-
## Examples
66-
67-
### A. Simple example
68-
The following example returns the highest (maximum) tax rate in the [!INCLUDE[ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database.
69-
70-
```sql
71-
SELECT MAX(TaxRate)
72-
FROM Sales.SalesTaxRate;
73-
GO
74-
```
75-
76-
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
77-
78-
```
79-
-------------------
80-
19.60
81-
Warning, null value eliminated from aggregate.
82-
83-
(1 row(s) affected)
84-
```
85-
86-
### B. Using the OVER clause
87-
The following example uses the MIN, MAX, AVG, and COUNT functions with the OVER clause to provide aggregated values for each department in the `HumanResources.Department` table in the [!INCLUDE[ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database.
88-
89-
```sql
90-
SELECT DISTINCT Name
91-
, MIN(Rate) OVER (PARTITION BY edh.DepartmentID) AS MinSalary
92-
, MAX(Rate) OVER (PARTITION BY edh.DepartmentID) AS MaxSalary
93-
, AVG(Rate) OVER (PARTITION BY edh.DepartmentID) AS AvgSalary
94-
,COUNT(edh.BusinessEntityID) OVER (PARTITION BY edh.DepartmentID) AS EmployeesPerDept
95-
FROM HumanResources.EmployeePayHistory AS eph
96-
JOIN HumanResources.EmployeeDepartmentHistory AS edh
97-
ON eph.BusinessEntityID = edh.BusinessEntityID
98-
JOIN HumanResources.Department AS d
99-
ON d.DepartmentID = edh.DepartmentID
100-
WHERE edh.EndDate IS NULL
101-
ORDER BY Name;
102-
```
103-
104-
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
105-
106-
```
107-
Name MinSalary MaxSalary AvgSalary EmployeesPerDept
108-
----------------------------- --------------------- --------------------- --------------------- ----------------
109-
Document Control 10.25 17.7885 14.3884 5
110-
Engineering 32.6923 63.4615 40.1442 6
111-
Executive 39.06 125.50 68.3034 4
112-
Facilities and Maintenance 9.25 24.0385 13.0316 7
113-
Finance 13.4615 43.2692 23.935 10
114-
Human Resources 13.9423 27.1394 18.0248 6
115-
Information Services 27.4038 50.4808 34.1586 10
116-
Marketing 13.4615 37.50 18.4318 11
117-
Production 6.50 84.1346 13.5537 195
118-
Production Control 8.62 24.5192 16.7746 8
119-
Purchasing 9.86 30.00 18.0202 14
120-
Quality Assurance 10.5769 28.8462 15.4647 6
121-
Research and Development 40.8654 50.4808 43.6731 4
122-
Sales 23.0769 72.1154 29.9719 18
123-
Shipping and Receiving 9.00 19.2308 10.8718 6
124-
Tool Design 8.62 29.8462 23.5054 6
125-
126-
(16 row(s) affected)
127-
```
128-
129-
### C. Using MAX with character data
130-
The following example returns the database name that sorts as the last name alphabetically. The example uses `WHERE database_id < 5`, to consider only the system databases.
131-
```sql
132-
SELECT MAX(name) FROM sys.databases WHERE database_id < 5;
48+
49+
#### ALL
50+
51+
Applies the aggregate function to all values. `ALL` is the default.
52+
53+
#### DISTINCT
54+
55+
Specifies that each unique value is considered. `DISTINCT` isn't meaningful with `MAX`, and is available for ISO compatibility only.
56+
57+
#### *expression*
58+
59+
A constant, column name, or function, and any combination of arithmetic, bitwise, and string operators. `MAX` can be used with **numeric**, **char**, **nchar**, **varchar**, **nvarchar**, **uniqueidentifier**, or **datetime** columns, but not with **bit** columns. Aggregate functions and subqueries aren't permitted.
60+
61+
For more information, see [Expressions](../language-elements/expressions-transact-sql.md).
62+
63+
#### OVER ( [ *partition_by_clause* ] [ *order_by_clause* ] )
64+
65+
*partition_by_clause* divides the result set produced by the `FROM` clause into partitions to which the function is applied. If not specified, the function treats all rows of the query result set as a single group.
66+
67+
*order_by_clause* determines the logical order in which the operation is performed.
68+
69+
For more information, see [SELECT - OVER clause](../queries/select-over-clause-transact-sql.md).
70+
71+
## Return types
72+
73+
Returns a value same as *expression*.
74+
75+
## Remarks
76+
77+
`MAX` ignores any null values.
78+
79+
`MAX` returns `NULL` when there's no row to select.
80+
81+
For character columns, `MAX` finds the highest value in the collating sequence.
82+
83+
`MAX` is a deterministic function when used without the `OVER` and `ORDER BY` clauses. It's nondeterministic when specified with the `OVER` and `ORDER BY` clauses. For more information, see [Deterministic and nondeterministic functions](../../relational-databases/user-defined-functions/deterministic-and-nondeterministic-functions.md).
84+
85+
`MAX` operates on a set of rows. To obtain the maximum value for a set of values inline, see [Logical functions - GREATEST](logical-functions-greatest-transact-sql.md).
86+
87+
## Examples
88+
89+
[!INCLUDE [article-uses-adventureworks](../../includes/article-uses-adventureworks.md)]
90+
91+
### A. Basic example
92+
93+
The following example returns the highest (maximum) tax rate.
94+
95+
```sql
96+
SELECT MAX(TaxRate)
97+
FROM Sales.SalesTaxRate;
98+
GO
99+
```
100+
101+
[!INCLUDE [ssResult](../../includes/ssresult-md.md)]
102+
103+
```output
104+
-------------------
105+
19.60
106+
Warning, null value eliminated from aggregate.
133107
```
134-
The last system database is `tempdb`.
135-
136-
## See Also
137-
[Aggregate Functions &#40;Transact-SQL&#41;](../../t-sql/functions/aggregate-functions-transact-sql.md)
138-
[OVER Clause &#40;Transact-SQL&#41;](../../t-sql/queries/select-over-clause-transact-sql.md)
139-
140-
141108

109+
### B. Use the OVER clause
110+
111+
The following example uses the `MIN`, `MAX`, `AVG`, and `COUNT` functions with the `OVER` clause to provide aggregated values for each department in the `HumanResources.Department` table.
112+
113+
```sql
114+
SELECT DISTINCT Name,
115+
MIN(Rate) OVER (PARTITION BY edh.DepartmentID) AS MinSalary,
116+
MAX(Rate) OVER (PARTITION BY edh.DepartmentID) AS MaxSalary,
117+
AVG(Rate) OVER (PARTITION BY edh.DepartmentID) AS AvgSalary,
118+
COUNT(edh.BusinessEntityID) OVER (PARTITION BY edh.DepartmentID) AS EmployeesPerDept
119+
FROM HumanResources.EmployeePayHistory AS eph
120+
INNER JOIN HumanResources.EmployeeDepartmentHistory AS edh
121+
ON eph.BusinessEntityID = edh.BusinessEntityID
122+
INNER JOIN HumanResources.Department AS d
123+
ON d.DepartmentID = edh.DepartmentID
124+
WHERE edh.EndDate IS NULL
125+
ORDER BY Name;
126+
```
127+
128+
[!INCLUDE [ssResult](../../includes/ssresult-md.md)]
129+
130+
```output
131+
Name MinSalary MaxSalary AvgSalary EmployeesPerDept
132+
----------------------------- --------------------- --------------------- --------------------- ----------------
133+
Document Control 10.25 17.7885 14.3884 5
134+
Engineering 32.6923 63.4615 40.1442 6
135+
Executive 39.06 125.50 68.3034 4
136+
Facilities and Maintenance 9.25 24.0385 13.0316 7
137+
Finance 13.4615 43.2692 23.935 10
138+
Human Resources 13.9423 27.1394 18.0248 6
139+
Information Services 27.4038 50.4808 34.1586 10
140+
Marketing 13.4615 37.50 18.4318 11
141+
Production 6.50 84.1346 13.5537 195
142+
Production Control 8.62 24.5192 16.7746 8
143+
Purchasing 9.86 30.00 18.0202 14
144+
Quality Assurance 10.5769 28.8462 15.4647 6
145+
Research and Development 40.8654 50.4808 43.6731 4
146+
Sales 23.0769 72.1154 29.9719 18
147+
Shipping and Receiving 9.00 19.2308 10.8718 6
148+
Tool Design 8.62 29.8462 23.5054 6
149+
```
150+
151+
### C. Use MAX with character data
152+
153+
The following example returns the database name that sorts using the database name alphabetically. The example uses `WHERE database_id < 5`, to select the system databases.
154+
155+
```sql
156+
SELECT MAX(name)
157+
FROM sys.databases
158+
WHERE database_id < 5;
159+
```
160+
161+
The last system database is `tempdb`.
162+
163+
## Related content
164+
165+
- [Aggregate Functions (Transact-SQL)](aggregate-functions-transact-sql.md)
166+
- [Logical functions - GREATEST (Transact-SQL)](logical-functions-greatest-transact-sql.md)
167+
- [SELECT - OVER clause (Transact-SQL)](../queries/select-over-clause-transact-sql.md)

0 commit comments

Comments
 (0)