EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MySQL Tutorial MIN() in MySQL
Secondary Sidebar
MySQL Tutorial
  • Functions
    • MySQL Function
    • MySQL Aggregate Function
    • MySQL String functions
    • MySQL Date Functions
    • MySQL Window Functions
    • MySQL Math Functions
    • MySQL Boolean
    • Cursor in MySQL
    • Condition in MySQL
    • MySQL BETWEEN
    • Insert in MySQL
    • MySQL IFNULL()
    • MySQL TIMESTAMPDIFF()
    • MySQL COALESCE()
    • MySQL count()
    • MIN() in MySQL
    • MySQL Numeric
    • MySQL field()
    • MySQL FIND_IN_SET()
    • MySQL avg()
    • MySQL MAX() Function
    • MySQL BIN()
    • MySQL Concat
    • MySQL DECODE()
    • MySQL REGEXP_REPLACE()
    • MySQL Asynchronous
    • MySQL innodb_buffer_pool_size
    • MySQL key_buffer_size
    • MySQL TRUNCATE()
    • MySQL ROW_NUMBER()
    • NOT in MySQL
    • MySQL IN Operator
    • LIKE in MySQL
    • ANY in MySQL
    • MySQL NOT IN
    • MySQL CHECK Constraint
    • MySQL DISTINCT
    • MySQL ALL
    • MySQL Union
    • MySQL UNION ALL
    • MySQL EXISTS
    • MySQL ON DELETE CASCADE
    • MySQL REGEXP
    • MySQL Index
    • MySQL Add Index
    • MySQL REINDEX
    • MySQL UNIQUE INDEX
    • MySQL Clustered Index
    • MySQL? InnoDB Cluster
    • Table in MySQL
    • ALTER TABLE MySQL
    • MySQL Temporary Table
    • MySQL Clone Table
    • MySQL Repair Table
    • MySQL Lock Table
    • MySQL Optimize Table
    • TRUNCATE TABLE MySQL
    • MySQL Table Dump
    • MySQL Update Set
    • MySQL ALTER TABLE Add Column
    • MySQL RANK()
    • MySQL CTE
    • MySQL LAG()
    • MySQL GROUP_CONCAT()
    • MySQL EXTRACT()
    • MySQL REPLACE
    • MySQL AUTO_INCREMENT
    • MySQL SYSDATE()
    • MySQL NULLIF()
    • MySQL Substring
    • MySQL SUBSTRING_INDEX()
    • MySQL LOWERCASE
    • MySQL Row
    • MySQL NOW
    • MySQL CEIL
    • MySQL Alias
    • MySQL Trigger
    • MySQL SHOW Triggers
    • MySQL UPDATE Trigger
    • MySQL DELETE Trigger
    • MySQL AFTER UPDATE Trigger
    • MySQL Stored Procedure
    • ROLLUP in MySQL
    • MySQL? INSTR()
    • MySQL Subquery
    • MySQL Timestamp
    • MySQL? Hour()
    • MySQL MOD()
    • MySQL DATE_FORMAT()
    • ALTER Column in MySQL
    • MySQL Rename Column
    • MySQL Interval
    • MySQL CURDATE
    • MySQL BIT
    • MySQL Binlog
    • MySQL Average
    • MySQL TEXT
    • MySQL SHOW
    • MySQL Offset
    • MySQL Timezone
    • mysql_real_escape_string
    • MySQL Datetime
    • MySQL DATE_SUB()
    • MySQL FULLTEXT
    • MySQL DATE_ADD()
    • MySQL sum()
    • MySQL Merge
    • MySQL BigInt
    • MySQL ROUND
    • MySQL VARCHAR
    • MySQL Decimal
    • MySQL Limit
    • MySQL today()
    • MySQL WEEKDAY
    • MySQL Split
    • MySQL Create Function
    • MySQL BLOB
    • MySQL encode()
    • MySQL Primary Key
    • MySQL Foreign Key
    • Unique Key in MySQL
    • MySQL Drop Foreign Key
    • MySQL DROP TRIGGER
    • MYSQL Database
    • Delete Database MySQL
    • MySQL Root
    • MySQL Root Password
    • MySQL Client
    • MySQL Users
    • MySQL?User Permissions
    • MySQL add user
    • MySQL List User
    • MySQL Show Users
    • MySQL User Password
    • MySQL?Cardinality
    • MySQL Workbench
    • MySQL Backup
    • MySQL REVOKE
    • MySQL Dump
    • MySQL Cluster
    • MySQL Partitioning
    • MySQL Full Text Search
    • MySQL Admin Tool
    • MySQL Export Database
    • MySQL Export to CSV
  • Basic
    • MySQL floor
    • MySQL DESCRIBE table
    • MySQL encryption
    • Introduction to MySQL
    • Is SQL Server a Database?
    • What is MySQL
    • Is MySQL Programming Language
    • MySQL Server
    • MySQL Community Server
    • How To Install MySQL
    • MySQL Versions
    • MySQL OpenSource
    • MySQL GUI Tools
    • MySQL Grant
    • MySQL Error 1064
    • MySQL Drop Database
    • MySQL not equal
    • MySQL SELECT INTO Variable
    • MySQL Commands
    • MySQL Operators
    • What is MySQL Schema
    • Wildcards in MySQL
    • MySQL Constraints
    • MySQL Administration
    • MySQL Data Type
    • MYSQL COMMIT
    • MySQL FORMAT
    • Timestamp to Date in MySQL
    • MySQL DATEDIFF
    • MySQL?Incremental Backup
    • MySQL JSON Data Type
    • MySQL ENUM
    • MySQL Default Port
    • Cheat Sheet MySQL
  • Queries
    • MySQL Queries
    • MySQL Query Commands
    • SELECT in MySQL
    • MySQL INSERT IGNORE
    • MySQL having
    • ORDER BY in MySQL
    • MySQL Cheat Sheet
    • MySQL ORDER BY Random
    • MySQL ORDER BY DESC
    • MySQL GROUP BY
    • MySQL GROUP BY Count
    • MySQL GROUP BY month
    • MySQL WHERE Clause
    • MySQL WITH
    • MySQL FETCH
    • MySQL DDL
    • MySQL DML
    • MySQL WHERE IN Array
    • MySQL Fetch Array
    • MySQL ISNULL
    • MySQL Index Types
    • Mysql? Export Schema
    • Amazon RDS for MySQL
    • MySQL greatest
  • Database
    • What is Data Modeling
    • What is Data Processing
    • DBMS Architecture
    • DBMS Keys
    • Careers in Database Administration
    • What is MySQL Database
    • MySQL Relational Database
    • How to Connect Database to MySQL
    • MySQL Database Repair
    • RDBMS Interview Questions
    • DBMS Interview Questions
  • Joins
    • Joins in MySQL
    • MySQL Outer Join
    • Left Outer Join in MySQL
    • MySQL Self Join
    • Natural Join in MySQL
    • MySQL DELETE JOIN
    • MySQL Update Join
    • MySQL Cross Join
  • Advanced
    • MySQL Formatter
    • MySQL TINYINT
    • MySQL Grant All Privileges
    • MySQL DROP TABLE
    • MySQL rename database
    • MySQL Flush Privileges
    • MySQL super Privilege
    • MySQL Character Set
    • MySQL Log File
    • MySQL Flush Log
    • Grant Privileges MySQL
    • MySQL WHILE LOOP
    • IF Statement in MySQL
    • MySQL CASE Statement
    • MySQL IF Function
    • MySQL IF EXISTS
    • MySQL UUID
    • Views in MySQL
    • MySQL Replication
    • MySQL Partition
    • Toad for MySQL
    • Navicat for MySQL
    • MySQL AES_Encrypt
    • MySQL Performance Tuning
    • MySQL Transaction
    • MySQL? sort_buffer_size
    • MySQL? Sync
    • MySQL? Query Cache
    • MySQL Collation
    • MySQL ODBC Driver
    • MySQL Partitioning
    • MySQL InnoDB
    • MySQL Float vs Decimal
    • MySQL Union vs Union All
  • Interview Questions
    • MySQL Interview Questions

Related Courses

MS SQL Certification Courses

Oracle Certification Courses

PL/SQL Certification Courses

MIN() in MySQL

By Aanchal SharmaAanchal Sharma

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

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

MIN ( [DISTINCT] Expression)

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (85,992 ratings)

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.

MIN() in MySQL 1

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:

MIN() in MySQL 2

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:

min function with where clause

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:

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 – 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:

min function with group by clause

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:

min function with order by clause

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.

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

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:

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

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 – 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:

find minimum character length

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 –

  1.  LIKE in MySQL
  2. ORDER BY in MySQL
  3. Table in MySQL
  4. MySQL Aggregate Function
Popular Course in this category
MySQL Training Program (12 Courses, 10 Projects)
  12 Online Courses |  10 Hands-on Projects |  92+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

MS SQL Training (16 Courses, 11+ Projects)4.9
Oracle Training (14 Courses, 8+ Projects)4.8
PL SQL Training (4 Courses, 2+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • 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

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

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

EDUCBA Login

Forgot Password?

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

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

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

Let’s Get Started

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