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

%3CLINGO-SUB%20id%3D%22lingo-sub-1408531%22%20slang%3D%22en-US%22%3ENOT%20IN%20or%20IN%20Clause%20gives%20unexpected%20result%20Azure%20Synapse%2FAzure%20SQL%20Database%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1408531%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20seen%20SQL%20developers%20using%20IN%20%2F%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Flanguage-elements%2Fin-transact-sql%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ENOT%20IN%3C%2FA%3E%20%2F%20NOT%20EXISTS%20conditions%20frequently%20in%20Azure%20Synapse%2FAzure%20SQL%20Database%20to%20filter%20out%20the%20rows%20which%20are%20not%20common%20in%20either%20of%20the%20tables%20using%20few%20joins%20and%20sub-queries.%20I%20made%20an%20observation%20recently%20that%20the%20NOT%20IN%20clause%20did%20not%20give%20expected%20number%20of%20rows%20for%20a%20query.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20ex.%20If%20you%20expect%20an%20output%20of%203022%20rows%20not%20in%20table%20T1%20but%20the%20actual%20result%20you%20get%20is%20really%20unexpected%20!%20Say%20you%20get%20an%20output%20result%200.%20Oops!%20How%20is%20it%20possible%20that%20you%20had%20to%20get%20an%20output%20of%203022%20rows%2C%20but%20the%20actual%20windows%20shows%20you%20Zero%20result%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E%3CSTRONG%3EExpected%20%3A%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22mubhashk_1-1590061290302.png%22%20style%3D%22width%3A%20170px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F193535i99286B085E8E94A6%2Fimage-dimensions%2F170x86%3Fv%3D1.0%22%20width%3D%22170%22%20height%3D%2286%22%20title%3D%22mubhashk_1-1590061290302.png%22%20alt%3D%22mubhashk_1-1590061290302.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CFONT%20color%3D%22%230000FF%22%3EActual%20Output%20%3A%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22mubhashk_0-1590061196350.png%22%20style%3D%22width%3A%20304px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F193534i9C6818139971B5F0%2Fimage-dimensions%2F304x99%3Fv%3D1.0%22%20width%3D%22304%22%20height%3D%2299%22%20title%3D%22mubhashk_0-1590061196350.png%22%20alt%3D%22mubhashk_0-1590061196350.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EQuestion%20%3A%20Why%20there%20is%20a%20difference%20%3F%3C%2FP%3E%0A%3CP%3EAnswer%20%3A%20This%20is%20a%20limitation%20of%20the%20command%20when%20you%20have%20NULL%20record%20value%20in%20your%20table.%20Yes%20you%20read%20it%20correct%2C%20this%20is%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Flanguage-elements%2Fin-transact-sql%3Fview%3Dsql-server-ver15%23result-value%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3Elimitation%3C%2FA%3E.%20Please%20read%20out%20below%20%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22mubhashk_3-1590061627806.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F193537i7C28FD33B3D5B6BF%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22mubhashk_3-1590061627806.png%22%20alt%3D%22mubhashk_3-1590061627806.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E%3CSTRONG%3EDemo%20%3A%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3ECase%201%20where%20Not%20IN%20gives%20expected%20result%20%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22mubhashk_5-1590064583159.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F193545iFEBD75EEEB6143DF%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22mubhashk_5-1590064583159.png%22%20alt%3D%22mubhashk_5-1590064583159.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20introduced%20NULL%20values%20in%20one%20columns%20on%20which%20I%20have%20put%20NOT%20IN%20or%20IN%20join%20and%20see%20the%20difference%20in%20output%20just%20because%20of%20one%20Null%20Value%20%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22mubhashk_7-1590064957837.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F193549iB69A74A5A5649FF8%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22mubhashk_7-1590064957837.png%22%20alt%3D%22mubhashk_7-1590064957837.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E%3CSTRONG%3EHow%20to%20Identify%20the%20issue%3A%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3ETry%20to%20find%20if%20any%20of%20the%20columns%20have%20NULL%20values%20due%20to%20which%20the%20issue%20is%20being%20faced%20%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22mubhashk_8-1590065029618.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F193550i7E01BCD9E66B8313%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22mubhashk_8-1590065029618.png%22%20alt%3D%22mubhashk_8-1590065029618.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E%3CSTRONG%3EMitigation%20%3A%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E1.%20You%20can%20use%20NOT%20EXISTS%20instead%20which%20will%20work%20even%20after%20these%20constraints%20(NULL%20valued%20column)%3C%2FP%3E%0A%3CP%3EYou%20need%20to%20use%20Syntax%20for%20-ERR%3AREF-NOT-FOUND-NOT%20EXISTS%20%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22mubhashk_9-1590065333983.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F193552i3B2B1D688EEE3AE6%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22mubhashk_9-1590065333983.png%22%20alt%3D%22mubhashk_9-1590065333983.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E2.%20You%20can%20eliminate%20NULLs.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThat's%20all%20Folks%3C%2FP%3E%0A%3CP%3ECheers!%20Don't%20forget%20to%20comment%2FAsk%20Questions!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
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!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

0 Replies