How to get max(id) of row data in MySQL?



To get max(id), use MAX() method in MySQL. Following is the syntax −

select MAX(yourColumnName) AS anyAliasName from yourTableName;

Let us first create a table −

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

Insert some records in the table using insert command −

mysql> insert into DemoTable710 values(1001); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable710 values(2001); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable710 values(1998); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable710 values(1789); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable710 values(1678); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable710 values(9087); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable710 values(1908); Query OK, 1 row affected (0.17 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable710;

This will produce the following output -

+------+ | Id | +------+ | 1001 | | 2001 | | 1998 | | 1789 | | 1678 | | 9087 | | 1908 | +------+ 7 rows in set (0.00 sec)

Following is the query to get max(id) of row data in MySQL −

mysql> select MAX(Id) AS Max_Id from DemoTable710;

This will produce the following output -

+--------+ | Max_Id | +--------+ | 9087 | +--------+ 1 row in set (0.00 sec)
Updated on: 2019-08-21T12:16:55+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements