SQL SELECT MIN, MAX Statement

The SQL SELECT MIN and MAX syntax

The general MIN syntax is:

  1. SELECT MIN(columnname)
  2. FROM tablename

The general MAX syntax is:

  1. SELECT MAX(columnname)
  2. FROM tablename

 


Problem: Find the cheapest product

  1. SELECT MIN(UnitPrice)
  2. FROM Product

Results:

UnitPrice
2.50

 


 

SQL SELECT MAX and MIN Examples

Problem: Find the largest order placed in 2014

  1. SELECT MAX(TotalAmount)
  2. FROM [Order]
  3. WHERE YEAR(OrderDate) = 2014

Results:

TotalAmount
17250.00

 


Problem: Find the last order date in 2013

  1. SELECT MAX(OrderDate)
  2. FROM [Order]
  3. WHERE YEAR(OrderDate) = 2013

MIN and MAX can also be used with numeric and date types.
Results:

OrderDate
2013-12-31 00:00:00.000

댓글 남기기