MAX

Transact-SQL Reference

Transact-SQL Reference

MAX

Returns the maximum value in the expression.

Syntax

MAX ( [ ALL | DISTINCT ] expression )

Arguments

ALL

Applies the aggregate function to all values. ALL is the default.

DISTINCT

Specifies that each unique value is considered. DISTINCT is not meaningful with MAX and is available for SQL-92 compatibility only.

expression

Is a constant, column name, or function, and any combination of arithmetic, bitwise, and string operators. MAX can be used with numeric, character, and datetime columns, but not with bit columns. Aggregate functions and subqueries are not permitted.

Return Types

Returns a value same as expression.

Important  Distinct aggregates, for example AVG(DISTINCT column_name), COUNT(DISTINCT column_name), MAX(DISTINCT column_name), MIN(DISTINCT column_name), and SUM(DISTINCT column_name), are not supported when using CUBE or ROLLUP. If used, Microsoft® SQL Server™ returns an error message and cancels the query.

Remarks

MAX ignores any null values.

For character columns, MAX finds the highest value in the collating sequence.

Examples

This example returns the book with the highest (maximum) year-to-date sales.

USE pubs
GO
SELECT MAX(ytd_sales)
FROM titles
GO

Here is the result set:

----------- 
22246         

(1 row(s) affected)

Warning, null value eliminated from aggregate.

See Also

Aggregate Functions