Your query should check null condition as well.

Query -

select id , v from #t where  v not in ('PC' , 'PT') OR v is null
Answer from Sudharshan on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/sql โ€บ using "where" statement is removing null values, help?
r/SQL on Reddit: Using "WHERE" Statement is removing Null Values, help?
January 7, 2019 -

Hi All,

I'm working in Microsoft Access 2010 and a query i've created is giving me some issues. Basically, I have set up the query to select all records that do not contain the letter 'R' by including the phrase:

SELCECT *
FROM 
WHERE Field Is Not Like '*R*';

However, some records with are null values in Field are not being selected, but not all records. I've revised the where clause to say:

WHERE (Field IS NULL OR Field Is Not Like '*R*');

and that seems to fix the issue.

Can anyone explain why I need that extra statement? I suspect it's some weird issue with the wildcards around the R in my statement, but I would think if the value in Field doesn't have an 'R' in it anywhere, it should select that record.

๐ŸŒ
W3Schools
w3schools.com โ€บ sql โ€บ sql_null_values.asp
SQL NULL Values - IS NULL and IS NOT NULL
The IS NULL operator is used to test for empty values (NULL values). The following SQL lists all customers with a NULL value in the "Address" field:
๐ŸŒ
SQL Shack
sqlshack.com โ€บ working-with-sql-null-values
Working with SQL NULL values
May 19, 2021 - If we widen this theoretical explanation, the NULL value points to an unknown value but this unknown value does not equivalent to a zero value or a field that contains spaces. Due to this structure of the NULL values, it is not possible to use traditional comparison (=, <, > and <>) operators in the queries. As a matter of fact, in the SQL Standards using the WHERE clause as the below will lead to return empty result sets.
Top answer
1 of 2
10

To start off, NULL does not mean "no value" it means "Unknown value" in SQL Server. There is a session setting called ANSI_NULLS that could make your queries behave as you would like them to, however, it's deprecated and will be forced to ON (which you don't seem to like) in a future version: http://msdn.microsoft.com/en-us/library/ms188048.aspx

I get what you're trying to do, and to make a counter point I would ask if you've seen any queries generated by reporting services or something like Cognos? If so, you'll see exactly what you're describing you don't want to do. Why? Well because with a schema that allows nulls, that's the way to do it. I'm not saying it's a super awesome and great idea but it works all of the time.

What your designer could do is check to see if that column could even be null and if so the appropriate logic could step it and create the correct query. You could also have options such as "This column may be null, do you want those values?". I don't know the end-game per-se and writing your own dynamic querying tool is quite the deat when the logical consistencies are all factored in (such as this).

I would continue to do explicit null checking on columns that could possibly be null, sure it doesn't look the best but it works all of the time.

ANSI_NULL set option will work for now but NOT a good idea especially if you don't control the environment, plus it will be forced set ON later and cause errors where you'll need to re-write your application logic anyway. This is the way SQL Server works with NULLs.

2 of 2
10

The NULL problem is a thorny issue with SQL. It is basically a mistake that is now burnt into all SQL software on the planet. We have to deal with it.

value <> 26 or value is null is a good way to implement this logic. There are other formulations of the same semantics.

If you know that value is never -1 (for example) you can say ISNULL(value, -1) <> 26. I don't think that's better from a readability standpoint. It can also cause optimizer problems because this predicate might not be SARGable. value is null is a SARGable and indexable predicate contrary to popular belief.

SQL has the IS DINSTINCT FROM operator but T-SQL does not support it. Please take a second to vote for the request to have it implemented! It is a purely syntactic issue. The optimizer does not need to change at all. It already supports that operator internally.

TL;DR: The way you are doing it right now is the right way. Live with it.

๐ŸŒ
OutSystems
outsystems.com โ€บ forums โ€บ discussion โ€บ 52118 โ€บ sql-query-to-fetch-the-values-including-null
SQL query to fetch the values including NULL | OutSystems
My current set up for drop down filter accepts multiple values. Implementation is done to fetch the details from SQL query. But when the Blank is selected in the drop down, it implies to NULL value to fetch hte records from database. Below is the query executed which is returning wrong results ...
๐ŸŒ
IBM
ibm.com โ€บ docs โ€บ en โ€บ db2-for-zos โ€บ 12.0.0
Application programming and SQL - Handling null values
A null value indicates the absence of a column value in a row. A null value is an unknown value; it is not the same as zero or all blanks.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ sql โ€บ sql-null-values.htm
SQL - NULL Values
You can update the NULL values present in a table using the UPDATE statement in SQL. To do so, you can use the IS NULL operator in your WHERE clause to filter the rows containing NULL values and then set the new value using the SET keyword.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ sql โ€บ how-to-use-null-values-inside-not-in-clause-in-sql
How to Use NULL Values Inside NOT IN Clause in SQL? - GeeksforGeeks
July 23, 2025 - The subquery includes NULL values. When NULL is part of a NOT IN clause, all comparisons involving it return NULL, invalidating the condition and resulting in no rows being returned.
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ sql โ€บ t-sql โ€บ language-elements โ€บ null-and-unknown-transact-sql
NULL and UNKNOWN (Transact-SQL) - SQL Server | Microsoft Learn
To test for null values in a query, use IS NULL or IS NOT NULL in the WHERE clause. You can insert null values into a column by explicitly stating NULL in an INSERT or UPDATE statement, or by leaving a column out of an INSERT statement.
๐ŸŒ
Kiprosh Blogs
blog.kiprosh.com โ€บ alternative-ways-to-include-null-values-in-the-results-while-using-sql-negation-commands-with-rails
Alternative ways to include NULL values while using SQL negation
June 3, 2021 - ... The above queries will consider records with state having a NOT NULL value, but state having a NULL value will not be considered. To consider state with the NULL value, we have to explicitly add the OR clause.
Top answer
1 of 2
1

