
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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)
- Related Questions & Answers
- Fetch a single ordered date from a column with MySQL LIMIT
- MySQL query to retrieve current date from a list of dates
- Select query to display duplicate values with max date
- Concatenate date and time from separate columns into a single column in MySQL
- How to compare the first date and the last date from a MySQL column with date records?
- MySQL query to make a date column NULL?
- MySQL query to fetch the latest date from a table with date records
- Display record with today and tomorrow’s date from a column with date record in MySQL
- Find date record after a particular date from a column with VARCHAR type in MySQL
- Sort array based on min and max date in JavaScript?
- How to add a year and two days to a date with a single MySQL query?
- Display month names and year from a column with date records with MySQL
- Display distinct dates in MySQL from a column with date records
- A single MySQL query to search multiple words from different column values
- MySQL date column auto fill with current date?
Advertisements