MySQL MariaDB

MySQL WHERE DATE Greater Than

While working with databases in MySQL, it is a common task to search for specific data based on the value of DATE.  To do this, use the Greater Than operator in the “WHERE” clause. This helps us to compare the DATE values in a column with a specific date value.

This post will teach how to filter records where the DATE value matches a “WHERE” clause condition having the Greater Than operator.

Comparing Dates Using the Greater Than Operator

To compare the DATE value, use the comparison operators, and if the value is Greater Than the specified value, the output will return “1”, otherwise, it will return “0”.

Let’s run a query having two DATE values with the format “YYYY-MM-DD”:

SELECT '2023-01-30' > '2023-01-01';

The output displays the value “1” as the condition is fulfilled, meaning that the value at the left-side of the operator is greater than the one on the right side:

The user can compare the DATE with the output of built-in functions, such as “CURDATE()”, run this query:

SELECT '2023-01-30' > CURDATE();

The output displays “0”, which means the specified date is not greater than the output of “CURDATE()”:

Fetch Records With “Where DATE Greater Than” Condition

Run this query to filter the records of “user” table data only when the value of “time” is Greater Than the “2022-11-18”:

SELECT * FROM USER WHERE TIME > '2022-11-18';

Let’s break down the query mentioned above:

  • The “SELECT” statement filters the data from the table
  • The “*” represents “Select ALL” columns
  • The “WHERE” clause specifies a condition for returning the output
  • The “>” operator checks if the value of the left side is Greater Than value on the right side

After the successful execution of the query, the user will get the records that match a specified condition:

The user can use the “DATE()” function, to ensure that the column value of the table gets properly formatted as “YYYY-MM-DD” before comparison, run this query:

SELECT * FROM USER WHERE DATE(TIME) > '2022-11-18';

The output displays the records that fulfill the condition specified in the “WHERE” clause:

The user can use the “DATE()” function to format both operands before the comparison even in a string format(YYYY-MM-DD):

SELECT * FROM USER WHERE DATE(TIME) > DATE('2023-02-21');

The output displays the record that matches a specified condition:

Fetch Records With “Where DATE/Time Greater Than” Condition

The table “user” has the value of DATE and Time in the format “YYYY-MM-DD hh:mm:ss” so you can also specify the Time in the operand for comparing both DATE and Time using the Greater than operator. Run this query:

SELECT * FROM USER WHERE DATE(TIME) > '2023-02-21 12:49:35';

The output will display the records that fulfill the specified condition.

You have learned how to filter records where the DATE value matches a “WHERE” clause condition having the Greater Than operator.

Conclusion

The comparison Greater Than operator in the “WHERE” clause compares a column having DATE values in the format “YYYY-MM-DD” with a specified DATE with the same format. The “DATE()” function can be used to ensure that one or both operands are formatted explicitly. This post demonstrated the usage of the MySQL WHERE DATE Greater Than query.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.