MySQL LAST
To get the last value of a column, the MySQL LAST function is used.
Syntax:
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:
SELECT name FROM students ORDER BY id DESC LIMIT 1;
Output:
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:
SELECT name FROM students ORDER BY name DESC LIMIT 1;
Output:
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.