Using SUM and EXCEPT in one query
04:12 07 Nov 2025

I'm using Microsoft SQL Server. I have two tables and I want to remove some rows from table 1 if the ID exist in table 2, and then do a sum of the AMT column for the remaining rows.

I currently have two queries to do this which works:

Query 1:

SELECT ID, AMT
INTO #temptable
FROM table1

EXCEPT

SELECT ID, AMT
FROM table2;

Query 2:

SELECT SUM(AMT)
FROM #temptable;

However, is there a way to combine these two queries into one to make it more efficient?

sql sql-server sum except