MySQL MIN() and MAX() Functions

MySQL MIN() and MAX() Functions

The MIN() and MAX() functions in MySQL are aggregate functions used to find the smallest and largest values in a selected column.

They are commonly used with numeric, date, or even text columns.


โœ… MIN() Function

The MIN() function returns the lowest (minimum) value from a column.

Syntax:



๐Ÿง  Example Table: students

id name age marks
1 John 20 85
2 Emma 21 90
3 Raj 22 76
4 Arjun 23 92
5 Sara 19 88

โœ… Example: Find Minimum Marks


โœ” Output:

LowestMarks
76

๐Ÿ‘‰ MIN() with Condition


โœ” Finds youngest student among those scoring above 80.



๐Ÿš€ MAX() Function

The MAX() function returns the highest (maximum) value from a column.

Syntax:



โœ… Example: Find Maximum Age


โœ” Output:

OldestStudent
23

๐Ÿ‘‰ MAX() with Condition


โœ” Returns highest marks of students younger than 22.



๐Ÿงพ MIN() and MAX() with Text Columns

These functions also work on text (alphabetical sorting).


๐Ÿ“Œ Text comparison follows alphabetical order (A โ†’ Z).


๐Ÿ“Œ Combined Example

You can retrieve both in one query:



๐ŸŽฏ Summary

Function Returns Works On
MIN() Smallest value Numbers, dates, text
MAX() Largest value Numbers, dates, text

CodeCapsule

Sanjit Sinha โ€” Web Developer | PHP โ€ข Laravel โ€ข CodeIgniter โ€ข MySQL โ€ข Bootstrap Founder, CodeCapsule โ€” Student projects & practical coding guides. Email: info@codecapsule.in โ€ข Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *