I am working on a SQL Server 2008 database where all items that can be used in the database are stored in table A with a key value of ItemNum. Table B and Table C use ItemNum as a key value and are where the items from table A are actually used. Table A contains many duplicates that aren't used in Table B and Table C. I need to delete all ItemNum from Table A that are not used in tables B or table C.
Right now I have the following two queries using
Select statements (which will eventually be switched to
Delete statements) which I am not sure are working correctly, and I want to create the most efficient query possible.
USE Database GO SELECT ItemNum FROM B WHERE EXISTS (SELECT ItemNum FROM A WHERE A.ItemNum = B.ItemNum) USE Database GO SELECT ItemNum FROM C WHERE EXISTS (SELECT ItemNum FROM A WHERE A.ItemNum = C.ItemNum)