SQL MIN() and MAX() Functions

The SQL MIN() and MAX() functions are used to return the minimum and maximum values ​​from the selected column.
MIN() :- Returns Smallest value.
MAX() :- Returns Largest value.

MIN() Functions Syntax :

The syntax of the MIN() Functions is −

SELECT MIN(column-name)
FROM table-name
WHERE condition;

This will return the smallest value from the column-name records fulfilling the WHERE condition.



MAX() Functions Syntax :

The syntax of the MAX() Functions statement is −

SELECT MAX(column-name)
FROM table-name
WHERE condition;

This will return the Largest value from the column-name records fulfilling the WHERE condition.



MIN() and Max() Function Example :

Consider the Customer table with the following records -

CustomerID CustomerName Age Address CustomerSalary
1 Aarav 28 Udaipur 28000
2 Vivaan 25 Mumbai 30000
3 Reyansh 28 Chennai 35000
4 Muhammad 24 Udaipur 50000
5 Sai 30 Mumbai 27000


TEST CASE 1 :- The following code is an example, which would return minimum age from the record of Customer table.

SELECT MIN(Age) AS SmallestAge
FROM Customer; 


The result for the respective sql query is as follows −

SmallestAge
24



TEST CASE 2:- The following code is an example, which would return maximum age from the record of Customer table.

SELECT MAX(Age) AS LargestAge
FROM Customer; 


The result for the respective sql query is as follows −

LargestAge
30





Visit :


Discussion



* You must be logged in to add comment.