- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL query to fetch the latest date from a table with date records
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)
Advertisements
To Continue Learning Please Login
Login with Google