MySQL MIN() Function with Examples


How to Use the MySQL MIN() Function

In this tutorial, you'll learn how to use the MySQL MIN() function. MIN() is a function to return the minimum value in a group of values.

For example, let's say you have a list of items that you're selling, and you want to know which item has the lowest sales price. In this case, you can use the MIN() function.

MySQL MIN() Syntax

The syntax of the MIN() function is as follows:

MIN(expression)

Examples

The following is the demo orders table we're going to use in the examples:

order_numberorder_datecustomeramount
#ORD_000012019-11-12Gloria Harvey2450.50
#ORD_000022019-10-03Annie Mitchell2000.00
#ORD_000032019-10-03Grant Daniels1890.40
#ORD_000042019-11-10Thomas Pratt1370.50
#ORD_000052019-11-12Ronald Houston2780.50
#ORD_000062019-11-12Vera Bell1290.00
#ORD_000072019-10-04Lindsay Leonard1768.00
#ORD_000082019-10-04Mario Simmons2340.50

Example 1: MIN() without WHERE Clause

Let's say you want to get the smallest amount in the "amount" column. You can write the statement as follows:

SELECT MIN(orders.amount) AS SmallestAmount
FROM orders;

Result:

This statement returns 1290.00, which is the smallest value.

Example 2: MIN() and WHERE Clause

The following SQL statement returns the smallest amount, where order_date is 2019-11-12:


SELECT MIN(orders.amount) AS SmallestAmount
FROM orders
WHERE order_date = '2019-11-12';

Result:

The above statement returns 1290.00.

Example 3: MIN() and AND Operator

In this example, I show you how to use the MIN() function and AND operator in the WHERE clause.
The following SQL statement returns the minimum value between two dates:

SELECT MIN(o.amount) AS SmallestAmount
FROM orders o
WHERE o.order_date >= '2019-10-01' AND o.order_date <= '2019-10-05';

Result:

MySQL searches for the smallest amount between 2019-10-01 and 2019-10-05 and returns 1768.00.

In this tutorial, you've learned how to use the MySQL MIN() function. MIN() is an aggregate function to get the minimum value in a group. If you want to find the maximum value in a group of values, then you can use the MAX() function.

Leave a Reply

Your email address will not be published. Required fields are marked *