MySQL Tutorial
The MAX() function returns the largest value of the selected column.
SELECT MAX(column_name)
FROM table_name
WHERE condition;
Let's put these statements into real use.
We've a table named Students in our database that contains the following records:
ROLL_NO | NAME | SUBJECT |
---|---|---|
1 | Will | JAVA |
2 | SAM | Python |
3 | Sara | HTML | 4 | Rim | Java |
5 | Micheal | SQL |
6 | Lara | JAVA |
Now lets select the largest value of ROLL_NO from Students Table.
SELECT MAX(ROLL_NO) AS "SmallestRollNo"
FROM Students;
The above SELECT statement returns the largest value .
SmallestRollNo |
---|
6 |
We can select the largest value from table with where condition.
SELECT MAX(ROLL_NO) AS "SmallestRollNo"
FROM Students
WHERE SUBJECT='JAVA'
The above SELECT statement retrieve the smallest value if the SUBJECT is JAVA.
SmallestRollNo |
---|
6 |