- 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
Retrieve MIN and MAX date in a single MySQL query from a column with date values
For this, you can use aggregate function MIN() and MAX(). Let us first create a table −
mysql> create table DemoTable(AdmissionDate date); Query OK, 0 rows affected (0.76 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('2019-01-21'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('2018-12-31'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('2016-11-01'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('2019-04-11'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2016-02-01'); Query OK, 1 row affected (0.28 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+---------------+ | AdmissionDate | +---------------+ | 2019-01-21 | | 2018-12-31 | | 2016-11-01 | | 2019-04-11 | | 2016-02-01 | +---------------+ 5 rows in set (0.00 sec)
Following is the query to retrieve MIN and MAX date in a single query −
mysql> select MIN(AdmissionDate) AS MinDate,MAX(AdmissionDate) AS MaxDate from DemoTable;
This will produce the following output −
+------------+------------+ | MinDate | MaxDate | +------------+------------+ | 2016-02-01 | 2019-04-11 | +------------+------------+ 1 row in set (0.00 sec)
Advertisements