NOT IN or IN Clause gives unexpected result Azure Synapse/Azure SQL Database

Microsoft

I have seen SQL developers using IN / NOT IN / NOT EXISTS conditions frequently in Azure Synapse/Azure SQL Database to filter out the rows which are not common in either of the tables using few joins and sub-queries. I made an observation recently that the NOT IN clause did not give expected number of rows for a query. 

 

For ex. If you expect an output of 3022 rows not in table T1 but the actual result you get is really unexpected ! Say you get an output result 0. Oops! How is it possible that you had to get an output of 3022 rows, but the actual windows shows you Zero result 

 

Expected :

mubhashk_1-1590061290302.png

 

Actual Output :

mubhashk_0-1590061196350.png

 

Question : Why there is a difference ?

Answer : This is a limitation of the command when you have NULL record value in your table. Yes you read it correct, this is limitation. Please read out below :

mubhashk_3-1590061627806.png

Demo :

Case 1 where Not IN gives expected result :

mubhashk_5-1590064583159.png

 

I introduced NULL values in one columns on which I have put NOT IN or IN join and see the difference in output just because of one Null Value :

 

mubhashk_7-1590064957837.png

 

How to Identify the issue:

Try to find if any of the columns have NULL values due to which the issue is being faced :

 

mubhashk_8-1590065029618.png

 

Mitigation :

1. You can use NOT EXISTS instead which will work even after these constraints (NULL valued column)

You need to use Syntax for NOT EXISTS :

 

mubhashk_9-1590065333983.png

 

2. You can eliminate NULLs. 

 

That's all Folks

Cheers! Don't forget to comment/Ask Questions!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2 Replies
Thanks for the article - I was pulling my hair out trying to work out why my query was returning unexpected results - your article led me around the issue.

@Darren009 you need to change a default settings for SSMS.

Such as SET ANSI_NULL = OFF. It should fix your problem