sql_func_min
SQL MIN() Function
Section titled “SQL MIN() Function”The MIN() Function
Section titled “The MIN() Function”The MIN() function is an aggregate function that returns the minimum (smallest) value of an expression in a set of rows. This expression is typically a column name.
MIN() can be used with numeric, string, or date/time columns. For strings, it typically returns the first value alphabetically. For dates, it returns the earliest date/time.
NULL values in the specified column are ignored by the MIN() function.
SQL MIN() Syntax
Section titled “SQL MIN() Syntax”SELECT MIN(expression)FROM table_nameWHERE condition;The expression is usually a column name. The WHERE clause is optional; if omitted, MIN() considers all rows in the table.
Demo Database
Section titled “Demo Database”We will use the Northwind sample database for our examples, focusing on the “Products” table.
Below is a selection from the “Products” table:
ProductIDProductNameSupplierIDCategoryIDUnitPriceReleaseDate
1Chais1110 boxes x 20 bags18.002023-01-15
2Chang1124 - 12 oz bottles19.002023-02-20
3Aniseed Syrup1212 - 550 ml bottles10.002022-12-05
4Chef Anton's Cajun Seasoning2248 - 6 oz jars22.002023-03-01
5Chef Anton's Gumbo Mix2236 boxes21.352023-01-20SQL MIN() Examples
Section titled “SQL MIN() Examples”- Find the lowest price of any product:
The following SQL statement finds the minimum value in the “Price” column from the “Products” table.
SELECT MIN(Price) AS SmallestPriceFROM Products;This query will return 10.00 based on the sample data (Aniseed Syrup).
- Find the earliest release date of any product:
SELECT MIN(ReleaseDate) AS EarliestReleaseDateFROM Products;This query will return 2022-12-05 based on the sample data.
- Find the product name that comes first alphabetically (among products in CategoryID 1):
SELECT MIN(ProductName) AS FirstProductNameInCategory1FROM ProductsWHERE CategoryID = 1;Based on the sample (Chais, Chang), this would return Chais.
Practical Application:
MIN() is useful for finding the lowest value in a dataset, such as the cheapest product, the earliest transaction date, or the lowest score. Like other aggregate functions, it is often used with the GROUP BY clause to find minimum values within different categories (e.g., the minimum price for each product category).