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.

How to Query Between Two Dates in MySQL?

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

Similar Reads

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....

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....

Example of How to query between two dates in MySQL

Example 1: Get the data of employee between two dates...

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....