MariaDB MIN() Function

In MariaDB, MIN() is a built-in function that returns the minimum of all values ​​represented by a given expression.

MariaDB MIN() Syntax

Here is the syntax of the MariaDB MIN() function:

MIN(expr)

We typically use the MIN() function like this:

SELECT MIN(expr), ...
FROM table_name
[WHERE ...];

Or use the MIN() function with the GROUP BY clause:

SELECT MIN(expr), group_expr1, group_expr2, ...
FROM table_name
[WHERE ...]
GROUP BY group_expr1, group_expr2, ...;

Parameters

expr
The expr expression used for aggregation operations. It can be a column name or an expression.
group_expr1, group_expr2, ...
The expression or column name used for grouping.

Return value

The MariaDB MIN(expr) function returns the minimum value among all the values ​​represented by the expression.

If there is no matching row, the MIN() function returns NULL.

MariaDB MIN() Examples

We’ll demonstrate with the student_score table. Let’s first create a demo table and insert test rows.

CREATE TABLE `student_score` (
    `id` INT PRIMARY KEY AUTO_INCREMENT,
    `name` VARCHAR(255) NOT NULL,
    `subject` VARCHAR(255) NOT NULL,
    `score` INT NOT NULL
);
INSERT INTO `student_score` (`name`, `subject`, `score`)
VALUES ('Tom', 'Math', 80),
    ('Tom', 'English', 90),
    ('Jim', 'Math', 84),
    ('Jim', 'English', 96),
    ('Tim', 'Math', 80),
    ('Tim', 'English', 98);

Here is all the rows in the table:

+----+------+---------+-------+
| id | name | subject | score |
+----+------+---------+-------+
|  1 | Tom  | Math    |    80 |
|  2 | Tom  | English |    90 |
|  3 | Jim  | Math    |    84 |
|  4 | Jim  | English |    96 |
|  5 | Tim  | Math    |    80 |
|  6 | Tim  | English |    98 |
+----+------+---------+-------+

Basic example

The following SQL statement returns the least grade among all grades.

SELECT MIN(score) from student_score;
+------------+
| MIN(score) |
+------------+
|         98 |
+------------+

GROUP BY

Let’s use MIN() and GROUP BY to see the least grade for more dimensions.

  1. Query the least grade for each subject

    SELECT subject, MIN(score)
    FROM student_score
    GROUP BY subject;
    

    Output:

    +---------+------------+
    | subject | MIN(score) |
    +---------+------------+
    | Math    |         80 |
    | English |         90 |
    +---------+------------+

    Here, MySQL will first group the result set by subject according to GROUP BY subject, and then execute within each group MIN(score).

  2. Query the least grade for each student

    SELECT name, MIN(score)
    FROM student_score
    GROUP BY name;
    

    Output:

    +------+------------+
    | name | MIN(score) |
    +------+------------+
    | Tom  |         80 |
    | Jim  |         84 |
    | Tim  |         80 |
    +------+------------+

    Here, MySQL will first group the result set by name according to GROUP BY name, and then execute MIN(score) within each group.

Conclusion

In MariaDB, MIN() is a built-in function that returns the minimum of all values ​​represented by a given expression.