Dofactory.com
Dofactory.com
Earn income with your data and sql skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

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



Last updated on Dec 21, 2023

Earn income with your data and sql skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.