DELETE certain rows based upon comparisons with other tables
suggest changeIt is possible to DELETE data from a table if it matches (or mismatches) certain data in other tables.
Let’s assume we want to DELETE data from Source once it is loaded into Target.
DELETE FROM Source
WHERE EXISTS ( SELECT 1 -- specific value in SELECT doesn't matter
FROM Target
Where Source.ID = Target.ID )
Most common RDBMS implementations (for example MySQL, Oracle, PostgreSQL, and Teradata) allow tables to be joined during DELETE, which allows a more complex comparison in a compact syntax.
Adding complexity to original scenario, let’s assume Aggregate is built from Target once a day and does not contain the same ID but contains the same date. Let us also assume that we want to delete data from Source only after the aggregate is populated for the day.
The following is not valid as written: Target and Aggregate are not named in FROM, so their columns are out of scope. The statement further down names those tables. Column names must also match (DataDate and AggDate, not Date).
In PostgreSQL use:
DELETE FROM Source
USING TargetSchema.Target, AggregateSchema.Aggregate
WHERE Source.ID = TargetSchema.Target.ID
AND TargetSchema.Target.DataDate = AggregateSchema.Aggregate.AggDate
This essentially results in INNER JOINs between Source, Target and Aggregate. The deletion is performed on Source when the same IDs exist in Target AND date present in Target for those IDs also exists in Aggregate.
Same query may also be written (on MySQL, Oracle, Teradata) as:
DELETE Source
FROM Source, TargetSchema.Target, AggregateSchema.Aggregate
WHERE Source.ID = TargetSchema.Target.ID
AND TargetSchema.Target.DataDate = AggregateSchema.Aggregate.AggDate
Explicit joins may be written in DELETE statements on some systems (for example Oracle and MySQL) but not on all platforms (Teradata does not support them).
Comparisons can be designed to check mismatch scenarios instead of matching ones with all syntax styles (observe NOT EXISTS below)
DELETE FROM Source
WHERE NOT EXISTS ( SELECT 1 -- specific value in SELECT doesn't matter
FROM Target
Where Source.ID = Target.ID )