Search code examples
sqlsql-serverdifference

How can I "subtract" one table from another?


I have a master table A, with ~9 million rows. Another table B (same structure) has ~28K rows from table A. What would be the best way to remove all contents of B from table A?

The combination of all columns (~10) are unique. Nothing more in the form a of a unique key.


Solution

  • If you have sufficient rights you can create a new table and rename that one to A. To create the new table you can use the following script:

    CREATE TABLE TEMP_A AS
    SELECT *
    FROM   A
    MINUS
    SELECT *
    FROM   B
    

    This should perform pretty good.