delete where not in another table - Mailing list pgsql-sql

From Campbell, Lance
Subject delete where not in another table
Date
Msg-id B75CD08C73BD3543B97E4EF3964B7D701F353B73@CITESMBX1.ad.uillinois.edu
Whole thread Raw
Responses Re: delete where not in another table
List pgsql-sql

If I have two tables, T1 and T2, such that both have the same primary key of “user_id”.

What is the SQL I would use to delete all rows from T1 that are not in T2?

 

This is one way to write the SQL but it is really inefficient:

 

DELETE FROM T1 WHERE T1.user_id NOT IN (SELECT user_id FROM T2 WHERE T2.user_id=T1.user_id);

 

I was thinking there should be a way to write this with a join.

 

 

 

Thanks,

 

Lance Campbell

Software Architect

Web Services at Public Affairs

217-333-0382

University of Illinois at Urbana-Champaign logo

 

 

pgsql-sql by date:

Previous
From: David Johnston
Date:
Subject: Re: Update a composite nested type variable
Next
From: Igor Neyman
Date:
Subject: Re: delete where not in another table