Mysql Min



The MySQL MIN function returns the minimum value of an expression. SQL is a standard language for storing, manipulating and retrieving data in databases. Our SQL tutorial will teach you how to use SQL in: MySQL, SQL Server, MS Access, Oracle, Sybase, Informix, Postgres, and other database systems. Session 3: Migration from Amazon RDS to MySQL Database Service - Sunny Piao, MySQL Solution Engineer, Korea (30mins) Session 4: Upgrading MySQL to MySQL 8.0 on OCI - Ivan Ma, MySQL Solution Engineer, ASEAN/GCR (45 mins) Q&A Session: Min Whan Chang, MySQL Enterprise Sales Lead, Korea All speakers (15 mins). Introduction to MySQL MIN function. The MIN function returns the minimum value in a set of values. The MIN function is very useful in some scenarios such as finding the smallest number, selecting the least expensive product, or getting the lowest credit limit. Here is the basic syntax of the MIN function.


In this tutorial we will learn about some of the most commonly used aggregate functions like COUNT, MIN, MAX, AVG and SUM in MySQL.

What are Aggregate Functions in MySQL?

These are the functions in MySQL that performs some calculation on a set of values and then returns a single value.

Example: The SUM(column_name) function computes the sum of the values present in a given column.

List of aggregate functions that we will cover in this tutorial.

The orders table

For this tutorial we will be using the orders table shown below.

COUNT

We use the COUNT function to find the number of rows matching the given condition.

Syntax:

In the following example we are listing total number of orders in the orders table.

So, there are total 6 orders in the orders table.

Mysql

We can even use an alias in the above query.

COUNT with WHERE clause

Syntax:

In the following example we are counting total number of orders placed by employeeid 'e04'.

So, employeeid 'e04' placed total 2 orders.

COUNT DISTINCT

If we want to count distinct value in a column then we use DISTINCT in the COUNT function.

Syntax:

In the following example we are counting total number of distinct employees who placed an order.

Mysql Min Two Values

So, total 5 unique employeeid is present in the orders table.

MIN

We use the MIN function to find the minimum value.

Syntax:

In the following example we will display the minimum order amount in the orders table.

So, minimum order amount is 9.50 in the orders table.

We can use an alias for the above query.

MIN with WHERE clause

Syntax:

In the following example we will display the minimum order amount for the employeeid 'e03' in the orders table.

So, employeeid 'e03' has a minimum order amount of 15.00.

MAX

We use the MAX function to find the maximum value.

Syntax:

Min

In the following example we will display the maximum order amount in the orders table.

So, maximum or highest order amount in the orders table is 100.70.

We can use an alias for the given query like the following.

MAX with WHERE clause

Syntax:

In the following example we will display the max order amount for the employeeid 'e01' in the orders table.

So, employeeid 'e01' has placed max value order of 25.50.

AVG

We use the AVG function to find the average.

Mysql Mint

Syntax:

In the following example we will display the average order amount in the orders table.

So, the average order amount in the orders table is 45.478333.

We can also use an alias in the above query.

AVG with WHERE clause

Syntax:

Operator

In the following example we will display the average order amount for the employeeid 'e04' in the orders table.

So, average order amount placed by employeeid 'e04' is 54.745000.

SUM

We use the SUM function to find the sum.

Syntax:

In the following example we will display the sum i.e., total of the order amount in the orders table.

So, total order amount in the orders table is 272.87.

When using an alias the output will look like the following.

SUM with WHERE clause

Syntax:

In the following example we will display the sum i.e., total of the order amount for OPEN orders in the orders table.

So, total order amount for all the orders that are in OPEN state is 162.67.