MIN

Find the minimum value. This function returns the lowest value in a set of values, applicable to numbers, strings, and dates.

Syntax

MIN([DISTINCT] expr)

Description

Returns the minimum value of expr. MIN() may take a string argument, in which case it returns the minimum string value. The DISTINCT keyword can be used to find the minimum of the distinct values of expr, however, this produces the same result as omitting DISTINCT.

Note that SET and ENUM fields are currently compared by their string value rather than their relative position in the set, so MIN() may produce a different lowest result than ORDER BY ASC.

It is an aggregate function, and so can be used with the GROUP BY clause.

MIN() can be used as a window function.

MIN() returns NULL if there were no matching rows.

Not only ascending, but also descending indexes can be used to optimize MIN.

Examples

CREATE TABLE student (name CHAR(10), test CHAR(10), score TINYINT); 

INSERT INTO student VALUES 
  ('Chun', 'SQL', 75), ('Chun', 'Tuning', 73), 
  ('Esben', 'SQL', 43), ('Esben', 'Tuning', 31), 
  ('Kaolin', 'SQL', 56), ('Kaolin', 'Tuning', 88), 
  ('Tatiana', 'SQL', 87), ('Tatiana', 'Tuning', 83);

SELECT name, MIN(score) FROM student GROUP BY name;
+---------+------------+
| name    | MIN(score) |
+---------+------------+
| Chun    |         73 |
| Esben   |         31 |
| Kaolin  |         56 |
| Tatiana |         83 |
+---------+------------+

MIN() with a string:

Be careful to avoid this common mistake, not grouping correctly and returning mismatched data:

Difference between ORDER BY ASC and MIN():

As a window function:

See Also

  • AVG (average)

  • MAX (maximum)

  • SUM (sum total)

  • LEAST() returns the smallest value from a list.

This page is licensed: GPLv2, originally from fill_help_tables.sql

Last updated

Was this helpful?