This article will be looking into how to SELECT the last record of a MySQL table. We will be going through a few examples to demonstrate the concept.
Table of Contents
Let us get started by making the sample data to be used across the examples. Create a table named sales_team_emails, followed by inserting some rows into it.
# create the table sales_team_emails CREATE TABLE sales_team_emails ( sales_person_id INT AUTO_INCREMENT, sales_person_name VARCHAR(255), sales_person_email VARCHAR(255), PRIMARY KEY (sales_person_id) ); # Insert rows to table sales_team_emails INSERT INTO sales_team_emails (sales_person_name,sales_person_email) VALUES("Aditi","[email protected]"), ("Furan T","[email protected]"), ("Veronica Hedge","[email protected]"), ("Atharv","[email protected]"), ("Erick","[email protected]"), ("Rasmus","[email protected]"), ("Aditi Sharma","[email protected]"), ("Furan T","[email protected]"), ("Veronica Longman","[email protected]"), ("Simon Rappid","[email protected]");
To view the snapshot of the table sales_team_emails, execute:
SELECT * FROM sales_team_emails;

The output in image_1 shows that sales_person_id =10 is the last record in the table.
MySQL select last record of table using LIMIT
Here we will be using the LIMIT clause. LIMIT clause is used when we want to restrict the result set to a certain number of rows. To get the last record of the table sales_team_emails, observe the below query.
SELECT * FROM sales_team_emails ORDER BY sales_person_id DESC LIMIT 1;
Action Output Message:-
Frequently Asked:
1 row(s) returned.
Output:-

Explanation:-
As we can see in the output image_2, only a single record is retrieved. Here we are arranging the records of the table sales_team_emails into descending order based on sales_person_id using the ORDER BY clause so that the record with the highest sales_person_id will be the first record to retrieve. Finally, we are retrieving only one row in the result set using the LIMIT clause to restrict the number of rows to 1.
MySQL select last record of table using MAX() function
Here we will be using the MAX() function of the MySQL clause. MAX(columnName) function in MySQL will return the highest value of a column.To get the last record of the table sales_team_emails, observe the below query.
SELECT * FROM sales_team_emails WHERE sales_person_id=(SELECT MAX(sales_person_id) FROM sales_team_emails);
Action Output Message:-
1 row(s) returned.
Output:-

Explanation:-
The output is the same: the last record of the table sales_team_emails with sales_person_id =10 is returned. Here we are using a subquery to get the maximum of the sales_person_id from the table sales_team_emails using the max(sales_person_id) function. Finally, we select all the details from the sales_team_emails table for the sales_person_id retrieved from the subquery.
READ MORE
- MySQL Select last N rows
- MySQL select rows by range
- MySQL select TOP 10 rows
- MySQL select row with max value
We hope this article helped you with getting the last record of a table in MySQL. Good Luck!!