How the MIN() function works in Mariadb?
The MIN()
function is a useful tool for finding the minimum value of a set of values.
The MIN()
function is a useful tool for finding the minimum value of a set of values. It can be used for various purposes, such as finding the lowest price, the earliest date, or the smallest number.
Syntax
The syntax of the MIN()
function is as follows:
MIN(expr)
The function takes one argument:
expr
: An expression that represents the set of values to find the minimum from. It can be any valid expression that returns a value, such as a column name, a literal, or a function.
The function returns a value that represents the minimum value of the input expression. The data type of the return value depends on the data type of the input expression. If the input expression is numeric, the return value is numeric. If the input expression is string, the return value is string. If the input expression is date or time, the return value is date or time. If the input expression is NULL or empty, the return value is NULL.
Examples
In this section, we will show some examples of how to use the MIN()
function in different scenarios.
Example 1: Finding the minimum of a column value
Suppose you have a table called products
that stores the information of various products, such as their product_id, name, and price. The price column is a numeric value that represents the price of the product. You want to find the minimum price of all the products, so that you can find the cheapest product, the best deal, or the lowest cost. You can use the MIN()
function to do so. For example, you can execute the following statement:
SELECT MIN(price) AS min_price FROM products;
This will return the minimum price of all the products, or NULL if the table is empty. For example, the result might look like this:
+-----------+
| min_price |
+-----------+
| 9.99 |
+-----------+
Note that the data type of the return value is the same as the data type of the input column. For example, if the price column is a decimal value, the return value is a decimal value.
Example 3: Finding the minimum of a function result
Suppose you want to find the minimum of the result of a function, such as NOW()
or RAND()
. You can use the MIN()
function to do so. For example, you can execute the following statement:
SELECT MIN(NOW());
This will return the minimum value of the current date and time, which is the same as the current date and time. For example, the result might look like this:
+---------------------+
| MIN(NOW()) |
+---------------------+
| 2024-01-01 10:00:00 |
+---------------------+
Related Functions
There are some other functions that are related to the MIN()
function and can be used to perform other calculations on a set of values. Here are some of them:
MAX()
: This function returns the maximum value of a set of values.AVG()
: This function returns the average value of a set of values.SUM()
: This function returns the sum of a set of values.COUNT()
: This function returns the number of values in a set of values.GROUP_CONCAT()
: This function returns a string that concatenates the values in a set of values, separated by a delimiter.
Conclusion
The MIN()
function is a powerful and flexible function that can help you find the minimum value of a set of values. It can be used for various purposes, such as finding the lowest price, the earliest date, or the smallest number. You can also use some other related functions to find other values, such as maximum, average, sum, count, or group_concat. By using these functions, you can achieve a better analysis and understanding of your data.