

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Only display row with highest ID in MySQL
To order, use the ORDER BY DESC clause. With that, since we want a single ID, which should be the highest, use LIMIT 1. This will fetch the row with highest ID. Let us first create a table −
mysql> create table DemoTable ( Id int, FirstName varchar(50) ); Query OK, 0 rows affected (0.83 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(100,'Chris'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(110,'Robert'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(120,'Mike'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(115,'Bob'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------+-----------+ | Id | FirstName | +------+-----------+ | 100 | Chris | | 110 | Robert | | 120 | Mike | | 115 | Bob | +------+-----------+ 4 rows in set (0.00 sec)
Following is the query to display row with highest ID −
mysql> select *from DemoTable order by Id DESC LIMIT 1;
This will produce the following output −
+------+-----------+ | Id | FirstName | +------+-----------+ | 120 | Mike | +------+-----------+ 1 row in set (0.00 sec)
- Related Questions & Answers
- How can I select the row with the highest ID in MySQL?
- Can we update a row with the highest ID in a single MySQL query?
- Insert row with only default values in MySQL
- Querying only the field name and display only the id in MongoDB?
- Display highest amount from corresponding duplicate ids in MySQL
- Display first selected row in MySQL?
- Get MAX and MIN values along with their row id in MySQL?
- Finding average marks of students for different subjects and display only the highest average marks in MySQL
- Return only a single row from duplicate rows with MySQL
- Only display specified values inside the IN clause with MySQL?
- MySql how to display the records with latest ID in a table?
- Delete one row and reorder the others with the correct ID in MySQL?
- MySQL query to get the highest value from a single row with multiple columns
- MySQL query to merge rows if Id is the same and display the highest corresponding value from other columns
- Display all products having the highest amount in a MySQL table with product details?
Advertisements