EN
MySQL - select last row
0 points
In this article, we would like to show you how to select the last row from a table in MySQL.
Quick solution:
xxxxxxxxxx
1
SELECT * FROM `table_name`
2
ORDER BY `column_name` DESC
3
LIMIT 1;
To show how to select the last row from a table, we will use the following table:

Note:
At the end of this article you can find database preparation SQL queries.
In this example, we will select all the information (columns) about the last user from the users
table.
Query:
xxxxxxxxxx
1
SELECT * FROM `users`
2
ORDER BY `id` DESC
3
LIMIT 1;
Output:

In this example, we will select id
, name
and country
column information about the last user from the users
table.
Query:
xxxxxxxxxx
1
SELECT `id`, `name`, `country`
2
FROM `users`
3
ORDER BY `id` DESC
4
LIMIT 1;
Output:

create_tables.sql
file:
xxxxxxxxxx
1
CREATE TABLE `users` (
2
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
3
`name` VARCHAR(100) NOT NULL,
4
`email` VARCHAR(100) NOT NULL,
5
`country` VARCHAR(15) NOT NULL,
6
PRIMARY KEY (`id`)
7
)
8
ENGINE=InnoDB;
insert_data.sql
file:
xxxxxxxxxx
1
INSERT INTO `users`
2
(`name`, `email`, `country`)
3
VALUES
4
('Tom', 'tom@email.com', 'Poland'),
5
('Chris', 'chris@email.com', 'Spain'),
6
('Jack', 'jack@email.com', 'Spain'),
7
('Kim', 'kim@email.com', 'Vietnam'),
8
('Marco', 'marco@email.com', 'Italy'),
9
('Kate', 'kate@email.com', 'Spain'),
10
('Nam', 'nam@email.com', 'Vietnam');