×

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

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.

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 INSERT INTO SELECT

In this tutorial, we will help you to understand and learn how to copy records from one table and add them to another table in the SQL with the help...

5 minutes read.

SQL INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query. There are...

3 minutes read.

Codd’s Rules in SQL

Codd’s Rules Dr. Edgar F. Codd, in 1985, laid down 13 fundamental rules after doing large-scale research on the Relational Model of databases. According to him, every database must follow these...

3 minutes read.

SQL DROP Database

This tutorial will help us understand how to drop a database from the SQL Server with a few examples. The DROP command removes all the objects stored in the database and...

4 minutes read.

SQL SET Operator

In this tutorial, we will understand the operator who falls under the SET operator in SQL with the help of different examples. The SQL SET Operator is used to merge the...

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

SQL Arithmetic Operators

This page contains all the information, learn about SQL Arithmetic Operators concept in the SQL table with the help of examples. The Arithmetic Operators is used to perform mathematical calculations on...

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

How to Create Temporary Table in SQL?

How to Create Temporary Table in SQL  Introduction to Temporary Tables Temporary table is a table which is used to store temporary data that can be used further in the same client...

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

Where Condition in SQL

WHERE clause is used with the Data Manipulation Language Command in SQL, the Data Manipulation Language commands are the SELECT statement, UPDATE statement, and DELETE statement. WHERE clause condition is an...

5 minutes read.

SQL Except

In SQL, we probably use the JOIN clause to receive the combined result from one or more than one table. But sometimes, we want a result that contains data from...

4 minutes read.

SQL WHERE Clause

The WHERE clause is a search filter that returns only true records. The WHERE clause chooses the selected records based on the given conditions. The WHERE clause is used with...

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

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.

GROUP BY vs ORDER BY

The GROUP BY clause and ORDER BY clause in SQL are used to arrange data obtained by SQL queries. The important difference between the GROUP BY clause and ORDER BY...

4 minutes read.

SQL Scalar Functions

TEACHER Table LCASE() Function The LCASE() function is used to return lower case values of specified column. Syntax: Select Lcase(column_name) from table_name; Example: Select Lcase(teacher_name) from teacher; Syntax: Select Lcase(column_name) from table_name [where condition]; Example: Select Lcase(teacher_name) from teacher where teacher_id=1; UCASE() Function The UCASE()...

1 minute read.

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

4 minutes read.