SQL SELECT MIN and MAX

SQL SELECT MIN, MAX Functions

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

#

Problem: Find the largest order ever placed.

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

Syntax

MIN syntax.

SELECT MIN(column-name)
  FROM table-name

MAX syntax.

SELECT MAX(column-name)
  FROM table-name

More Examples

SQL 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

SQL 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

SQL 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

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