The SQL min()
function is used to find the minimum/lowest value in the given column. The syntax for using the function is given below.
1
2
3
**SELECT**
**MIN**``(``**column**``)
**FROM**
**Table**
**WHERE**
condition;
Where, column
and Table
are the column name and table name respectively for which we need to find the minimum value. The [WHERE](https://www.journaldev.com/18411/sql-where-clause)
condition is used to narrow down the set of values on the basis of the condition
.
Let us look at an example to get a better understanding.
Example: The table below contains the score of 3 different students in 3 different subjects(Maths, Science, and English) out of 100.
RollNameMathsScienceEnglish1John9589902Kate7890913Alex879289
Table – Marks
Now, let us try to get the minimum marks that a student scored at Maths.
1
2
**SELECT**
**MIN**``(Maths)
**FROM**
Marks;
Output:
1
Example – To calculate the overall minimum marks that a student scored out of 300.
1
2
**SELECT**
**MIN**``(Maths + Science + English)
**FROM**
Marks;
Output:
1
259
We get the desired output as Roll 2 got the lowest total marks(78 + 90 + 91 = 259).
The SQL max()
function on the other hand is used to find the maximum/highest value in the given column. The syntax for using the function is given below.
1
2
3
**SELECT**
**MAX**``(``**column**``)
**FROM**
**Table**
**WHERE**
condition;
Similar to the min()
function, here too column
and Table
are the column name and table name respectively for which the maximum value is to be calculated. The [WHERE](https://www.journaldev.com/18411/sql-where-clause)
condition is used to narrow down the set of values on the basis of the condition
.
Example – Considering the same Marks
table used for the min()
function, let us this time try to calculate the maximum marks that a student scored in English.
1
2
**SELECT**
**MAX**``(English)
**FROM**
Marks;
Output:
1
Example – To calculate the maximum marks that a student scored out of 300 in **all **the subjects.
1
2
**SELECT**
**MAX**``(Maths + Science + English)
**FROM**
Marks;
Output:
1
274
From the table Marks it is clear that Roll 1 has the highest total marks(95 + 90 + 89 = 274). Hence, the output is justified.
#sql #functions #max #min