
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Query MySQL database to echo highest auto incremented number?
Let us first create a table with Id as auto_increment −
mysql> create table DemoTable ( UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, UserName varchar(20) ); Query OK, 0 rows affected (0.56 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(UserName) values('John'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(UserName) values('Larry'); Query OK, 1 row affected (0.35 sec) mysql> insert into DemoTable(UserName) values('Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(UserName) values('Bob'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(UserName) values('Carol'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(UserName) values('David'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(UserName) values('Robert'); Query OK, 1 row affected (0.20 sec)
Following is the query to display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------+----------+ | UserId | UserName | +--------+----------+ | 1 | John | | 2 | Larry | | 3 | Chris | | 4 | Bob | | 5 | Carol | | 6 | David | | 7 | Robert | +--------+----------+ 7 rows in set (0.00 sec)
Here is the query to echo highest auto incremented number −
mysql> select *from DemoTable order by UserId desc limit 0,1;
This will produce the following output displaying the highest auto incremented value −
+--------+----------+ | UserId | UserName | +--------+----------+ | 7 | Robert | +--------+----------+ 1 row in set (0.00 sec)
- Related Articles
- Reserving MySQL auto-incremented IDs?
- How to insert data to MySQL having auto incremented primary key?
- How to Auto Generate Database Diagram in MySQL?
- How to set the initial value of an auto-incremented column in MySQL using JDBC?
- How to retrieve auto-incremented value generated by PreparedStatement using JDBC?
- How to retrieve auto-incremented value generated by Statement using JDBC?
- MySQL query to find the top two highest scores
- How to get the fourth highest value using MySQL query?
- How to add auto-increment to column in MySQL database using PhpMyAdmin?
- How to insert data into a table with auto-incremented columns using JDBC?
- What is the MySQL query to display the number of tables in a database?
- How to change auto increment number in MySQL?
- Apply MySQL query to each table in a database?
- MySQL query to get first two highest column values from a table?
- MySQL database field type for search query?

Advertisements