SQL MIN() function is used to return minimum value from a column of a table using a SELECT statement.

SQL MIN function syntax

Syntax:
SELECT MIN([COLUMN_NAME]) FROM [TABLE_NAME]

MIN function Query Example:
To get the minimum price of the book from a BOOKS table, we need use MIN function on PRICE column as below.

SELECT MIN(PRICE) FROM BOOKS;

SQL MIN function with Group By

SQL MIN() function syntax  when grouping the results by one or more columns.

Syntax:

SELECT  [COLUMN1],[COLUMN2],  ……MIN[COLUMN_N]
FROM [TABLE_NAME]  [WHERE CONDITION]
GROUP BY COLUMN1,COLUMN2……[COLUMNN]

SQL MIN() Example with group by

Let’s say we want to get minimum price of a book titled “The Castle “from the BOOKS table and there are multiple books with same title with different prices also.

TITLEPRICE
Historica98
The Castle100
The Castle150
Animal Farm300
Animal Farm120

So, the query below will find the min price of books of same titles

Query example:
SELECT TITLE, MIN(PRICE) FROM BOOKS GROUP BY TITLE, PRICE;

Output

TITLEPRICE
Historica98
The Castle100
Animal Farm120

Related Posts