×

How to compare date in SQL

In this section, we will learn about how dates can be compared in SQL.

  • We can compare any random date with another date stored in a column of a table.
  • This comparison can be made with the help of comparison operators such >, <,   >=, >=, =.
  • The date () function is also used in SQL to compare two different dates.
  • The data type DATE allows storing the dates in SQL tables in ‘YYYY-MM-DD’ format. But while writing the query to compare the dates, the date to be written in the query can be in a relaxed string format.
  • According to the relaxed string format, different parts of the date can be separated using any character in between. MySQL also allows a date to be written in a query without any separator, provided the string written as a date form a sensible date.

Example 1:

Write a query to find all the employees whose joining date is greater than or the same as 5th May 1999.

Create a database with the name “dbemployee” with a table ‘employee’ created into it. We will consider this table and database for all the following examples.

mysql> CREATE DATABASE dbemployee;
Query OK, 1 row affected (0.00 sec)
mysql> USE dbemployee;
Database changed
mysql> CREATE TABLE employee (Emp_Id INT NOT NULL, Emp_Name VARCHAR (20), Emp_Dept VARCHAR (20), Emp_Salary INT, Emp_Joining_Date DATE);
Query OK, 0 rows affected (0.09 sec)
How to compare date in SQL

We have created a new database with the name ‘dbemployee’, and with ‘USE dbemployee’ command, we have selected this database. Then, with the ‘CREATE TABLE’ command, we have created a table ‘employee’ in the database ‘dbemployee’.

Now, we will insert data into the above created table.

mysql> INSERT INTO employee VALUES (1, "Sana Khan", "HRM", 45000, "1999-06-17");
Query OK, 1 row affected (0.05 sec)
mysql> INSERT INTO employee VALUES (2, "Anupama Deshmukh", "Finance", 32000, CURDATE ());
Query OK, 1 row affected (0.11 sec)


mysql> INSERT INTO employee VALUES (3, "Kajal Shah", "Purchasing", 71000, "2020-12-12");
Query OK, 1 row affected (0.09 sec)


mysql> INSERT INTO employee VALUES (4, "Mayuri Koli", "Accounts", 64000, "1987-08-18");
Query OK, 1 row affected (0.09 sec)


mysql> INSERT INTO employee VALUES (5, "Surili Maheshwari", "Production", 30000, "1970-10-10");
Query OK, 1 row affected (0.09 sec)
How to compare date in SQL

After inserting data successfully into the table, we will now fetch all the records of a table.

mysql> SELECT *FROM employee;
+--------+-------------------+------------+------------+------------------+
| Emp_Id | Emp_Name          | Emp_Dept   | Emp_Salary | Emp_Joining_Date |
+--------+-------------------+------------+------------+------------------+
|      1 | Sana Khan         | HRM        |      45000 | 1999-06-17       |
|      2 | Anupama Deshmukh  | Finance    |      32000 | 2021-06-26       |
|      3 | Kajal Shah        | Purchasing |      71000 | 2020-12-12       |
|      4 | Mayuri Koli       | Accounts   |      64000 | 1987-08-18       |
|      5 | Surili Maheshwari | Production |      30000 | 1970-10-10       |
+--------+-------------------+------------+------------+------------------+
5 rows in set (0.00 sec)
How to compare date in SQL

Now, let’s write a query for the given problem statement.

mysql> SELECT *FROM employee WHERE Emp_Joining_Date >= '1999-05-05';

Output:

+--------+------------------+------------+------------+------------------+
| Emp_Id | Emp_Name         | Emp_Dept   | Emp_Salary | Emp_Joining_Date |
+--------+------------------+------------+------------+------------------+
|      1 | Sana Khan        | HRM        |      45000 | 1999-06-17       |
|      2 | Anupama Deshmukh | Finance    |      32000 | 2021-06-26       |
|      3 | Kajal Shah       | Purchasing |      71000 | 2020-12-12       |
+--------+------------------+------------+------------+------------------+
3 rows in set (0.00 sec)
How to compare date in SQL

There are three employees with employee ids 1, 2 and 3 whose joining date is greater than 5th May 1999.

Example 2:

Write a query to find all the employees whose joining date is less than or the same as 5th May 1999.

mysql> SELECT *FROM employee WHERE Emp_Joining_Date <= '19990505';

Output:

+--------+-------------------+------------+------------+------------------+
| Emp_Id | Emp_Name          | Emp_Dept   | Emp_Salary | Emp_Joining_Date |
+--------+-------------------+------------+------------+------------------+
|      4 | Mayuri Koli       | Accounts   |      64000 | 1987-08-18       |
|      5 | Surili Maheshwari | Production |      30000 | 1970-10-10       |
+--------+-------------------+------------+------------+------------------+
2 rows in set (0.00 sec)
How to compare date in SQL

Two employees with employee ids 4 and 5 whose joining date is less than 5th May 1999.

Example 3:

Write a query to find all the employees whose joining date is same as 8th August 1987.

