×

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 Composite Index

Composite Index A composite index is an index that is used on multiple columns. MySQL management system manages multiple columns simultaneously. Therefore, the single index contains multiple columns in one query...

3 minutes read.

MySQL CEIL() function

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

3 minutes read.

MySQL RTRIM() function

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

2 minutes read.

MySQL HEX() function

In this context, we will learn how we can use the MySQL HEX() function with proper syntax and good examples. Introduction of MySQL HEX() function For returning an equivalent hexadecimal string value...

3 minutes read.

MySQL vs SQL

What is MySQL? The open-source MySQL relational database management system is a vital software component for web-based applications. As the data is saved and sent over the internet, databases and related...

6 minutes read.

MySQL ISNULL function

This function is used to check the null value in table data. It returns the result in a Boolean form. If table data is null, then the output becomes 1....

2 minutes read.

MySQL RIGHT() Function

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

3 minutes read.

MySQL Truncate() Function

In this context, we will learn how we can use the MYSQL TRUNCATE function to truncate a number to a mentioned number of decimal places. Syntax of MySQL Truncate Function A number...

2 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 Subquery

The subquery is a MySQL query used to manage data operations. Mainly subquery helps to retrieve data with the necessary condition. It creates a nested query with two different queries....

5 minutes read.

MySQL Stored Procedure

MySQL creates the "stored procedure" function to operate database information. You can use parameters, blocks, and statements to create a new procedure. The procedure requires a database table to use...

3 minutes read.

MySQL Date and Time function

The date function displays day, year, month, time, and current date. It shows the date and time as per the requirement of the applications. The data either store on the...

8 minutes read.

MySQL LOG2() function

In this context, we will learn how we can use the MySQL LOG2() function to calculate the Logarithm of a specific number with base 2 with proper syntax and examples. Introduction...

2 minutes read.

MySQL Operators

MySQL Operators MySQL operator needs advanced operation on the table and its data. This operator works with the "WHERE" clause. MySQL operators are a statement to modify information. It helps to...

13 minutes read.

MySQL Tutorial

In this tutorial, you will get information about the MySQL management system. This tutorial will cover the basic and advanced level MySQL concepts with examples that will help you become...

5 minutes read.

MySQL LOG10() function

In this context, we will learn how we can use the MySQL LOG10() function with proper syntax and good examples. Introduction of MySQL LOG10() function To evaluate the natural logarithmic value of...

2 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 Character Length Function

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

2 minutes read.

MySQL Union

MySQL contains the same categories of data in a different table. Sometimes, you require multiple tables to collect data together. MySQL union is a function to combine two table's data...

5 minutes read.

MySQL RPAD() function

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

2 minutes read.