The max () function of MySQL is used to find a record of the maximum value in a recordset.
The EMPLOYEE_TBL table that you want to know about Max functionality has the following records:
Mysql> SELECT * from EMPLOYEE_TBL;
+------+------+------------+--------------------+
| id | name | work_date | daily_typing_pages
| +------+------+------------+--------------------+
| 1 | John | 2007-01-24 | The |
| 2 | Ram | 2007-05-27 | The |
| 3 | Jack | 2007-05-06 | 170 |
| 3 | Jack | 2007-04-06 | |
| 4 | Jill | 2007-04-06 | The |
| 5 | Zara | 2007-06-06 | A |
| 5 | Zara | 2007-02-06 | |
+------+------+------------+--------------------+
7 rows in Set (0.00 sec)
Now, suppose you want to simply use the following command based on the maximum value of the daily_typing_pages in the table above:
Mysql> SELECT MAX (daily_typing_pages)
-> from Employee_tbl;
+-------------------------+
| MAX (daily_typing_pages) |
+-------------------------+
| |
+-------------------------+
1 row in Set (0.00 sec)
All records can be found, with a maximum value of each using the GROUP BY clause as follows:
mysql> SELECT ID, name, work_date, MAX (daily_typing_pages)
-> from Employee_tbl GROUP by name;
+------+------+------------+-------------------------+
| id | name | work_date | MAX (daily_typing_pages) |
+------+------+------------+-------------------------+
| 3 | Jack | 2007-05-06 | 170 |
| 4 | Jill | 2007-04-06 | The |
| 1 | John | 2007-01-24 | The |
| 2 | Ram | 2007-05-27 | The |
| 5 | Zara | 2007-06-06 | |
+------+------+------------+-------------------------+
5 rows in Set (0.00 sec)
You can also use the Min function and the Max function to find the lowest value, try the following example:
Mysql> SELECT MIN (daily_typing_pages) least, Max (daily_typing_pages) Max
-> from Employee_tbl;
+-------+------+
| least | max |
+-------+------+
| 100 | |
+-------+------+
1 row in Set (0.01 sec)