The MySQL MIN function is an aggregate function that is used to retrieve the minimum value from a set of values. It is commonly used with numerical or date columns in a SELECT statement to find the smallest value in a specified column. Here is a basic syntax of the MIN function:
SELECT MIN(column_name) FROM table_name WHERE condition;
column_name: The name of the column from which you want to retrieve the minimum value.
table_name: The name of the table that contains the specified column.
condition: An optional condition that filters the rows before calculating the minimum value.
Example
Let’s look at an example to illustrate how the MIN function works. Consider a table named “products” with columns “product_id” and “price”:
CREATE TABLE products ( product_id INT PRIMARY KEY, price DECIMAL(8, 2) ); INSERT INTO products (product_id, price) VALUES (1, 19.99), (2, 15.49), (3, 25.00), (4, 12.99);
Now, if you want to find the minimum price from the “products” table, you can use the MIN() function as follows:
SELECT MIN(price) AS min_price FROM products;
The result of this query will be:
+-----------+ | min_price | +-----------+ | 12.99 | +-----------+
In this example, the MIN() function returns the smallest value from the “price” column, which is 12.99.
You can also use the MIN() function with a WHERE clause to find the minimum value based on specific conditions. For instance:
SELECT MIN(price) AS min_price FROM products WHERE price > 15.00;
This query will return the minimum price for products with a price greater than 15.00.
In summary, the MySQL MIN function is a useful tool for finding the smallest value in a specified column or a subset of data based on specified conditions.