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
Query:
CREATE DATABASE Gfg;
Step 2: Using the database
Query:
USE DATABASE Gfg;
After executing this query we can create tables in our database.
Step 3: Table definition
Query:
CREATE TABLE EMPLOYEE (name VARCHAR(20), age VARCHAR(20),
GENDER(20), birth DATE, Department VARCHAR(50) );
Output:
After executing this query, a table with the name Employee will get created with the specified column.
Step 4: Adding data to the table
Output:
After executing this query, records will get inserted as shown above.
Step 5: Deleting from the table based on a date
Query:
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).
Output:
As we have deleted the employees whose birthdate was older than ‘2001-01-04’, we can see the employees with the date greater than and equal to 2001-01-04.
Last Updated :
06 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...