MySQL - MIN() Function
The MySQL MIN() function is a type of aggregate function. It is used to retrieve the minimum value among the values in a particular column/table.
This function is mostly used in cases where a user needs to find the patterns in data stored. For instance, consider an educational institution. Suppose the institution maintains a database to track the performance of its students. In order to find the student with the lowest mark in their institution, the MIN() function can be applied on a student database table.
Syntax
Following is the syntax of MySQL MIN() function −
MIN(expr);
Parameters
This method accepts a parameter. The same is described below −
expr: This can be an expression or a particular column for which we want to find the minimum value.
Return value
This function returns the lowest (minimum) value from the specified column in the specified table.
Example
In the below query, we are creating a table named CUSTOMERS using the MySQL CREATE TABLE statement as follows −
CREATE TABLE CUSTOMERS ( ID INT AUTO_INCREMENT, NAME VARCHAR(20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );
The following query inserts 7 records into the above created table −
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 ), (2, 'Khilan', 25, 'Delhi', 1500.00 ), (3, 'Kaushik', 23, 'Kota', 2000.00 ), (4, 'Chaitali', 25, 'Mumbai', 6500.00 ), (5, 'Hardik', 27, 'Bhopal', 8500.00 ), (6, 'Komal', 22, 'Hyderabad', 4500.00 ), (7, 'Muffy', 24, 'Indore', 10000.00 );
To display all the records that are inserted, execute the following query −
Select * From CUSTOMERS;
Following is the CUSTOMERS table −
| ID | NAME | AGE | ADDRESS | SALARY |
|---|---|---|---|---|
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | Kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | Hyderabad | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
Now, we are using the MySQL MIN() function to calculate the minimum SALARY among all the customers −
SELECT MIN(SALARY) from CUSTOMERS;
Output
This will produce the following result −
| MIN(SALARY) |
|---|
| 1500.00 |
Example
If the result-set obtained from MIN() function contains no rows, this function will return NULL as an output.
In the below query, we are retrieving the minimum SALARY of a student named 'Hrudai'. But the table holds no records of 'Hrudai' −
SELECT MIN(SALARY) FROM CUSTOMERS WHERE NAME = 'Hrudai';
Output
This will return NULL as output because there is no customer whose name is 'Hrudai' −
| MIN(SALARY) |
|---|
| NULL |
Example
We can also use the MySQL MIN() function in subqueries to calculate the maximum salary of customers.
The below query selects the name of the customer whose 'SALARY' is lowest −
SELECT * FROM CUSTOMERS WHERE SALARY = (SELECT MIN(SALARY) FROM CUSTOMERS);
Output
Khilan is the customer who has the lowest salary among all customers −
| ID | NAME | AGE | ADDRESS | SALARY |
|---|---|---|---|---|
| 2 | Khilan | 25 | Delhi | 1500.00 |