CodesJava

Easy learning with example program codes

LAST function in MySQL

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
1 row in set <0.00 sec>

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
1 row in set <0.00 sec>

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.

Please follow and like us:
error









Copyright © 2019 CodesJava DMCA.com Protection Status