SQL SELECT MIN, MAX Statement

How do I get the max and min values for a table column?

  • SELECT MIN returns the minimum value for a column.
  • SELECT MAX returns the maximum value for a column.

The SQL SELECT MIN and MAX syntax

The general MIN syntax is:
SELECT MIN(column-name)
  FROM table-name
The general MAX syntax is:
SELECT MAX(column-name)
  FROM table-name

PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued

SQL MAX and MIN Examples

Problem: Find the cheapest product
SELECT MIN(UnitPrice)
  FROM Product
Result:
UnitPrice
2.50

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

ORDER
Id
OrderDate
OrderNumber
CustomerId
TotalAmount
Problem: Find the last order date in 2013
SELECT MAX(OrderDate)
  FROM [Order]
 WHERE YEAR(OrderDate) = 2013
Note: MIN and MAX can be used with numeric, string, and date values.
Result:
OrderDate
2013-12-31 00:00:00.000


Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Licensing       EULA       Sitemap      
© Data & Object Factory, LLC.
Made with    in Austin, Texas