SQL Query to Delete a Data From a Table Based on Date
Many of the time we have to delete data based on the date. These dates can be some older dates. For this purpose, we can use delete query along with where clause. This approach helps us to delete some old data in our database. In this article, we are going to delete the data of employees based on their birth dates.
Step 1: Creating the database
CREATE DATABASE Gfg;
Step 2: Using the database
USE DATABASE Gfg;
After executing this query we can create tables in our database.
Step 3: Table definition
CREATE TABLE EMPLOYEE (name VARCHAR(20), age VARCHAR(20), GENDER(20), birth DATE, Department VARCHAR(50) );
After executing this query, a table with the name Employee will get created with the specified column.
Step 4: Adding data to the table
After executing this query, records will get inserted as shown above.
Step 5: Deleting from the table based on a date
DELETE FROM Employee where birth < '2001-01-04'
In this query, we are deleting the records which have birthdate older than 2001-01-04 (year-month-date).
As we have deleted the employees whos birthdate was older than ‘2001-01-04’, we can see the employees with the date greater than and equal to 2001-01-04.