EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials MySQL Tutorial MIN() in MySQL

MIN() in MySQL

Aanchal Sharma
Article byAanchal Sharma
Priya Pedamkar
Reviewed byPriya Pedamkar

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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
C++ PROGRAMMING Course Bundle - 9 Courses in 1 | 5 Mock Tests
37+ Hour of HD Videos
9 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ASP.NET Course Bundle - 28 Courses in 1 | 5 Mock Tests
123+ Hours of HD Videos
28 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SQL Course Bundle - 51 Courses in 1 | 6 Mock Tests
204+ Hours of HD Videos
51 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SOFTWARE TESTING Course Bundle - 13 Courses in 1
53+ Hour of HD Videos
13 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
MYSQL Course Bundle - 18 Courses in 1 | 3 Mock Tests
 93+ Hour of HD Videos
18 Courses
3 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

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

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

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more