EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials MySQL Tutorial MIN() in MySQL
 

MIN() in MySQL

Aanchal Sharma
Article byAanchal Sharma
EDUCBA
Reviewed byRavi Rathore

Updated May 10, 2023

MIN() in MySQL

 

 

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 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 no condition matches. Suppose we can use this function to find out the least population of a city from a list of cities.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax with Explanation:

MIN ( [DISTINCT] Expression)

The term ‘Expression’ requires a numeric value that can either be a field in a table or a formula. This keyword allows for removing the duplicate values from the list of records in the database table. But almost it will be the same result without distinct keywords, too; it has not much effect on MIN () like on other SUM (), COUNT (), or AVG () aggregate functions.

Also,

MIN([DISTINCT] expression) [over_clause]

The over_clause is not compulsory but 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 know that MIN() function is used to find the minimum value from the total rows selected by the select statement, we can now easily get the benefits in our business or 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.

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]

A database table as a source can return the minimum value present in the specified column by using the MIN() function.

Examples to Implement MIN() in MySQL

Given below are the examples of MIN() in MySQL:

Example #1 – Get minimum value in a column

Let us take the below-shown screenshot of records from a table of Products.

MIN() in MySQL 1

After inspection of all product prices, we applied the MIN() to get the product whose price is lower than others.

SELECT MIN(Price) AS SmallestPrice FROM Products;

Output:

MIN() in MySQL 2

Example #2 – With WHERE clause

Here we use MIN() to return the smallest value from a particular condition using the WHERE keyword with the SELECT statement.

SELECT MIN(Price) AS SmallestPrice FROM Products WHERE SupplierID=2;

Output:

min function with where clause

We have the smallest value for the only price whose SupplierID is two from the Products table. We can also use any multi-word alias (like SmallestPrice) to define the lowest value, as in the above query.

Example #3 – Sub-query statement

To provide not only the minimum price from the 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:

min function in a subquery statement

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 – With GROUP BY Clause

This function with the GROUP BY clause helps find the minimum value for every group based on the criteria.

SELECT SupplierID , MIN(Price) FROM Products GROUP BY SupplierID;

Output:

min function with group by clause

SupplierID groups the product fields from the Products table, but the minimum prices are arranged randomly, not ascending or descending.

Example #5 – With ORDER BY clause

This function with the 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:

min function with order by clause

Now, the fields are grouped by SupplierID, and the result set is ordered based on MIN(Price) in ascending order, where the first small minimum value is set first and the next greater minimum at last.

Note: We can also use ASC or DESC to set an order BY clause like “ORDER BY MIN(Price) ASC”.

Example #6 – 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:

having clause

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 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.

The example uses the Suppliers table data for supplier ID 1, 2, and 3.

min in mysql 6

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

Example #7 – Find the Minimum Character Length

With MIN(), we can find the minimum value from numeric data in the column field and implement it in other areas. We can combine MySQL MIN() function with 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:

find minimum character length

Conclusion

So, MIN() function helps deal with many cases, such as getting the lowest number, the cheapest item, the least expensive product, the lowest credit limit, and the smallest payment from consumers. Hence, in this article, you all might have discovered some important usages of the MIN() function in MySQL with syntaxes and examples that will help you to treat the functional queries related to databases and tables with the areas of business or any industry with massive data stored.

Recommended Articles

We hope that this EDUCBA information on “MIN() in MySQL” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1.  LIKE in MySQL
  2. ORDER BY in MySQL
  3. Table in MySQL
  4. MySQL Aggregate Function

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW