MySQL MIN

MIN function is used to find a minimum value from a list of values. MySQL MIN Function is used for this task on data columns of a MySQL data Table. 

This function accepts single column or expression as argument and returns single value as output. Any MySQL Datatype column or expression can be passed to MySQL MIN function.

If you need to find column wise minimum values give the individual columns in separate MIN functions in the SELECT statement.

Syntax of MySQL MIN Function

The MIN function is used in a SELECT query according to the given syntax.

MIN([DISTINCT]  expression|table_column)

  • MIN is the keyword used for in the MySQL SELECT statement.
  • DISTINCT is the keyword used in the query to compare only distinct values from column or expression. DISTINCT is optional keyword
  • table_column is the column name of the table from where the minimum value is to be found.
  • Expression is the expression using table column names and/or MySQL Arithmetic Operators.

Datatype of the argument to the MIN function is the datatype of the output value.

Examples of MySQL MIN

Finding minimum value in one column

The following query displays minimum billAmt.

SELECT MIN(billAmt) FROM sales;
MySQL Min

Finding minimum values for multiple columns

This query displays minimum of columns billAmt and purAmt using separate MIN functions.

SELECT MIN(billAmt), MIN(purAmt) FROM sales;
Multiple columns

Finding minimum value in an expression

This query displays minimum values from columns billAmt  and purAmt.  Minimum value of expression to calculate profit percentage  is found with separate MIN function.

SELECT MIN(billAmt), MIN(purAmt),MIN(((billAmt-purAmt)/purAmt)*100) AS "lowest Profit Percentage" FROM sales;
MySQL min using expression

Using distinct keyword

To get minimum values from distinct column values, DISTINCT keyword is used. Two separate MIN functions are used for two columns.

SELECT MIN(DISTINCT billAmt), MIN(DISTINCT purAmt) FROM sales;
With Where Clause

Using where clause

To get minimum column values that meets a specific condition, WHERE clause is used in the following query.

SELECT MIN(billAmt), MIN(purAmt) FROM sales WHERE sid='S1001';SELECT MIN(billAmt), MIN(purAmt) FROM sales WHERE sid='S1002';

Finding minimum value from data types other than numeric data type

This query is used to find alphabetically highest artist name and the minimum from DOB.

SELECT MIN(artist_name), MIN(dob) from artists;
String and Date

Points to remember

  • You can use one MIN functions for a column name or expression. Separate MIN functions are needed for different columns or expressions.
  • MIN function can be used only in a SELECT statement.
  • DISTINCT keyword is used within parenthesis before column name or expression in the MIN function to find minimum value among distinct values. This doesn’t make any difference to the output.
  • WHERE clause is used to filter rows to find minimum values among specific values matching a specific condition.
  • MIN function returns output depending on the datatype of the column or expression.
  • MIN function considers NULL values in columns as zeroes. It returns MIN of all the non-null values.