Get the maximum value of a column with MySQL Aggregate function


To get the maximum value of a column, MySQL has a predefined aggregate function MAX(). Let us first create a table −

mysql> create table DemoTable
(
   Id int
);
Query OK, 0 rows affected (0.96 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(100);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values(988);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values(1000);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values(99);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values(999);
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+------+
| Id   |
+------+
| 100  |
| 988  |
| 1000 |
| 99   |
| 999  |
+------+
5 rows in set (0.00 sec)

Following is the query to get the maximum value of a column −

mysql> select max(Id) AS MaxId from DemoTable;

This will produce the following output −

+-------+
| MaxId |
+-------+
| 1000  |
+-------+
1 row in set (0.03 sec)

Updated on: 01-Oct-2019

110 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements