How to Query Between Two Dates in MySQL?
Last Updated :
12 Apr, 2024
MySql is a popular open-source relational database management system (RDBMS) that is uniquely used to construct expandable and high-productivity databases. MySQL, which was created by MySQL AB and later acquired by its current owner Oracle Corporation, was originally introduced in 1995.
MySQL is reputed for its sturdy and quick functioning attributes which involve easy-to-handle features and dependability. MySQL can normally be seen together with dynamic web applications and is generally used to serve languages such as PHP but also other server-side programming languages like Python. In this article, you will discover how to do a query between two dates in MySQL including some examples
MySQL Query Between Two Dates
Similarly to MySQL(relational database management system), the DATE data type is used to store the data. The data storage format required for MySQL database is 'YYYY-MM-DD'. The range of dates is '1000-01-01' to '9999-12-31'. Many times it happens that we want to get the data between the two dates whether it may be financial analysis, user tracking, inventory management, etc. To query between two dates we use the BETWEEN keyword to specify the range. We can perform certain operations between two dates such as SELECT, UPDATE, DELETE, etc.
Syntax:
SELECT * FROM table_name
WHERE date BETWEEN 'YYYY-MM-DD' AND 'YYYY-MM-DD';
- SELECT * FROM table_name: Retrieves all columns from the specified table.
- WHERE date BETWEEN 'YYYY-MM-DD' AND 'YYYY-MM-DD': Filters the rows based on a date range, selecting rows where the date column falls between the specified start and end dates inclusively. Replace 'YYYY-MM-DD' with the desired start and end dates.
Setting up Environment
Here we will an example of an EMPLOYEE table having EMP_ID, NAME, SALARY, and JOIN_DATE as columns and we are going to perform some operations on the table.
CREATE TABLE EMPLOYEE (EMP_ID INT Primary key,
NAME VARCHAR(20),
SALARY INT,
JOIN_DATE DATE);
Insert the data on it:
INSERT INTO EMPLOYEE values
(1, 'Sahil', 15000, '2023-12-01'),
(2, 'Alen', 13000, '2023-12-08'),
(3, 'John', 14000, '2023-12-15'),
(4, 'Alex', 13000, '2023-12-22'),
(5, 'Mathew', 14000, '2023-12-31'),
(6, 'Sia', 15000, '2024-01-01'),
(7, 'David', 16000, '2024-01-10'),
(8, 'Tim', 14000, '2024-01-25'),
(9, 'Leo', 15000, '2024-02-01'),
(10, 'Tom', 16000, '2024-02-05');
EMPLOYEE table:
EMPLOYEE TableExample of How to query between two dates in MySQL
Example 1: Get the data of employee between two dates
Let's take an example to print the data of the employees joined between two dates
Syntax:
SELECT * FROM table_name
WHERE date BETWEEN 'YYYY-MM-DD' AND 'YYYY-MM-DD';
Let's write a query where we want to print the data of the employee whose joining date is between '2023-12-01' AND '2024-01-01'
Query:
SELECT * from EMPLOYEE
WHERE JOIN_DATE BETWEEN '2023-12-01' AND '2024-01-01';
Output:
Records between the datesExplanation: The above example prints all the records of the employees whose JOIN_DATE is between '2023-12-01' AND '2024-01-01'. There are 6 employees whose JOIN_DATE is between '2023-12-01' AND '2024-01-01'. The ' * ' is used to print all the records and the BETWEEN keyword is used to specify the range of dates.
Example 2: Update the records between two dates
Here we will take an example to update the data between two dates.
Syntax:
UPDATE table_name
SET column1= value1, column2= value2, . . . . , column_n=value_n
WHERE date BETWEEN 'YYYY-MM-DD' AND 'YYYY-MM-DD';
We will write a query to UPDATE the SALARY of the employees to 15000 whose JOIN_DATE is BETWEEN '2023-12-15' AND '2024-01-25'.
Query:
UPDATE EMPLOYEE
SET SALARY=15000
WHERE JOIN_DATE BETWEEN '2023-12-15' AND '2024-01-25';
Output:
Updated tableExplanation: The example above updates our records of employees whose dates of joining are between ‘2023-12-15’ and ‘2024-01-25’. The number of employees with a JOIN_DATE within the range of ‘2023-12-15’ and ‘2024-01-25’ is 6. To update the table, we use the keywords UPDATE, SET to set a new value in the record, and BETWEEN to indicate the period.
Example 3: Deleting the records between two dates
Let's take an example of deleting the records between two dates
Syntax:
DELETE from table_name
WHERE date BETWEEN YYYY-MM-DD' AND 'YYYY-MM-DD';
Let's write a query to DELETE the records of the employee whose JOIN_DATE is BETWEEN '2023-12-31' AND '2024-01-01'.
Query:
DELETE from EMPLOYEE
WHERE JOIN_DATE BETWEEN '2023-12-31' AND '2024-01-01';
Output:
Fig 4. Table after deletionExplanation: In this example, we delete those records for employees whose JOIN_DATE is between '2023-12-31' AND '2024-01-01'. We can see that two have JOIN_DATE values within this criteria. Finally, we use DELETE which is a keyword, for deleting or removing things from tables, and BETWEEN which shows that something is included in a certain range.
Conclusion
In conclusion, MySQL provides a feature to query between two dates. It is achieved by applying the keyword BETWEEN and we indicate the date range within the dates specified. The MySQL convention for the format of date is 'YYYY-MM-DD' which allows the date to be specified in the range of '1000-01-01 to 9999-12-31'. Query between two dates is quite common when it comes to user tracking, financial analysis, inventory management, etc. We can perform various operations such as SELECT, UPDATE, and DELETE while writing a query between two dates.
Similar Reads
How to Get all Dates Between Two Dates in SQL
Navigating and manipulating date ranges is a common task in SQL, especially when we need to generate lists of dates between two specified dates. This article covers two methods using the SQL GROUP BY clause: the traditional method with explicit column names and the shorter "GROUP BY 1" syntax. Weâll
4 min read
How to Select Dates Between Two Dates in PostgreSQL?
When managing a PostgreSQL database, we may often encounter scenarios where we need to filter data based on date ranges. This task is crucial for generating reports, analyzing trends, or retrieving time-sensitive information. However, querying for data within a specific date range can be a challengi
4 min read
How to Select Dates Between Two Date Range in PL/SQL
In database management, PL/SQL (Procedural Language/Structured Query Language) stands out as a powerful tool for managing data within Oracle databases. When dealing with date-related queries, particularly selecting dates between two date ranges, PL/SQL offers robust functionality to streamline the p
4 min read
How to Convert Timestamp to Datetime in MySQL?
In this article, we are going to learn how to convert Timestamp to Datetime in MySQL.To execute these queries, we need to first add integer data(written in timestamp format) and then use the FROM_UNIXTIME() function to convert it into "Datetime" Data Type. FROM_UNIXTIME(): This function in MySQL ret
2 min read
How to Select Data Between Two Dates and Times in SQL Server?
In SQL, some transactions need to be extracted based on their completion times and dates. Here, the DATETIME2 data type in SQL to perform such operations. For this article, we will be using the Microsoft SQL Server as our database. Note: Here, we will use the two dates and times given in the query a
2 min read
How to get Difference between two Dates in days using jQuery ?
In this article, we will learn how to find the difference between the 2 dates in days using jQuery. Here, we will take the input for the dates using a jquery datepicker.Considering the given below 2 input Dates, we need to calculate the difference between them which will be returned in a number of d
3 min read
How to Convert Epoch Time to Date in SQL?
To convert Epoch Time to Date in SQL use the DATEADD() function. DATEADD() function in SQL Server is used to add a time or date interval to a specified date and return the modified date. Some features of the SQL DATEADD() function are: This function is used to sum up a time or a date interval to a d
2 min read
How To Get Current Date in Power Query?
Power query is a analysis tool in excel.From 2016 version onwards it was inbuilted in the Excel . Power Query helps us to perform all type of tasks regarding to transformation. Â In this article is we will see how to get current date in power query . Getting Current Date in Power QueryTo get the curr
2 min read
How to Calculate the Number of Days between Two Dates in JavaScript?
Calculating the number of days between two dates is a common task in web development, especially when working with schedules, deadlines, or event planning. JavaScript provides a simple and effective way to perform this calculation by using different approaches. Whether you're comparing past and futu
4 min read
How to Strip Time Component From Datetime in MySQL?
MySQL is a very popular open-source and free database server that is ideal for small and large applications and is a relational database management system where SQL (Structured Query Language) queries or statements are used to manage and manipulate the data. MySQL provides many built-in functions li
4 min read