
- 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
How can I select the row with the highest ID in MySQL?
You can select the row with highest ID in MySQL with the help of ORDER BY with LIMIT OFFSET
The syntax is as follows −
select *from yourTableName order by yourColumnName desc limit 1 offset 0;
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table HighestIdOrderBy −> ( −> EmployeeId int, −> EmployeeName varchar(200) −> ); Query OK, 0 rows affected (0.58 sec)
Insert records in the table with the help of insert command. The query is as follows −
mysql> insert into HighestIdOrderBy values(200,'David'); Query OK, 1 row affected (0.20 sec) mysql> insert into HighestIdOrderBy values(1000,'Bob'); Query OK, 1 row affected (0.18 sec) mysql> insert into HighestIdOrderBy values(600,'John'); Query OK, 1 row affected (0.16 sec) mysql> insert into HighestIdOrderBy values(300,'Johnson'); Query OK, 1 row affected (0.18 sec) mysql> insert into HighestIdOrderBy values(100,'Carol'); Query OK, 1 row affected (0.12 sec)
Display all records from the table with select statement. The query is as follows −
mysql> select *from HighestIdOrderBy;
The following is the output −
+------------+--------------+ | EmployeeId | EmployeeName | +------------+--------------+ | 200 | David | | 1000 | Bob | | 600 | John | | 300 | Johnson | | 100 | Carol | +------------+--------------+ 5 rows in set (0.00 sec)
Here is the query to select maximum id from MySQL table. The query is as follows −
mysql> select *from HighestIdOrderBy order by EmployeeId desc limit 1 offset 0;
The following is the output −
+------------+--------------+ | EmployeeId | EmployeeName | +------------+--------------+ | 1000 | Bob | +------------+--------------+ 1 row in set (0.00 sec)
- Related Articles
- Only display row with highest ID in MySQL
- Can we update a row with the highest ID in a single MySQL query?
- How to select all the records except a row with certain id from a MySQL table?
- MySQL select * with distinct id?
- Select highest salary in MySQL?
- Delete one row and reorder the others with the correct ID in MySQL?
- MySQL - SELECT … WHERE id IN (..) order with particular column?
- How to select everything before @ in an email-id with in MySQL?
- Select nth highest value in MySQL
- How can I select an element by its ID attribute using jQuery?
- How to select the sum of the column values with higher value in reach row with MySQL?
- How to select last row in MySQL?
- How to select ID column as null in MySQL?
- How to get max(id) of row data in MySQL?
- How to select next row pagination in MySQL?

Advertisements