
141 views
MySQL min()
The MIN()
function in MySQL is an aggregate function used to retrieve the minimum (smallest) value from a specified column. It is commonly used to find the minimum value in a dataset or to retrieve the earliest date in a date column. The basic syntax of the MIN()
function is as follows:
MIN(expression)
expression
: This should be a column or an expression from which you want to find the minimum value.
Here are some common ways to use the MIN()
function:
- Find the Minimum Value in a Column:
To find the minimum value in a column, useMIN(column_name)
.
SELECT MIN(price) FROM products;
- Find the Minimum Date in a Date Column:
To find the earliest date in a date column, useMIN(date_column)
.
SELECT MIN(order_date) FROM orders;
- Find the Minimum Value Based on a Condition:
You can find the minimum value in a column that meets specific conditions by using theMIN()
function with aWHERE
clause.
SELECT MIN(score) FROM test_scores WHERE subject = 'Math';
- Find the Minimum Value Grouped by a Column:
When you want to find the minimum value for each group within a dataset, you can use theGROUP BY
clause with theMIN()
function.
SELECT department, MIN(salary) FROM employees GROUP BY department;
The MIN()
function returns the smallest value from the specified column. It’s a helpful tool for identifying the minimum value in your data, whether that’s the lowest price, the earliest date, or the minimum score in a specific category.