Saturday, April 15, 2017

MAX() in SQL Server


SQL MAX() function is used to return the maximum of all values in a column. The MAX() can only be applied on ANY type of column.  For character columns, MAX finds the highest value in the collating sequence. The MAX() determines the maximum of all the non-NULL values in the columns, NULL values are ignored.

Syntax:

     
            SELECT MAX(ALL | DISTINCT column_name) FROM table_name


column_name is the name of the column of which total occurrences is to be calculated.

table_name is the name of the table


Example 1:
MAX() on numeric column.

SELECT * FROM TempEmployeePayHistory



SELECT MAX(Rate) FROM TempEmployeePayHistory



Example 2:
MAX() on date column.

          SELECT * FROM DatabaseLog



SELECT MAX(PostTime) FROM DatabaseLog



Example 3:
MAX() on text column.

          SELECT MAX([Event]) FROM DatabaseLog



Example 4:
MAX() with GROUP BY clause.

SELECT Event, MAX(PostTime) FROM DatabaseLog GROUP BY [Event]



Share This
Previous Post
Next Post

TekGrabs, established in 2017, is in the business of sharing Information. It provides news, articles and photos about the different technologies, inventions in different domains. Provides information on programming and databases.

0 comments: