T-SQL: Comparing Two Tables - Records that don't exist in second table

前端 未结 7 1370
小鲜肉
小鲜肉 2021-01-02 05:05

If UNION ALL is an addition in T-SQL. What is the equivalent of subtraction?

For example, if I have a table PEOPLE and a table

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-02 06:02

    SELECT * FROM Table1
    WHERE Table1.Key NOT IN (SELECT Table2.Key FROM Table2 WHERE Table2.Key IS NOT NULL)
    

    Added IS NOT NULL to make people happy.

    I would agree with Tom. His version is most likely more efficient. The only possible reason to use mine, might be that it's prettier.

提交回复
热议问题