SQL MAX() Function

SQL MAX() function returns the largest value from the selected column.

Syntax

SELECT MAX(columnName) FROM TableName

To understand MAX() function we will take example from [SalesPerson] table of AdventureWorks2012 database. Below is a selection for SalesPerson table.

MAX Function

We will select maximum bonus from SalesPerson table.

SELECT MAX(Bonus) AS MAXBonus
 FROM [Sales].[SalesPerson]

This query will give you output 6700.00 as maximum bonus.

MAXBonusWe will take one more example to understand it better. Here we will try to find maximum value for SalesYTD column from SalesPerson table.

SELECT MAX(SalesYTD) AS Sales
FROM [Sales].[SalesPerson]

MAX Sales

Note:

– Max() function ignores null value

– For character columns, Max() function finds the highest value from collating order i.e. from alphabets A-Z, it will return Z as maximum value.

Reference: Manzoor Siddiqui [www.SQLServerLog.com]

You may also like

You may also like...

Leave a Reply