SQL MIN Function

MIN returns the minimum or lowest value from a set of values.

This function ignores NULL values.

Example

#

This example returns the minimum, or lowest, product price.

SELECT MIN(UnitPrice) AS 'Min'
  FROM Product
Result:  1 record
Min
2.50

Syntax

Syntax of the MIN function.

MIN(value)

value -- a value, column, or subquery.

More Examples

MIN with GROUP BY

ORDER
Id
OrderDate
OrderNumber
CustomerId
TotalAmount
Problem: List the smallest orders by month for the year 2013.
SELECT MONTH(OrderDate) AS Month
       MIN(TotalAmount) AS 'Total Sales'
  FROM [Order]
 WHERE YEAR(OrderDate) = 2013
 GROUP BY MONTH(OrderDate)
Result:  12 records
Month Min Order Amount
1 49.80
2 174.90
3 147.00
4 136.80
5 110.00

MIN with GROUP BY

PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued
SUPPLIER
Id
CompanyName
ContactName
City
Country
Phone
Fax
Problem: List the cheapest product for each supplier.
SELECT S.CompanyName, 
       MIN(UnitPrice) AS 'Lowest Price'
  FROM Product P
  JOIN Supplier S ON S.Id = P.SupplierId
 GROUP BY S.CompanyName
Result:  29 records
CompanyName Lowest Price
Aux joyeux ecclésiastiques 18.00
Bigfoot Breweries 14.00
Cooperativa de Quesos 'Las Cabras' 21.00
Escargots Nouveaux 13.25
Exotic Liquids 10.00

You may also like



Guides


vsn 3.1