VOOZH about

URL: https://www.geeksforgeeks.org/sql/sql-query-to-delete-a-data-from-a-table-based-on-date/

⇱ SQL Query to Delete a Data From a Table Based on Date - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

SQL Query to Delete a Data From a Table Based on Date

Last Updated : 6 Mar, 2023

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;

👁 Image

Step 2: Using the database

Query:

USE DATABASE Gfg;

👁 Image

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:

👁 Image

After executing this query, a table with the name Employee will get created with the specified column. 

Step 4: Adding data to the table

👁 Image

Output:

👁 Image

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

👁 Image

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.

👁 Image

Comment
Article Tags: