
- 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
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 Articles
- 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?
- 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
- Display first selected row in 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?
- Display highest amount from corresponding duplicate ids in MySQL
- Only display specified values inside the IN clause with MySQL?
- 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?
- Return only a single row from duplicate rows with MySQL
- How to get max(id) of row data in MySQL?

Advertisements