SQL SELECT LAST

The LAST() function returns the last value of the selected column.

Syntax SQL LAST()

SELECT LAST(column_name) FROM table_name;

You should note that the last() function is only supported in MS Access. But there are ways to get the last record in MySql, SQL Server, Oracle etc. databases.

Syntax of SQL Server


SELECT TOP 1 column_name FROM table_name

ORDER BY column_name DESC;

Example:
SELECT TOP 1 CustomerName FROM Customers

ORDER BY CustomerID DESC;

Syntax of MySQL

SELECT column_name FROM table_name

ORDER BY column_name DESC

LIMIT 1;
Example
SELECT CustomerName FROM Customers

ORDER BY CustomerID DESC

LIMIT 1;

Syntax of Oracle

SELECT column_name FROM table_name

ORDER BY column_name DESC

WHERE ROWNUM <=1;
Example
SELECT CustomerName FROM Customers

ORDER BY CustomerID DESC

WHERE ROWNUM <=1;

Let us take the example of CUSTOMERS to examine SQL SELECT LAST command:

Table CUSTOMERS

Customer_Name Age Address Expenditure
Mithilesh Singh 25 Noida 5000
Harish Sharma 34 50000 ghaziabad

If you want to retrieve the last value of the "customer_name" column from the "customers" table, you need to write following query:


SELECT LAST (CUSTOMER_NAME) AS LAST_CUSTOMER FROM CUSTOMERS;   

After that query, you will find the result:   

Harish Sharma