Select Last in SQL
SELECT LAST in SQL
Important: There is no standard SQL SELECT LAST function or keyword to directly get the "last" row.
How to get the "last" record in SQL?
You usually define what "last" means by sorting with ORDER BY and then retrieving the top record in reverse order.
Common Approaches to Get the Last Row
1. Using ORDER BY with LIMIT or TOP
MySQL/PostgreSQL/SQLite:
SELECT * FROM table_nameORDER BY some_column DESCLIMIT 1;SQL Server:
SELECT TOP 1 * FROM table_nameORDER BY some_column DESC;Oracle (12c+):
SELECT * FROM table_nameORDER BY some_column DESCFETCH FIRST 1 ROW ONLY;Example: Get last employee hired (based on hire_date):
SELECT * FROM employeesORDER BY hire_date DESCLIMIT 1;Summary:
SQL has no built-in
SELECT LAST.Use
ORDER BY column DESCplusLIMIT 1(or equivalent) to get the "last" row.Define the column that determines order (date, ID, etc.).
Want help writing a query to get the last record from your table?