MySQL MIN Function
Introduction to MySQL MIN Function
This MySQL tutorial explains how to use the MIN function with syntax and examples. The MySQL MIN function returns the minimum value in a set of values.
MIN() Syntax
SELECT MIN(column_name) FROM table_name WHERE condition;
To understand MIN function, consider an “books” table, which is having the following records:
SELECT * FROM books;
BookId | BookName | Price | Author | publishedDate |
---|---|---|---|---|
1 | Learning PHP, MySQL, and JavaScript | 17 | Robin Nixon | 2017-02-02 |
2 | Ubuntu: Up and Running | 23 | Robin Nixon | 2017-03-23 |
3 | PHP and MySQL Web Development | 12 | Luke Welling | 2017-06-14 |
4 | Murach's PHP and MySQL | 14 | Joel Murach | 2017-06-17 |
5 | Murach's Java Programming | 62 | Joel Murach | 2017-07-28 |
6 | Head first php mysql | 22 | Lynn Beighley | 2017-07-31 |
7 | Head first sql | 11 | Lynn Beighley | 2017-09-10 |
8 | HTML5 for IOS and Android: A Beginner's Guide | 4 | Robin Nixon | 2017-09-12 |
The following MySQL statement finds the price of the most cheapest book:
SELECT MIN(Price) AS SmallestPrice FROM books;
SmallestPrice |
---|
4 |
The following MySQL statement finds the price of the most cheapest book in each author, sorted low to high:
SELECT MIN(Price) AS SmallestPrice, Author FROM books GROUP BY Author ORDER BY SmallestPrice ASC;
SmallestPrice | Author |
---|---|
4 | Robin Nixon |
11 | Lynn Beighley |
12 | Luke Welling |
14 | Joel Murach |