The MIN() and MAX() functions are aggregate functions used to find the minimum and maximum values in a set of values, respectively.
MIN() FunctionThe MIN() function returns the smallest value of the selected column.
MIN() SyntaxSELECT MIN(column_name) FROM table_name WHERE condition;
MAX() FunctionThe MAX() function returns the largest value of the selected column.
MAX() SyntaxSELECT MAX(column_name) FROM table_name WHERE condition;
Below is a selection from the Orders table:
Orders Table:
| OrderID | CustomerID | OrderDate |
|---|---|---|
| 10308 | 2 | 1996-09-18 |
| 10309 | 37 | 1996-09-19 |
| 10310 | 77 | 1996-09-20 |
MIN()The following SQL statement finds the smallest CustomerID in the Orders table.
SELECT MIN(CustomerID) AS SmallestCustomerID FROM Orders;
MAX()The following SQL statement finds the largest CustomerID in the Orders table.
SELECT MAX(CustomerID) AS LargestCustomerID FROM Orders;
Note: The
ASkeyword is used to create an alias for the column, making the result set more readable. We will cover aliases in a later chapter.