
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to find last date from records with date values in MySQL?
To get the last date i.e. the latest, use aggregate function MAX() with a subquery. Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, ExpiryDate date ); Query OK, 0 rows affected (1.40 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(ExpiryDate) values('2018-12-31'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(ExpiryDate) values('2019-09-01'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(ExpiryDate) values('2019-09-01'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(ExpiryDate) values('2016-08-30'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(ExpiryDate) values('2019-06-23'); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+------------+ | Id | ExpiryDate | +----+------------+ | 1 | 2018-12-31 | | 2 | 2019-09-01 | | 3 | 2019-09-01 | | 4 | 2016-08-30 | | 5 | 2019-06-23 | +----+------------+ 5 rows in set (0.00 sec)
Following is the query to find records with the last date −
mysql> select *from DemoTable where ExpiryDate=(select max(ExpiryDate) from DemoTable);
This will produce the following output. From the inserted records, the last date is 2019-09-0. The same can be seen in the below output −
+----+------------+ | Id | ExpiryDate | +----+------------+ | 2 | 2019-09-01 | | 3 | 2019-09-01 | +----+------------+ 2 rows in set (0.00 sec)
- Related Articles
- How to compare the first date and the last date from a MySQL column with date records?
- MySQL queries to update date records with NULL values
- How to change the date in a table with date records with MySQL?
- MySQL query to fetch the latest date from a table with date records
- How to get the records of the last two days from the current date in MySQL?
- How to display first day and last day of the month from date records in MySQL?
- Comparison of varchar date records from the current date in MySQL
- Fastest way to search for a date from the date records in MySQL
- Find the difference between current date and the date records from a MySQL table
- Add 30 days to date in a MySQL table with arrival date records
- How to update column values with date records and set 1 for corresponding records before the current date in SQL
- Display distinct dates in MySQL from a column with date records
- MySQL query to select records with a particular date?
- Filter dates from a table with DATE and NULL records in MySQL
- MySQL time period query to fetch date records from interval of 14 weeks from current date?

Advertisements