Home > SQL Server > Finding maximum or minimum value using MAX and MIN functions – SQL Server

Finding maximum or minimum value using MAX and MIN functions – SQL Server


Max() and Min() function are used to find the biggest and smallest number in a list of values. We can use these functions with numeric, character, GUID or datetime values.

Function syntax

MAX ( [ ALL | DISTINCT ] expression or column name) – this aggregation function returns a maximum value in a list.

MIN ( [ ALL | DISTINCT ] expression or column name) – this aggregation function returns a minimum value in a list.

  • ALL is the default keyword that uses all values in the list to find the max or min value.
  • Distinct can be used to find min or max value from the unique value list.
  • Both functions ignores NULL value from the list to find min or max value.

Examples

In this examples I am using Adventureworks2012 database.

1. Finding minimum and maximum integer order quantity value.

pic12. Finding minimum and maximum string in the currency code.

pic2We can also use over clause with these aggregation functions.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: