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 |
