This tutorial explains how to use the MySQL MIN function with syntax and examples.
The MySQL MIN function returns the minimum value of an expression.
The syntax for the MIN function in MySQL is:
OR the syntax for the MIN function when grouping the results by one or more columns is:
Parameters or Arguments
expression1, expression2, ... expression_n
Expressions that are not encapsulated within the MIN function and must be included in the GROUP BY clause at the end of the SQL statement.
This is the column or expression from which the minimum value will be returned.
The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.
Optional. These are conditions that must be met for the records to be selected.
The MIN function can be used in the following versions of MySQL:
MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0, MySQL 3.23
Example - With Single Expression
Let's look at some MySQL MIN function examples and explore how to use the MIN function in MySQL.
For example, you might wish to know how the minimum salary of all employees.
In this MIN function example, we've aliased the MIN(salary) expression as "Lowest Salary". As a result, "Lowest Salary" will display as the field name when the result set is returned.
Example - Using GROUP BY
In some cases, you will be required to use the GROUP BY clause with the MIN function.
For example, you could also use the MIN function to return the name of the department and the minimum salary in the department.
Because you have listed one column in your SELECT statement that is not encapsulated in the MIN function, you must use a GROUP BY clause. The department field must, therefore, be listed in the GROUP BY section.