Introduction to MySQL MIN() Function
MySQL MIN() function is an SQL query that returns the minimum value from a set of values recorded in a database table. Usually, MIN() is an aggregate function that can be used to calculate over a range of values returned by the SELECT query statement to find out the minimum record from those separated rows in the table. MIN() function receives a conditional expression that can be a column name or valid term involving columns. The function returns a NULL value if there is no condition that matches. Suppose we can use this function for finding out the least population of a city from a list of cities.
Syntax with Explanation:
MIN ( [DISTINCT] Expression)
Where the term ‘Expression’ denotes a numeric value that can either be a field in a table or a formula, which is required. This keyword allows removing the duplicate values from the list of records in the database table. But almost it will be the same result without distinct keyword too, it has no much effect on MIN () like on other SUM (), COUNT () or AVG () aggregate functions.
Also,
MIN([DISTINCT] expression) [over_clause]
The over_clause is not a compulsory part but it can be associated with window tasks. So, over_clause is used only if we don’t add the DISTINCT keyword in the MIN() function.
How Does MIN() Function Work in MySQL?
As we have known that MIN() function is used to find out the minimum value from total rows selected by the select statement, then we can now easily get the benefits in our business or in product management. For example, it will allow getting the low selling product of your company, a less expensive item or a minimum quantity of the product in your store consumed by the customers.
Just we can apply this SQL query in our Databases which returns the result. MySQL MIN() SQL statement is shown below:
SELECT MIN ([Column_Name]) FROM [Source]
Consequently, using MIN() function the minimum value present in the specified column can be returned form a database table as a source.
Examples to Implement MIN() in MySQL
Given below are the examples of MIN() in MySQL:
Example #1 – MIN() to get minimum value in a column
Let us take the below-shown screenshot of records from a table Products.
We have applied the MIN() to get the product whose price is lower than others after inspection of all product prices.
SELECT MIN(Price) AS SmallestPrice FROM Products;
Output:
Example #2 – MIN() function with WHERE clause.
Here we use MIN() to return the smallest value from a particular condition using WHERE keyword with SELECT statement.
SELECT MIN(Price) AS SmallestPrice FROM Products WHERE SupplierID=2;
Output:
We have got the smallest value for the only price whose SupplierID is 2 from the Products table. We can use any multi-word alias (like SmallestPrice) too to define the lowest value as in the above query.
Example #3 – MIn() function in a sub-query statement.
To provide not only the minimum price from products table but also to show the row information including other fields, we have used MIN() in a subquery form as follows:
SELECT * From Products WHERE Price = (SELECT MIN(Price) AS SmallestPrice FROM Products);
Output:
The inner query returns the lowest price and the outer query provides the row information of the lowest price field in the products table.
Example #4 – MIN() function with GROUP BY Clause.
This function with the GROUP BY clause helps to find out the minimum value for every group based on the criteria.
SELECT SupplierID , MIN(Price) FROM Products GROUP BY SupplierID;
Output:
The product fields are grouped by SupplierID from Products table but the minimum prices are arranged randomly not either ascending or descending.
Example #5 – MIN() function with ORDER BY clause.
This function with ORDER BY clause helps to specify the column with which we can order by the minimum value for every group based on the criteria.
SELECT SupplierID , MIN(Price) FROM Products
GROUP BY SupplierID
ORDER BY MIN(price);
Output:
Now, the fields are grouped by SupplierID and the result set is ordered on the basis of MIN(Price) in ascending order where the first small minimum value is set first and next greater minimum at last.
Example #6 – MIN() function with HAVING clause
We use the HAVING clause with MIN() function to filter these groups based on a certain condition.
SELECT SupplierID , MIN(Price) FROM Products
GROUP BY SupplierID
HAVING MIN(Price) <50
ORDER BY MIN(price);
Output:
In the above query, firstly we have used MIN() to get the lowest price from a group of Products with GROUP BY clause association and again based on the HAVING clause, we have applied the MIN function to get the lowest prices which are less than 50 in the table.
Next, suppose if we want names of suppliers instead of supplier ids, then we can even use the JOIN clause with the above query. We need to apply INNER JOIN for both the tables Products and Suppliers.
Here is the Suppliers table data for supplier ids 1,2,3 used in the example:
Now the SQL statement for this is as follows:
SELECT SupplierName, MIN(Price) FROM Products
INNER JOIN Suppliers
USING (SupplierID)
GROUP BY SupplierName
HAVING MIN(Price) <50
ORDER BY MIN(price);
Output:
Example #7 – MIN() function to find the Minimum Character Length.
With MIN() we cannot just find out the minimum value from numeric data in the column field but also can implement it to other areas. We can combine MySQL MIN() function with some other functions like CHAR_LENGTH.
For example, taking the same Products table, we can write the following query and find the minimum number of characters in the ProductName column:
SELECT MIN(CHAR_LENGTH(ProductName)) AS 'Min Char Length'
FROM Products;
Output:
Conclusion
So, MIN() function helps in dealing with many cases such as to get the lowest number, the cheapest item, the least expensive product, get the lowest credit limit and the smallest payment from consumers. Hence, in this article, you all might have discovered some important usages of MIN() function in MySQL with syntaxes and examples that will help you to treat with the functional queries related to database and tables with the areas of business or any industry with massive data stored.
Recommended Articles
This is a guide to MIN() in MySQL. Here we discuss the basic concept, how does MIN() function work in MySQL? with examples respectively. You may also have a look at the following articles to learn more –
12 Online Courses | 10 Hands-on Projects | 92+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses