SELECT MIN returns the smallest value for a column.
SELECT MAX returns the largest value for a column.
MIN and MAX are built-in functions
Find the largest order amount.
SELECT MAX(TotalAmount) AS 'Largest Amount'
FROM [Order]
Largest Amount |
---|
17250.00 |
MIN syntax.
SELECT MIN(column-name) FROM table-name
MAX syntax.
SELECT MAX(column-name) FROM table-name
PRODUCT |
---|
Id |
ProductName |
SupplierId |
UnitPrice |
Package |
IsDiscontinued |
SELECT MIN(UnitPrice) AS 'Cheapest Price'
FROM Product
Cheapest Price |
---|
2.50 |
ORDER |
---|
Id |
OrderDate |
OrderNumber |
CustomerId |
TotalAmount |
SELECT MAX(TotalAmount) AS 'Largest Order'
FROM [Order]
WHERE YEAR(OrderDate) = 2014
Largest Order |
---|
17250.00 |
ORDER |
---|
Id |
OrderDate |
OrderNumber |
CustomerId |
TotalAmount |
SELECT MAX(OrderDate) AS 'Last Order Date'
FROM [Order]
WHERE YEAR(OrderDate) = 2013
Note: MIN and MAX can be used with numeric, string, and date values.
Last Order Date |
---|
2013-12-31 00:00:00.000 |