×

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 Inner Join

In Structured Query Language, the most used join query is the Inner join query. Inner join query retrieves the records from one or more tables with similar data or records. The...

4 minutes read.

How to use INNER JOIN in SQL

In this article, we will learn about the INNER JOIN concept and how to use it in SQL with the WHERE clause. What is INNER JOIN in SQL? Inner Join is a...

6 minutes read.

SQL Syntax

In this tutorial, we will help you understand about the different SQL Syntax concepts with the help of an example. Every Structured Query Language starts with Keywords like select, insert, update,...

5 minutes read.

Trigger in SQL

In this article, we will learn about the concept of trigger in SQL and its implementation with the help of an example. A Trigger in Structured Query Language is a set...

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

SQL DELETE

In this tutorial, you will learn about the SQL DELETE concept by using examples. In Structured Query Language (SQL), we fetch the data from the database table using SELECT Statement and...

3 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 CROSS Join

In this tutorial, we will help you understand the concept of the SQL CROSS Join clause with the few examples. The SQL CROSS Join query is used to join one or...

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.

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 Cloning Tables

Cloning a Table: To create a copy of the table. To perform the operations, without affecting the actual table. Steps for creating a Cloning Table: Step 1: Empty Table Creation The syntax for Creating...

3 minutes read.

How to drop a column in SQL?

How to drop a column in SQL Introduction To delete a column from an already created table, one needs to use the ALTER command along with the DROP COLUMN clause. Syntax: ALTER TABLE tablename...

4 minutes read.

SQL Data Definition Language

Data definition language directly effects on the structure/schema of the database. CREATE, ALTER, DROP are the commands of DDL.CREATE: Creates new database, table, or view of table.ALTER: Modifies the database or...

3 minutes read.

SQL WHERE Multiple Conditions

In this topic, we will learn how to add multiple conditions using the WHERE clause. First, let's understand the concept of WHERE clause. WHERE clause is used to specify a condition while...

5 minutes read.

SQL Data Control Language

Data Control Language decides to whom should (which user) permit access privileges. GRANT and REVOKE are the commands of DCL. GRANT: It gives privileges to user. REVOKE: It takes back privileges from granted...

1 minute read.

Where vs Having

Difference Between Where and Having The WHERE and HAVING clauses in SQL are used to filter records stored in tables in the databases. The dissimilarities between the WHERE and HAVING clause...

3 minutes read.

DML Commands in SQL

DML is an abbreviation of Data Manipulation Language. Data Manipulation Language commands in Structured Query Language manipulate the data in the database. DML commands are used to retrieve records, add records,...

4 minutes read.

Truncate function in SQL

The TRUNCATE is a numeric function in SQL which truncates the number according to the particular decimal points. Syntax of TRUNCATE Function SELECT TRUNCATE(X, D) AS Alias_Name; In the TRUNCATE syntax, X...

4 minutes read.

SQL Count

Structured Query Language Count() Function is used with Structured Query Language SELECT Statement. SQL Count() function returns the number of items that match the specified criteria in the SELECT statement. Count()...

2 minutes read.

Drop vs Truncate in SQL

In this article, we will learn and understand the Drop and Truncate commands and the difference between these two commands. What is Drop Command? Drop is a Data Definition Language command in...

6 minutes read.