working with NULL values

Posted on 2010-11-24
Medium Priority
Last Modified: 2012-05-10
what is the reason that the select statement does not bring back valid records. seems like the NULL can kill the query from bringing back good results..

create table one (first tinyint)
insert into one select 1 union select 2 union select 3
create table two (second tinyint)
insert into two select 1 union select NULL

select first from one where first not in (select second from two) -- does not work

drop table one
drop table two
Question by:anushahanna
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 58

Accepted Solution

cyberkiwi earned 1000 total points
ID: 34208541
That is because NULLS are a quirk, they are incomparable.
set @d = null
if @d = 1 print 1
if @d != 1 print 1
neither will fire because it is neither equal to nor not equal to

It is just what it is
For IN (.. including NULL ..) it will return matches EXCEPT null, i.e. null records from left hand side won't be matched [unless ANSI_NULLS is set off]

FOR NOT IN (..includes null)
It will never return anything.
LVL 58

Assisted Solution

cyberkiwi earned 1000 total points
ID: 34208615
For not in (... includes null)
It will never return anything. -- except when ansi_nulls is off

good programming practice indicates nulls should never be in an -in- list, i.e. filter it out

in (select i from tbl where i is not null)
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 500 total points
ID: 34208623
could also do:

select first from one where first not in (select isnull(second,'') from two)

LVL 23

Assisted Solution

Racim BOUDJAKDJI earned 500 total points
ID: 34209514
NULL are not a value but a marker for missing value.  The best way to do design is to avoid using them at any cost.

produces the same as


Author Comment

ID: 34210242
phew.. will be careful with that one.. thanks for the example to make it easy..

really appreciate it..

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question