SQL LAST()Funciones


La función LAST()

La función LAST() devuelve el valor del último registro en la columna especificada.

Sintaxis de SQL LAST()

SELECT LAST(column_name) FROM table_name;

Nota:Solo MS Access admite la función LAST().


Solución alternativa de SQL LAST() en SQL Server, MySQL y Oracle

Sintaxis de SQL Server

SELECT TOP 1 column_name FROM table_name
ORDER BY column_name DESC;

Ejemplo

SELECT TOP 1 name FROM Websites
ORDER BY id DESC;

Sintaxis de MySQL

SELECT column_name FROM table_name
ORDER BY column_name DESC
LIMIT 1;

Ejemplo

SELECT name FROM Websites
ORDER BY id DESC
LIMIT 1;

Sintaxis de Oracle

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

Ejemplo

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


Base de datos de demostración

En este tutorial, usaremos la base de datos de muestra EJEMPLO.

A continuación se muestran los datos de la tabla "Websites":

+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1  | Google       | https://www.google.cm/    | 1     | USA     |
| 2  | 淘宝          | https://www.taobao.com/   | 13    | CN      |
| 3  | Ejemplo      | http://www.example.com/    | 4689  | CN      |
| 4  | 微博          | http://weibo.com/         | 20    | CN      |
| 5  | Facebook     | https://www.facebook.com/ | 3     | USA     |
|  6 | 百度         | https://www.baidu.com/    |     4 | CN      |
|  7 | stackoverflow | http://stackoverflow.com/ |     0 | IND     |
+----+---------------+---------------------------+-------+---------+

Ejemplo de SQL LAST()

La siguiente instrucción SQL selecciona el valor del último registro en la columna "name" de la tabla "Websites":

Ejemplo

SELECT name FROM Websites
ORDER BY id DESC
LIMIT 1;

Al ejecutar el SQL anterior, el resultado es el siguiente:

Otras extensiones