×

MySQL MIN function

The MIN() function determines the minimum or lowest value of the data set. This function works on numerical data type values. If the table displays zero value, then the row does not exist in the table column. The MIN function returns a minimum or least value of the single or multiple columns.

Syntax

The basic MIN () function syntax shows below.

MIN(function_expression)

MySQL MIN () function with the table syntax shows below.

SELECT MIN(function_expression) FROM Table_Name WHERE condition;

Description

  • The MIN() function returns a minimum value of the table.
  • The "function_expression" represents the value of data for function operation.
  • The "WHERE" clause applies conditions on the MIN() function.

Prerequisite for MIN function

  • Create a new table with the below statement.
mysql> create table pen (  

    product_id int NOT NULL,

    product_name varchar(100) NOT NULL,   

    available_quantity int, 

    required_quantity int, 

    Date date

    ); 
  • Insert the data in the table as per data type.
mysql> insert into pen(product_id,

product_name,

available_quantity, required_quantity,

Date)

VALUES   

(1, 'ink pen', 78, 45, '2021-07-01'), 

(2, 'ball pen', 100, 65, '2021-07-01'), 

(3, 'color pen', 78, 45, '2021-08-24'), 

(4, 'brush pen', 80, 50, '2021-11-21'), 

(5, 'craft pen', 95, 55, '2021-07-01'), 

(6, 'sketch pen', 100, 65, '2021-10-13');
  • You can refer to the table structure and information using the SELECT query.
mysql> SELECT * FROM pen;
MySQL MIN() function

Examples of the MIN function

1) Example: The basic MIN function example shows below. Execute the following query to show the minimum number of the available quantity in a table.

mysql> SELECT MIN(available_quantity) AS availability FROM pen;

 Output

MySQL MIN() function

2) Example: The MIN function with the date value example shows below. The following example shows the lowest date of the table.

mysql> SELECT MIN(Date) AS date FROM pen;

Output

MySQL MIN() function

The minimum function returns the first date of the month or year. The smallest and first date of the table is the first day with seven months in the table.

3) Example: The MIN function with the "DISTINCT" and the "WHERE" clauses example shows below. In this example, we use the "DISTINCT" clause with the "available_quantity" column, and the "WHERE" clause executes the "greater than" condition on the "available_quantity" column.

mysql> SELECT MIN(DISTINCT available_quantity) AS availability FROM pen WHERE available_quantity > 80;

 Output

MySQL MIN() function

Executing the query will return the smallest number after the 80's value in the table. This column shows the "95" value as the output of the function.

4) Example: The MIN functions with multiple columns example shows below.

The below query uses the product_id, date, and available_quantity columns to return the minimum value from the table.

mysql> SELECT MIN(product_id) AS number, MIN(Date) AS date, MIN(available_quantity) AS availability 
FROM pen;

Output

MySQL MIN() function

The minimum function works on multiple tables' columns simultaneously. Each column shows its smallest value without disturbing the entire row.

5) Example: The MIN functions with the "WHERE" clause example shows below.

mysql> SELECT MIN(product_id) AS product_id, MIN(Date) AS date, MIN(available_quantity) AS availability 
 FROM pen WHERE product_id < 4;

Output

MySQL MIN() function

6) Example: The MIN functions with the "WHERE" clause and "AND" operator example shows below. Here the "where" clause comes with the "less than" and "greater than" condition, and the "AND" operator works on the "available_quantity" and "product_id" columns.

mysql> SELECT MIN(product_id) AS product_id, MIN(Date) AS date, MIN(available_quantity) AS availability 
 FROM pen WHERE available_quantity < 80 AND product_id < 3;

Output

MySQL MIN() function

The output image displays the value between the "AND" condition. The product_id is "1" because of the "less than 3" condition. The availability shows a "78" value due to the "less than 80" condition.

7) Example: The MIN functions with the "GROUP BY" clause example shows below. This example uses the min() function with the "group by" clause.

mysql> SELECT MIN(product_id) AS product_id, 
MIN(Date) AS date, 
MIN(available_quantity) AS availability 
FROM pen 
GROUP BY available_quantity;

Output

MySQL MIN() function

The GROUP BY clause displays the output as per the grouping of the given columns. The table removes the third and sixth rows of the table. The available_quantity or availability column removes duplicate values and shows only single value rows.

8) Example: The MIN functions with the "GROUP BY" and "HAVING" clause example shows below.

mysql> SELECT MIN(product_id) AS product_id,

