SQL SELECT MIN, MAX

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

Example

#

Find the largest order amount.

SELECT MAX(TotalAmount) AS 'Largest Amount'
  FROM [Order]
Result
Largest Amount
17250.00

Syntax

MIN syntax.

SELECT MIN(column-name)
  FROM table-name

MAX syntax.

SELECT MAX(column-name)
  FROM table-name

More Examples

SELECT MIN

PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued
Problem: Find the cheapest product.
SELECT MIN(UnitPrice) AS 'Cheapest Price'
  FROM Product
Result
Cheapest Price
2.50

SELECT MAX

ORDER
Id
OrderDate
OrderNumber
CustomerId
TotalAmount
Problem: Find the largest order placed in 2014.
SELECT MAX(TotalAmount) AS 'Largest Order'
  FROM [Order]
 WHERE YEAR(OrderDate) = 2014
Result
Largest Order
17250.00

SELECT MAX Date

ORDER
Id
OrderDate
OrderNumber
CustomerId
TotalAmount
Problem: Find the last order date in 2013.
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.

Result
Last Order Date
2013-12-31 00:00:00.000

You may also like



Guides


vsn 3.1