SQL SELECT LAST
The LAST() function in Structured Query Language shows the last value from the specified column of the table.
Note: This SQL function is only supported in Microsoft Access database. Oracle supports ORDER BY and ROWNUM keywords, and MySQL supports the LIMIT keyword for selecting the last record.
Syntax of LAST() Function
In the above syntax, the LAST keyword denotes the last row to be shown from the table in the output, and the Field_Name denotes the column whose value we want to show.
Example of the LAST function in SQL
Firstly, we have to create a table and insert the data into the table in SQL.
The following SQL statement creates the Student_Details table with Student_ID as the primary key:
The following SQL queries insert the record of students into the above table using INSERT INTO statement:
Let's see the record of the above table using the following SELECT statement:
The following query shows the last Student_Name from the above table in the output:
Syntax of LIMIT Clause in MySQL
In this MySQL syntax, we have to specify the value 1 just after the LIMIT keyword for indicating the single row/record.
Example of LIMIT Clause in MySQL
Let's take the following Employee table to explain how to use the LIMIT clause in MySQL for accessing the last record:
The following MySQL query shows the last value of the Emp_City column from the above Employee table:
ROWNUM keyword in Oracle
The syntax for accessing the last record from the Oracle database is given below:
In this Oracle syntax, we have to specify the ROWNUM keyword, which is less than and equal to 1. In Oracle, the ROWNUM keyword is used in the WHERE clause for retrieving the last record from the table.
Example of ROWNUM Clause in Oracle
Let's take the following Cars table to explain how to use the ROWNUM keyword in MySQL:
The following MySQL query shows the last name of the car from the Car_Name column of the Cars table:
Next TopicSQL SELECT RANDOM