user1883676
user1883676

Reputation: 1

SQL Server Query helper

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

Answers (2)

Dan Pichelman
Dan Pichelman

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

Andomar
Andomar

Reputation: 238076

select  *
from    Orders o
where   not exists
        (
        select  *
        from    Attachments a
        where   a.OrderID = o.OrderID
        )

Upvotes: 1

Related Questions