The MIN() function retrieves the smallest value from the specified column.
The MAX() function fetches the largest value from the designated column.
Retrieve the minimum price from the Price column.
|
SELECT MIN(Price) |
Retrieve the maximum price from the Price column.
| SELECT MAX(Price) FROM Products; |
SELECT MIN(column_name) |
SELECT MAX(column_name) |
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 |
When employing MIN() or MAX(), the resultant column lacks a descriptive name. To assign a descriptive name to the column, utilize the AS keyword:
| SELECT MIN(Price) AS SmallestPrice FROM Products; |
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:
| SELECT MIN(Price) AS SmallestPrice, CategoryID FROM Products GROUP BY CategoryID; |