Here is a more long-winded explanation which takes it step by step. We have:

select * from tab1 where col1 not in (select col2 from tab2)

We can replace the subquery with the values in tab2. This gives us:

select * from tab1 where col1 not in (1, null, 6 ,4, 7)

Let's move the NOT:

select * from tab1 where not (col1 in (1, null, 6 ,4, 7))

IN is just a shortcut for OR, so what we really have is:

select * from tab1 where NOT (col1 = 1 OR col1 = null OR col1 = 6 OR col = 4 OR col1 = 7)

Let's nu study this for some of the values for tab1.col1. We can start with 1. This gives us

NOT (1 = 1 OR ...)

and we can stop there. The list of OR will return TRUE and with NOT in front we get FALSE.

What if we try 5?

NOT (5= 1 OR 5 = null OR 5 = 6 OR 5 = 4 OR 5 = 7)

so that's

NOT (FALSE OR 5 = NULL OR FALSE OR FALSE OR FALSE)

What about 5 = NULL? Well NULL is an unknown value. It could be different from 5, but by chance it could be 5. So the outcome is not TRUE, not FALSE, but UNKNOWN. So we have:

NOT (FALSE OR UNKNOWN OR FALSE OR FALSE OR FALSE)

The result of all the OR is UNKNOWN and with NOT in front, the value is still UNKNOWN by the three-valued logic of SQL. But for WHERE to include a row, the condition for the row must be TRUE, and thus the row with 5 is filtered out as well.

Using NOT EXISTS as Dan showed is a very good solution. Not the least since [NOT] EXISTS is more powerful than IN. However, there is a simpler fix to the original query:

select * from tab1 where col1 not in (select col2 from tab2 WHERE col2 IS NOT NULL)
2 of 2
1

It's SQL specification or bug?

The unintuitive behavior of NOT IN is part of the SQL standard specification. The SQL Server documentation includes this caution:

Any null values returned by subquery or expression that are compared to test_expression using IN or NOT IN return UNKNOWN. Using null values in together with IN or NOT IN can produce unexpected results.

The gotcha is that UNKNOWN is neither TRUE nor FALSE when NULL values are returned so the NOT IN predicate will never evaluate to either true or false . You may find NOT EXISTS is more intuitive because it will return only TRUE or FALSE.

SELECT *  
FROM tab1   
WHERE NOT EXISTS (  
    SELECT 1   
    FROM tab2  
    WHERE tab2.col2 = tab1.col1  
);  
๐ŸŒ
W3Computing
w3computing.com โ€บ sql server โ€บ queries โ€บ null values, like operator in where clause
NULL Values, LIKE Operator in WHERE Clause - SQL Server
May 12, 2021 - This specification in a WHERE clause of a SELECT statement has the following general form: ... Example 6.14 shows the use of the IS NULL operator. ... Get employee numbers and corresponding project numbers for employees with unknown jobs who work on project p2: USE sample; SELECT emp_no, project_no ...
๐ŸŒ
365 Data Science
365datascience.com โ€บ q&a โ€บ will "<0" (less than zero) include null values? โ€“ q&a hub
Will "<0" (less than zero) include null values? โ€“ Q&A Hub โ€“ 365 Data Science
I have a column with values =0, >0, and null. If I have a condition where I want to select all values less than 0, including those null values, "<0" doesn't seem to include null values. Is this true?
๐ŸŒ
Oracle
blog.toadworld.com โ€บ home โ€บ toad data point tutorials: sql where clause null values
Toad Data Point Tutorials: SQL Where Clause Null Values - The Quest ...
October 1, 2025 - Clause nulls are also an issue and have their own syntax to check for the existence of a null or non-existence of a null (i.e.: the field has a data value). This blog will focus on these areas of SQL using Traditional Toad Data Point:
๐ŸŒ
SQLServerCentral
sqlservercentral.com โ€บ forums โ€บ topic โ€บ where-clause-excludes-rows-with-null-values
Where clause excludes rows with Null Values โ€“ SQLServerCentral Forums
May 23, 2011 - For some reason, this query is also excluding results where the ACCOUNT_NAME field is null. I have tried including "AND (ACCOUNT_NAME IS NULL OR ACCOUNT_NAME='')" but that returned no results.
๐ŸŒ
Quora
quora.com โ€บ How-do-you-add-null-values-in-SQL
How to add null values in SQL - Quora
Answer (1 of 3): NULL values are handled differently depending on whether they are in the context of a tuple or a set. In the case of a tuple: (e.g. 1 + NULL), the result is always NULL. This is because if any part of a tuple is missing or unknown then the entire tuple cannot be known and hence m...