Vinícius
Vinícius

Reputation: 483

How to compare two tables in SQL?

For example, I have two tables, one of them I have the product description, in another I have data like "price", "date of registration", among others. I would like to know how I can delete the description that does not have id in the product table.

Upvotes: 0

Views: 100

Answers (2)

ranjith
ranjith

Reputation: 424

Consider if we have two tables

    Product table
    Id-----Description
 ---------------------------------
    1------It is abcd
    2------It is Efg
    3------It is Xyz
    Product table
    Id------Name------price---date
 -----------------------
    1-------Abcd------10-----1/1/2007
    2-------Efg------20-----2/2/2007

We need to delete decription 'Its xyz' which doesnot have id in product table. So use this query

delete from Description
where Id not in (
    select Id from Product
);

Upvotes: 0

Tobb
Tobb

Reputation: 12225

Something like:

delete from ProductDescription
where productId not in (
    select productId from Product
);

Upvotes: 1

Related Questions