Skip to content
Advertisement

SQL Server: select records, not linked to another table

I have a table:

and a linked table:

Relationship between these 2 tables:

I want to select all records from CollectionSiteAddress, which are not linked to CollectionSite (neither AddressId nor RemittanceId). Which request should I use?

I tried:

but it selects all records from CollectionSiteAddress

Advertisement

Answer

You are missing this WHERE clause:

because you want all the unmatched rows of CollectionSiteAddress.
I used the column [SiteCode] to check if it is NULL because it is not nullable in the definition of the table.
So you can write your query like this (shortened with aliases):

Or use NOT EXISTS:

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement