MySQL query to fetch the latest date from a table with date records

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable
(
   DueDate date
);
Query OK, 0 rows affected (0.56 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('2018-10-01');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('2016-12-31');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('2019-07-02');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values('2015-01-12');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values('2019-04-26');
Query OK, 1 row affected (0.13 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+------------+
| DueDate    |
+------------+
| 2018-10-01 |
| 2016-12-31 |
| 2019-07-02 |
| 2015-01-12 |
| 2019-04-26 |
+------------+
5 rows in set (0.00 sec)

Let’s say the current date is 2019-08-15. Now we will fetch the latest date −

mysql> select *from DemoTable order by DueDate DESC limit 1;

This will produce the following output −

+------------+
| DueDate    |
+------------+
| 2019-07-02 |
+------------+
1 row in set (0.03 sec)
raja
Published on 25-Sep-2019 10:42:05
Advertisements