MAX function in mysql

By | January 18, 2014

This function using to find out the maximum value in a recordset

Select * from mydoubts_data

————————————-
|id | jobid | name | salary | age|
————————————-
|1 | 121 | vasanthan| 2000 |31 |
|2 | 122 | John | 3000 |29 |
|3 | 123 | Kiran | 3400 |22 |
|4 | 154 | Raju | 2800 |35 |
|5 | 155 | Raju | 2906 |40 |
|6 | 143 | Kiran | 3400 |42 |

Depend on the above data you can find the records having maximum age

Select MAX(age) from mydoubts_data

—————-
| MAX(age) |
—————
| 42 |
—————

You can also use the query to find out the maximum of each record as follows

select id,jobid,name,max(age) from mydoubts_data group by name

——————————–
|id | jobid | name | age|
——————————–
|2 | 122 | John | |29 |
|6 | 143 | Kiran | |42 |
|5 | 155 | Raju | |40 |
|1 | 121 | vasanthan| |31 |

we can find out the maximum and minimum age from the record set

select MIN(age) leastval,MAX(age) maxval from mydoubts_data
———————
| leastval | maxval |
———————-
| 22 | 42 |
———————