mysql> SELECT *FROM employee WHERE Emp_Joining_Date = 19870818;

Output:

+--------+-------------+----------+------------+------------------+
| Emp_Id | Emp_Name    | Emp_Dept | Emp_Salary | Emp_Joining_Date |
+--------+-------------+----------+------------+------------------+
|      4 | Mayuri Koli | Accounts |      64000 | 1987-08-18       |
+--------+-------------+----------+------------+------------------+
1 row in set (0.00 sec)
How to compare date in SQL

There is only one employee with employee id 4 whose joining date is equal to 18th August 1987.

Using date()

Example 4:

Write a query using the date () function to find all the employees whose joining date is the same as 26th June 2021.

mysql> SELECT *FROM employee WHERE date (Emp_Joining_Date) = '2021-06-26';

Output:

+--------+------------------+----------+------------+------------------+
| Emp_Id | Emp_Name         | Emp_Dept | Emp_Salary | Emp_Joining_Date |
+--------+------------------+----------+------------+------------------+
|      2 | Anupama Deshmukh | Finance  |      32000 | 2021-06-26       |
+--------+------------------+----------+------------+------------------+
1 row in set (0.00 sec)
How to compare date in SQL

There is only one employee with employee id 2 whose joining date is equal to 26th June 2021.


Related Topics

SQL NOT Operator

SQL NOT is a Boolean operator used with the WHERE clause. NOT operator shows the records if the expression is false. When we use the NOT operator, we fetch only...

7 minutes read.

SQL Aggregate Operators

In SQL, the aggregate operators perform a calculation on multiple rows or multiple values of a single column and give the output a single value. Here, multiple rows of data are...

4 minutes read.

SQL SELECT MAX

The SQL Max() function is an aggregate function in SQL. This function returns the values which are greater in the condition. The condition may be a number, or it may...

5 minutes read.

SQL SELECT BETWEEN Operator

This SQL tutorial explains and helps us understand how to use the BETWEEN operator in the SELECT query with examples. The SQL SELECT BETWEEN operator retrieves the data from the table...

2 minutes read.

SQL TABLE

SQL TABLE Structured Query Language (SQL) is a relational database (RDBMS) where data is stored in the form of tables, that is, in rows and columns. These tables are known as...

3 minutes read.

How to create a database in SQL?

How to create a database in SQL It is very necessary to create a database in order to store the data into the database.Database name must always be unique.SQL does not...

6 minutes read.

Update Query in SQL

Update is an SQL command that is used to modify the data that in already present in database. Update is a command of DML. DML means Data Manipulation Language. Basically,...

3 minutes read.

How to use HAVING clause in SQL

In this article, we will learn about the HAVING clause concept and how to use it in SQL. What is the HAVING clause? In Structured Query Language, HAVING Clause used with GROUP...

7 minutes read.

SQL KEYS

SQL KEYS are single or multiple attributes used to get data from the table according to the requirement or condition. They can also be used to set up relationships amongst...

3 minutes read.

SQL Aggregate Functions

In this tutorial, we will learn and understand the SQL Aggregate Functions concept with the help of examples. SQL Aggregate Function is a function used to perform calculation operations on one...

4 minutes read.

SQL CRUD Operation

In any computer language, CRUD operation is a foundation. CRUD operation rules are applied in databases as well. These operations are the basic operations used to perform with any database...

7 minutes read.

SQL Truncate

This command deletes all records from table. Truncate is a DDL command. Syntax: TRUNCATE table table_name; Example: Truncate table teacher; ORDER BY The ORDER BY clause arranges the table or column in ascending...

5 minutes read.

SQL Right Join

The SQL Right Join query displays all the table records and similar records from the left table. The query display zero records if it doesn’t find any similar records. If...

4 minutes read.

SQL SELECT MIN

The SQL Min() function is an aggregate function in SQL. SQL Min() returns the minimum value of a given condition. The expression may be numerical, or it may be an expression. The syntax...

5 minutes read.

SQL CREATE TABLE

In SQL tutorial, we learned and created different databases. To stores data in databases, we need to create a table. To create the table, we need to use CREATE TABLE...

5 minutes read.

space() function in SQL

 This function returns a string with a specified number of spaces. If we specify a number, it returns the strings having that specified number of spaces. SPACE Function is available...

2 minutes read.

Commit and Rollback in SQL

In Structured Query Language, we have Data Definition Language (DDL) and Data Manipulation Language (DML) commands the same way we have Transaction Control Language (TCL) commands in the Structured Query...

4 minutes read.

SQL Queries

In a database, queries are used to request the result set of data from the table or action on the records. A Query can answer your simple or complicated question, perform...

5 minutes read.

SQL UNION ALL Operator

This page has all the information about UNION ALL operator, which is used to join all the values from the SELECT queries. Similar records were not considered in the result in...

3 minutes read.

How to use the BETWEEN operator in SQL

In this entire SQL article, we will understand and learn about the BETWEEN operator concept and how to use it in SQL. What is the BETWEEN operator in SQL? The Between operator...

4 minutes read.