Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (55.6k points)

Can anyone tell me how to get last record in SQL?

2 Answers

0 votes
by (119k points)

In SQL Server, you can sort the table by descending order using ORDER BY and use TOP 1 to get the top row from the sorted table. Here is the syntax to get the last record in SQL server:

SELECT TOP 1 ColumnName FROM TableName  

ORDER BY ColumnName DESC;  

In MySQL, you can sort the table by descending order using ORDER BY and use LIMIT 1 keyword to get the top row from the sorted table. Here is the syntax to get the last record in SQL server

SELECT ColumnName FROM TableName  

ORDER BY ColumnName DESC  

LIMIT 1;  

If you are interested in SQL, then check out this SQL training course by Intellipaat that provides instructor-led training, self-paced videos, and hands-on experience.

0 votes
by (1.5k points)

In SQL, to get the last record, we use an ORDER BY clause with DESC (descending order) and LIMIT 1 for SQL databases like MySQL, PostgreSQL, or TOP 1 for SQL Server:

SELECT * FROM table_name

ORDER BY column_name DESC

LIMIT 1;

LIMIT 1: Limits the result set to one row after sorting, thus returning only the last record in descending order.

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...