Mysql Date in between

The Tutorial illustrates an example of a MySQL date in between query. This query will help you in getting the data in a specific date range.

Mysql Date in between

MySQL Date in between - How to get records between given dates?

The Tutorial illustrates an example MySQL date in between SQL. In this Tutorial we illustrate an example that help you to get the date in between given dates. The between clause can be used with int, float and date data types. Here in this example we are going to see how to use the between clause of SQL to get the records between two dates. The Tutorial illustrates an example of using between clause with the MySQL date date types.

We have provided sql code for creating table, adding data and use of between clause. This tutorial will help beginners in understanding and mastering the date between clause for proper retrieval of data from database.

We have recorded the video instruction for getting the records between two dates using the between clause. You can learn by playing following video tutorial:

Query for creating table:-

The create table statement create a table employees1 with field name and data type respectively. Here is the sql query that you can use to create table in your database for running sql statements described in this tutorial:


CREATE TABLE employee1(
   Empid int(10),
   Empname varchar(60),
   date date );

Open MySQL terminal and create employee1 table in your database.

Output:-

Query for inserting data:-

The insert into add the records or rows value into the table 'employee1'.

mysql> insert into employee1 values('2','Komal','2008-12-23');
Query OK, 1 row affected (0.03 sec)

mysql> insert into employee1 values('2','Sandeep','2008-12-24');
Query OK, 1 row affected (0.02 sec)

mysql> insert into employee1 values('2','suman','2008-12-25');
Query OK, 1 row affected (0.03 sec)

More Insert query:


insert into employee1 values('4','John','2024-12-23');
insert into employee1 values('5','Mohan','2023-12-23');

You should add more more request by executing above query.

Query for Viewing data:-

The select return and display  the records from the table'employee1'.

mysql> select * from employee1;

Output:-

+-------+---------+------------+
| Empid | Empname | date       |
+-------+---------+------------+
|     1 | Girish  | 2008-12-20 |
|     2 | Komal   | 2008-12-21 |
|     3 | vineet  | 2008-12-21 |
|     4 | Amit    | 2008-12-20 |
|     2 | Komal   | 2008-12-23 |
|     2 | Sandeep | 2008-12-24 |
|     2 | suman   | 2008-12-25 |
+-------+---------+------------+
7 rows in set (0.00 sec)

Query for extracting record using date-in- between:-

The given below Query return you the Empid,Empname from table employee 1 where the value of date lies between  '2008-12-23'and '2008-12-25'.

mysql> select Empid,Empname from employee1 where date between '2008-12-23'and '2008-12-25';

Output:-

+-------+---------+
| Empid | Empname |
+-------+---------+
|     2 | Komal   |
|     2 | Sandeep |
|     2 | suman   |
+-------+---------+
3 rows in set (0.03 sec)

SQL Query to get data between 2023 and 2025:


select * from employee1 where date between '2001-12-23'and '2025-12-31';

Here is the screenshot of the output of the program:

MySQL Select between dates example

In this tutorial we have used between clause with date type data and understood the correct use of this in querying the database.

Related Tutorial: