SQL Tutorial

SQL Tutorial SQL Introduction SQL Syntax SQL Data Types SQL OPERATORS SQL COMMANDS SQL Queries

SQL Database

SQL Create Database SQL DROP Database SQL SELECT Database

SQL Table

SQL TABLE SQL CREATE TABLE SQL COPY TABLE SQL ALTER TABLE SQL DELETE SQL TRUNCATE TABLE SQL DROP TABLE SQL UPDATE TABLE SQL INSERT TABLE

SQL SELECT

SQL SELECT Statement SQL SELECT WHERE Clause SQL SELECT IN Operator SQL BETWEEN Operator SQL SELECT BETWEEN Operator SQL SELECT AND Operator SQL SELECT OR Operator SQL SELECT LIKE Operator SQL SELECT DISTINCT SQL SELECT SUM SQL SELECT MAX SQL SELECT MIN SQL SELECT AVG

SQL Clause

SQL WHERE Clause SQL GROUP BY CLAUSE SQL ORDER BY Clause SQL HAVING Clause

SQL INSERT

SQL INSERT Statement SQL INSERT INTO Statement SQL INSERT INTO Values SQL INSERT INTO SELECT SQL Insert multiple rows

SQL JOIN

SQL JOIN SQL Inner Join SQL Left Join SQL Right Join SQL Full Join SQL CROSS Join

SQL OPERATOR

SQL Comparison SQL LOGICAL Operator SQL Cast Operator SQL Arithmetic

Difference

SQL vs NOSQL WHERE vs HAVING DELETE vs DROP GROUP BY vs ORDER BY DROP vs TRUNCATE SQL IN vs SQL EXISTS Difference between Delete, Drop and Truncate in SQL

MISC

SQL SubQuery SQL CASE Commit and Rollback in SQL Pattern Matching in SQL DDL Commands in SQL DML Commands in SQL Types of SQL Commands SQL COUNT SQL Primary Key SQL FOREIGN KEY SET Operators in SQL Check Constraint in SQL SQL EXCEPT SQL VIEW SQL WHERE Statement SQL CRUD Operation Where Condition in SQL TCL Commands in SQL Types of SQL JOINS SQL Nth Highest Salary SQL NOT OPERATOR SQL UNION ALL SQL INTERSECT SQL Data Definition Language SQL Data Manipulation Language SQL Data Control Language SQL CONSTRAINTS SQL Aggregate Operators SQL KEYS Codd’s Rules in SQL What is SQL Injection? Trigger In SQL SQL WHERE Multiple Conditions Truncate function in SQL SQL Formatter WEB SQL SQL Auto Increment Save Point in SQL space() function in SQL SQL Aggregate Functions SQL Topological Sorting SQL Injection SQL Cloning Tables SQL Aliases SQL Handling Duplicate Update Query in SQL Grant Command in SQL SQL SET Keyword SQL Order BY LIMIT SQL Order BY RANDOM

How To

How to use the BETWEEN operator in SQL How To Use INNER JOIN In SQL How to use LIKE in SQL How to use HAVING Clause in SQL How to use GROUP BY Clause in SQL How To Remove Duplicates In SQL How To Delete A Row In SQL How to add column in table in SQL ? How to drop a column in SQL? How to create a database in SQL? How to use COUNT in SQL? How to Create Temporary Table in SQL? How to Add Foreign Key in SQL? How to Add Comments in SQL? How To Use Group By Clause In SQL How To Use Having Clause In SQL How To Delete Column In Table How To Compare Date In SQL How index works in SQL How to calculate age from Date of Birth in SQL How to Rename Column name in SQL what-are-single-row-and-multiple-row-subqueries">What are single row and multiple row subqueries?

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.