Reputation: 39
I am a beginner with SQL and I have a question regarding finding a subset of data that does not exist in another table.
Currently I have 2 tables
Table A has a single column of OrderID containing about 300 records Table B also has a single column containing 1000 records
How do I write a SQL query that helps me identify the 700 records not in Table A?
Thank you
Upvotes: 0
Views: 605
Reputation: 18747
You need to use NOT IN
.Try this:
SELECT * FROM TableB
WHERE OrderID NOT IN (SELECT OrderID FROM TableA)
OR
Use a join.
SELECT B.*
FROM TableB B LEFT JOIN TableA A ON A.OrderID = B.OrderID
WHERE A.OrderID IS NULL
Upvotes: 1
Reputation: 3031
Try this:
SELECT TableB.* FROM TableB LEFT JOIN TableA ON TableВ.OrderID = TableA.OrderID WHERE TableA.OrderID is NULL;
Upvotes: 0