LAST function in MySQL

MySQL LAST
To get the last value of a column, the MySQL LAST function is used.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT column,
FROM table_name
ORDER BY column_name DESC
LIMIT 1;
SELECT column, FROM table_name ORDER BY column_name DESC LIMIT 1;
SELECT column, 
FROM table_name  
ORDER BY column_name DESC  
LIMIT 1;

 

Example 1: Students Table:

ID NAME AGE
1 Joy 10
2 Smiley 13
3 Happy 11
4 James 13
5 Bond 10

 

Query:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT name
FROM students
ORDER BY id DESC
LIMIT 1;
SELECT name FROM students ORDER BY id DESC LIMIT 1;
SELECT name
FROM students
ORDER BY id DESC
LIMIT 1;

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
NAME
Bond
NAME Bond
NAME
Bond

Explanation:

The ‘students’ is an already existing table. Here we are retrieving the LAST record of the ‘name’ column ordering by the ‘id’ of the ‘students’ table.

Example 2: Students Table:

ID NAME AGE
1 Joy 10
2 Smiley 13
3 Happy 11
4 James 13
5 Bond 10

 

Query:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT name
FROM students
ORDER BY name DESC
LIMIT 1;
SELECT name FROM students ORDER BY name DESC LIMIT 1;
SELECT name
FROM students
ORDER BY name DESC
LIMIT 1;

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
NAME
Smiley
NAME Smiley
NAME
Smiley

Explanation:
The ‘students’ is an already existing table. Here we are retrieving the LAST record of the ‘name’ column ordering by the ‘name’ of the ‘students’ table.