SQL Select then Delete
sql, sql-server, sql-server-2008
Solution
delete from app_personal_details
where application_id in (
select status_id
from dbo.app_status
where submission_date <= dateadd(month, -12, getdate())
)
Problem
I need to clean out some old data from my database tables. Below shows two tables, app_status and app_personal_details. I have the following query to select all the records (applications) from the app_status table which have been created over 12 months from todays date ``` select status_id from dbo.app_status where submission_date <= dateadd(month, -12, getdate()) order by 1 ``` I then want to delete all the records from the app_personal_details table where the application_id exists in the list of retrieved status_id above. I hope this makes sense. I would like to know a quick way of deleting the data from both tables. Thanks for your help.