Curriculum
Course: SQL
Login

Curriculum

SQL

SQL References

0/80

MySQL Functions

0/139

SQL Server Functions

0/84

SQL Quick Ref

0/1
Text lesson

SQL MIN and MAX

The SQL MIN() and MAX() Functions

The MIN() function retrieves the smallest value from the specified column.

The MAX() function fetches the largest value from the designated column.

MIN Example

Retrieve the minimum price from the Price column.

SELECT MIN(Price)
FROM Products;

MAX Example

Retrieve the maximum price from the Price column.

SELECT MAX(Price)
FROM Products; 

Syntax

SELECT MIN(column_name)
FROM table_name
WHERE condition;

 

SELECT MAX(column_name)
FROM table_name
WHERE condition

Demo Database

The following excerpt is from the Products table utilized in the illustrations:

ProductID

ProductName

SupplierID

CategoryID

Unit

Price

1

Chais

1

1

10 boxes x 20 bags

18

2

Chang

1

1

24 – 12 oz bottles

19

3

Aniseed Syrup

1

2

12 – 550 ml bottles

10

4

Chef Anton’s Cajun Seasoning

2

2

48 – 6 oz jars

22

5

Chef Anton’s Gumbo Mix

2

2

36 boxes

21.35

Set Column Name (Alias)

When employing MIN() or MAX(), the resultant column lacks a descriptive name. To assign a descriptive name to the column, utilize the AS keyword:

Example

SELECT MIN(Price) AS SmallestPrice
FROM Products;

Use MIN() with GROUP BY

In this scenario, we utilize the MIN() function along with the GROUP BY clause to retrieve the smallest price for each category listed in the Products table:

Example

SELECT MIN(Price) AS SmallestPrice, CategoryID
FROM Products
GROUP BY CategoryID;