Luis
Luis

Reputation: 3277

Different between two tables

I am trying to take the different values between table2 to table1 (I should compare table2 to table1). I tried with Join but unfortunately it doesn't work for me (or simply I don't know what to write).

table1:

id
---
1
2

table2:

id
---
4
5
7
2
3
1

Result should be - 4, 5, 7, 3

Upvotes: 1

Views: 124

Answers (4)

ypercubeᵀᴹ
ypercubeᵀᴹ

Reputation: 115530

One more way:

SELECT id
FROM table2
WHERE id NOT IN
      ( SELECT id
        FROM table1
      )

Upvotes: 1

Michael Fredrickson
Michael Fredrickson

Reputation: 37388

SELECT t2.ID
FROM table2 t2
LEFT JOIN table1 t1 ON t1.ID = t2.ID
WHERE t1.ID IS NULL

Upvotes: 3

Curtis
Curtis

Reputation: 103358

SELECT id
FROM [TableB]
WHERE NOT EXISTS(SELECT id FROM [TableA] WHERE [TableA].id=[TableB].id)

Upvotes: 3

Kaganar
Kaganar

Reputation: 6570

Use except. See mention of except on MSDN.

Upvotes: 0

Related Questions