©SQL :: Manager
HomeProgram InformationTechnical detailsFree downloadDonate for a cup of coffee
myway SQL Manager for MySQL and MariaDB, Download on Uptodown
SQL functionMySQLMariaDB

The SQL MIN VALUE Function - Maximum, smallest value

MINSyntax:MIN(Expression or column)
Return value:Depending on the data types used
Function type:Aggregate function, Window function
Function Description

The MIN() function returns the smallest or minimum value of "Expression or column".

MIN() can also take a string argument, in which case it returns the minimum string value.

Using the DISTINCT clause produces no different results.

SET and ENUM columns are evaluated using their string values.

As an aggregate function, the MIN() function can be used with a GROUP BY clause.

MIN() can be used as a window function.

The MIN() function returns NULL if no result is found.

SQL Examples for the MIN function

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

DROP TABLE IF EXISTS `test_table`;
    
CREATE TABLE `test_table` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `type` enum('First','Second','Third','Last'DEFAULT NULL,
  `num` int(11) NOT NULL DEFAULT 0,
  `price` decimal(10,2) NOT NULL DEFAULT 0.00,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;

INSERT INTO `test_table` (`id`,`name`,`type`,`num`,`price`VALUES 
(1,'Art 1','First',1,10.50),
(2,'Art 2','Second',3,12.50),
(3,'Art 3','Second',2,15.50),
(4,'Art 4','Third',5,25.50),
(5,'Art 5','Last',2,5.50),
(6,'Art 6','First',12,1.50),
(7,'Art 7','Third',20,1.75),
(8,'Art 8','Last',100,11.90);

select * from `test_table`;

select min(num),min(price),min(`type`from test_table;
select min(num),min(price) from test_table where id>8;

/* as Window function */

SELECT name, `type`, price, min(price) 
  OVER (PARTITION BY `type`AS `min` FROM test_table;

id
name
type
num
price
int(11)varchar(20) LATIN1enum LATIN1int(11)decimal(10,2)
1Art 1First110.50
2Art 2Second312.50
3Art 3Second215.50
4Art 4Third525.50
5Art 5Last25.50
6Art 6First121.50
7Art 7Third201.75
8Art 8Last10011.90
min(num)
min(price)
min(`type`)
int(11)decimal(12)char(6) BINARY
11.50First
min(num)
min(price)
int(11)decimal(12)
NULLNULL
name
type
price
min
test_table
varchar(20) LATIN1
test_table
enum LATIN1
test_table
decimal(10,2)

decimal(12)
Art 1First10.501.50
Art 6First1.501.50
Art 2Second12.5012.50
Art 3Second15.5012.50
Art 7Third1.751.75
Art 4Third25.501.75
Art 8Last11.905.50
Art 5Last5.505.50
The examples were created with the MyWAY SQL manager: Download

How to use the MIN() function in MySQL and MariaDB databases

The MIN() function in MySQL and MariaDB is used to get the minimum value from a column or a set of values. It is an aggregate function that operates on a specific table column or expression and returns the lowest value within that column or expression. Calculations or transformations for column values ​​can also be carried out within the expression. The function works with numeric data types and date and time data types. For other data types, the lowest value is returned based on the collation of the column. NULL values ​​are excluded from the calculation.

The main purpose of the MIN() function is to get the minimum value from a column, but it can also be used in conjunction with the GROUP BY clause to calculate the minimum value for each group. This makes it possible to summarize data and to identify the minimum values ​​within certain subsets. Also, the function can be used to order SQL query results based on minimum value by using the function with the ORDER BY clause. In subqueries or conditional statements, the function can also be used to retrieve related data or make decisions based on the minimum value, for example as a condition in a WHERE clause.
MEDIANMICROSECONDMIDMINMINUTEMODMONTH

Further MySQL und MariaDB SQL Aggregate functions

ANY_VALUEANY_VALUE(Column)
More about ANY_VALUE Function

AVGAVG(Expression or column)
More about AVG Function

BIT_ANDBIT_AND(Expression or column)
More about BIT_AND Function

BIT_ORBIT_OR(Expression or column)
More about BIT_OR Function

BIT_XORBIT_XOR(Expression or column)
More about BIT_XOR Function

COUNTCOUNT(Expression or column)
More about COUNT Function

GROUP_CONCATGROUP_CONCAT([DISTINCT] Expression [, Expression ...] [ORDER BY ... [ASC | DESC]] [SEPARATOR ...] [LIMIT ...])
More about GROUP_CONCAT Function

JSON_ARRAYAGGJSON_ARRAYAGG(Expression or column)

JSON_OBJECTAGGJSON_OBJECTAGG(Expression or column)

MAXMAX(Expression or column)
More about MAX Function

SUMSUM(Expression or column)
More about SUM Function

STDSTD(Expression or column)
More about STD Function

STDDEVSTDDEV(Expression or column)
More about STDDEV Function

STDDEV_POPSTDDEV_POP(Expression or column)
More about STDDEV_POP Function

STDDEV_SAMPSTDDEV_SAMP(Expression or column)
More about STDDEV_SAMP Function

VAR_POPVAR_POP(Expression or column)
More about VAR_POP Function

VAR_SAMPVAR_SAMP(Expression or column)
More about VAR_SAMP Function

VARIANCEVARIANCE(Expression or column)
More about VARIANCE Function

Numeric functionsString functionsRegular ExpressionsDate and Time functions
Comparison functionsEncryption & CompressionConversion functionsNULL functions
Aggregate functionsWindow functionsJSON functionsGeometric functions
Sequence functionsInformation functionsDynamic ColumnsMiscellaneous functions
More information about the MIN SQL function: mysql.com and mariadb.com
Updated: 2023-11-21ImprintContactTerms & conditionsPrivacy & CookiesUpdatesSitemapFacebookLinkedinTwitterStatistics©2020-2024 Heino Cunze-Fischer