Skip to content
Advertisement

Query not returning expected data

I’m looking for an explanation for why 1 of the following 3 queries aren’t returning what I am expecting.

Assume the following:
Anmodning with ANNo=1, ANCpr=1111112222
And the Person table doesn’t have a row with PSCpr=111111-2222
Queries are executed in Management Studio against a SQL Server 2017.

Queries 2 and 3 returns the Anmodning row as expected but query 1 does not. Why is that?

Advertisement

Answer

I suspect the issue with the first query is a null-safety problem. If there are null values in Person(PSCpr), then the not in condition filters out all Anmodning rows, regardless of other values in Person.

Consider this simple example:

Returns no rows, while:

Returns 1 as you would expect.

This problem does not happen when you use left join, as in the second query.

You could also phrase this with not exists, which is null-safe, which I would recommend here:

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