Skip to content

Commit 44492ba

Browse files
committed
Edit pass
1 parent 58c0d33 commit 44492ba

2 files changed

Lines changed: 319 additions & 281 deletions

File tree

Lines changed: 145 additions & 123 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,131 +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 of all values of the specified expression in a group. May be followed by the [OVER clause](../../t-sql/queries/select-over-clause-transact-sql.md)
25-
If you want the maximum of all values of multiple expressions inline then look at [GREATEST](logical-functions-greatest-transact-sql.md)
26-
27-
:::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)
28-
29-
## Syntax
30-
31-
```syntaxsql
32-
-- Aggregation Function Syntax
33-
MAX( [ ALL | DISTINCT ] expression )
34-
35-
-- Analytic Function Syntax
36-
MAX ([ ALL ] expression) OVER ( <partition_by_clause> [ <order_by_clause> ] )
37-
```
38-
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+
3947
## Arguments
40-
**ALL**
41-
Applies the aggregate function to all values. ALL is the default.
42-
43-
DISTINCT
44-
Specifies that each unique value is considered. DISTINCT is not meaningful with MAX and is available for ISO compatibility only.
45-
46-
*expression*
47-
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.
48-
49-
For more information, see [Expressions &#40;Transact-SQL&#41;](../../t-sql/language-elements/expressions-transact-sql.md).
50-
51-
OVER **(** _partition\_by\_clause_ [ _order\_by\_clause_ ] **)**
52-
*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).
53-
54-
## Return Types
55-
Returns a value same as *expression*.
56-
57-
## Remarks
58-
MAX ignores any null values.
59-
60-
MAX returns NULL when there is no row to select.
61-
62-
For character columns, MAX finds the highest value in the collating sequence.
63-
64-
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).
65-
66-
Operates on a set of rows. To obtain the MAX value for a set of values inline, then look at [GREATEST](logical-functions-greatest-transact-sql.md)
67-
68-
## Examples
69-
70-
### A. Simple example
71-
The following example returns the highest (maximum) tax rate in the [!INCLUDE[ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database.
72-
73-
```sql
74-
SELECT MAX(TaxRate)
75-
FROM Sales.SalesTaxRate;
76-
GO
77-
```
78-
79-
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
80-
81-
```
82-
-------------------
83-
19.60
84-
Warning, null value eliminated from aggregate.
85-
86-
(1 row(s) affected)
87-
```
88-
89-
### B. Using the OVER clause
90-
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.
91-
92-
```sql
93-
SELECT DISTINCT Name
94-
, MIN(Rate) OVER (PARTITION BY edh.DepartmentID) AS MinSalary
95-
, MAX(Rate) OVER (PARTITION BY edh.DepartmentID) AS MaxSalary
96-
, AVG(Rate) OVER (PARTITION BY edh.DepartmentID) AS AvgSalary
97-
,COUNT(edh.BusinessEntityID) OVER (PARTITION BY edh.DepartmentID) AS EmployeesPerDept
98-
FROM HumanResources.EmployeePayHistory AS eph
99-
JOIN HumanResources.EmployeeDepartmentHistory AS edh
100-
ON eph.BusinessEntityID = edh.BusinessEntityID
101-
JOIN HumanResources.Department AS d
102-
ON d.DepartmentID = edh.DepartmentID
103-
WHERE edh.EndDate IS NULL
104-
ORDER BY Name;
105-
```
106-
107-
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
108-
109-
```
110-
Name MinSalary MaxSalary AvgSalary EmployeesPerDept
111-
----------------------------- --------------------- --------------------- --------------------- ----------------
112-
Document Control 10.25 17.7885 14.3884 5
113-
Engineering 32.6923 63.4615 40.1442 6
114-
Executive 39.06 125.50 68.3034 4
115-
Facilities and Maintenance 9.25 24.0385 13.0316 7
116-
Finance 13.4615 43.2692 23.935 10
117-
Human Resources 13.9423 27.1394 18.0248 6
118-
Information Services 27.4038 50.4808 34.1586 10
119-
Marketing 13.4615 37.50 18.4318 11
120-
Production 6.50 84.1346 13.5537 195
121-
Production Control 8.62 24.5192 16.7746 8
122-
Purchasing 9.86 30.00 18.0202 14
123-
Quality Assurance 10.5769 28.8462 15.4647 6
124-
Research and Development 40.8654 50.4808 43.6731 4
125-
Sales 23.0769 72.1154 29.9719 18
126-
Shipping and Receiving 9.00 19.2308 10.8718 6
127-
Tool Design 8.62 29.8462 23.5054 6
128-
129-
(16 row(s) affected)
130-
```
131-
132-
### C. Using MAX with character data
133-
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.
134-
```sql
135-
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.
136107
```
137-
The last system database is `tempdb`.
138-
139-
## See Also
140-
[Aggregate Functions &#40;Transact-SQL&#41;](../../t-sql/functions/aggregate-functions-transact-sql.md)
141-
[GREATEST](logical-functions-greatest-transact-sql.md)
142-
[OVER Clause &#40;Transact-SQL&#41;](../../t-sql/queries/select-over-clause-transact-sql.md)
143-
144-
145108

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)