Reputation: 1
Assume I have two tables as follow, I want to write a query and the rule is I want to find out which orderID DOES NOT have attachmentID. How can I query it in SQL?
OrderID Name
1 Computer
2 Laptop
3 Tablet
AttachmentID OrderID Url
1 1 ….
2 2 ….
Upvotes: 0
Views: 177
Reputation: 2332
Another possible answer, assuming Sql Server
select OrderID from Orders
EXCEPT
select OrderID from Attachments
See MSDN doc for Except and Intersect for more info.
Upvotes: 1
Reputation: 238076
select *
from Orders o
where not exists
(
select *
from Attachments a
where a.OrderID = o.OrderID
)
Upvotes: 1