WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

SQL LAST() Function


LAST () function

LAST () function returns the value of the last record in the specified column.

SQL LAST () syntax

SELECT LAST (column_name) FROM table_name;

Note: Only MS Access supports the LAST () function.


SQL LAST () workspace in SQL Server, MySQL, and Oracle

SQL Server syntax

SELECT TOP 1 column_name FROM table_name
ORDER BY column_name DESC;

Example

SELECT TOP 1 name FROM Websites
ORDER BY id DESC;

MySQL syntax

SELECT column_name FROM table_name
ORDER BY column_name DESC
LIMIT 1;

Example

SELECT name FROM Websites
ORDER BY id DESC
LIMIT 1;

Oracle syntax

SELECT column_name FROM table_name
ORDER BY column_name DESC
WHERE ROWNUM <=1;

Example

SELECT name FROM Websites
ORDER BY id DESC
WHERE ROWNUM <=1;


Demo database

In this tutorial, we will use the welookups sample database.

Here is the data from the "Websites" table:

+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1  | Google       | https://www.google.cm/  | 1     | USA     |
| 2  | Indiatimes          | https://www.Indiatimes.com/ | 13    | IN      |
| 3  | zomato      | http://www.zomato.com/  | 4689  | IN      |
| 4  | indiamart          | http://indiamart.com/       | 20    | IN      |
| 5  | Facebook     | https://www.facebook.com/| 3     | USA     |
|  6 | Yahoo         | https://www.Yahoo.com/  |     4 | IN      |
|  7 | stackoverflow | http://stackoverflow.com/|     0 | IND     |
+----+---------------+---------------------------+-------+---------+

SQL LAST () example

The following SQL statement selects the value of the last record in the "name" column of the "Websites" table:

Example

SELECT name FROM Websites
ORDER BY id DESC
LIMIT 1;


Welookups is optimized for learning.Copy right 2018 kumar aditya singh .
All Right Reserved and you agree to have read and accepted our term and condition.
All Rights Reserved.

welookups.com