mysql> SELECT MIN(product_id) AS product_id, 
MIN(Date) AS date, 
MIN(available_quantity) AS availability 
FROM pen 
GROUP BY available_quantity 
HAVING available_quantity > 80;

Output

MySQL MIN() function

The above image gives two rows as per the "group by" and "HAVING" conditions. The group by clause removes a similar value of the "availability" columns. The having clause displays greater than 80 quantities value of the table.

9) Example: The MIN functions with the "ORDER BY" clause example shows below. In this statement, the "ORDER BY" clauses display in a particular order based on the specified column.

mysql> SELECT MIN(product_id) AS product_id, 
MIN(Date) AS date, 
MIN(available_quantity) AS availability 
FROM pen 
ORDER BY available_quantity ASC;

Output

MySQL MIN() function

Related Topics

MySQL POWER() function

In this context, we will learn how we can use the MySQL POWER() function with proper syntax and good examples. Introduction of MySQL POWER() function The value of a number raised to...

3 minutes read.

MySQL FIELD() function

In this context, we will learn how we can use the MySQL FIELD() function with proper syntax and good examples. Introduction of MySQL FIELD() function The index position of a mentioned value...

3 minutes read.

MySQL RIGHT JOIN

MySQL right join links two tables with each other. The left table column connects with the complete right side table. Each row of the right table tries to connect with...

4 minutes read.

MySQL DATE_ADD() function

In this context, we will learn how we can use the MySQL DATE_ADD() function with proper syntax and good examples. Introduction of MySQL DATE_ADD() function In MySQL, the DATE_ADD() function is used...

3 minutes read.

MySQL ROUND() function

In this context, we will learn how we can use the MySQL ROUND() function with proper syntax and good examples. Introduction of MySQL ROUND() function The ROUND() function in MySQL is used...

3 minutes read.

MySQL AVG function

This function works on numerical data type values. The average function shows the average value of the data set. If an average function returns a null value, then the row...

5 minutes read.

MySQL EXPORT_SET() function

In this context, we will learn how we can use the MySQL EXPORT_SET() function with proper syntax and good examples. Introduction of MySQL EXPORT_SET() function This function returns a string and shows...

3 minutes read.

MySQL MOD() function

In this context, we will learn how we can use the MySQL MOD() function with proper syntax and good examples. Introduction of MySQL MOD() function Basically, in MySQL, the MOD() function is...

3 minutes read.

MySQL POW() function

In this context, we will learn how we can use the MySQL POW() function with proper syntax and good examples. Introduction of MySQL POW() function This function in MySQL is used to...

2 minutes read.

MySQL Clauses

MySQL Clauses MySQL system clauses are keywords or statements to handle information. It helps to operate a group of the data and apply it to require conditions. The clauses apply conditions...

16 minutes read.

MySQL Command line client Basic Queries

MySQL – command line client Basic Queries There are many queries to work with the MySQL command-line client. Today, we will explore MySQL basic queries and their function. The MySQL query...

8 minutes read.

find_in_set() function in MySQL

This Function is used for finding the position of a particular string from the list of strings. For suppose if the specified string is repeated multiple times, then this functions...

4 minutes read.

MySQL LN() function

In this context, we will learn how we can use the MySQL LN() function with proper syntax and good examples. Introduction of MySQL LN() function For evaluation of the natural logarithm of...

2 minutes read.

MySQL QUARTER() function

In this context, we will learn how we can use the MySQL QUARTER() function with proper syntax and good examples. Introduction of MySQL QUARTER() function The QUARTER() function in MySQL returns the...

3 minutes read.

MySQL Features

Features of MySQL MySQL is a relational database management system (RDBMS): It is a collection of many programs and makes relations with many other programs. Easy to use MySQL database: MySQL...

2 minutes read.

MySQL Drop Index

Sometimes we have an index that is not required in data operations. In that case, we can use this statement to remove an existing index from the table. We can...

4 minutes read.

MySQL DATEDIFF() function

In this context, we will learn how we can use the MySQL DATEDIFF() function with proper syntax and good examples. Introduction of MySQL DATEDIFF() function DATEDIFF() function in MySQL is used to...

3 minutes read.

MySQL Create index

An index creation helps to make a row of the table unique. The index operates and handles table data quickly. MySQL index requires NOT NULL column constraint. The index column...

2 minutes read.

MySQL Join

The relational database system needs to interconnect multiple tables with each other. MySQL is a popular and easy data management system to connect multiple tables. The foreign key is used...

5 minutes read.

MySQL COUNT function

The count function returns the number of rows in the table. This function shows either the entire rows count or the required row count of the table. It determines the...

5 minutes read.