Open In App

How to Use NULL Values Inside NOT IN Clause in SQL?

Last Updated : 23 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In SQL, NULL holds a special status as it represents the absence of a value, making it fundamentally different from regular values. Unlike numbers or strings, NULL cannot be directly compared using operators like = or !=. This special status often leads to unexpected behavior in SQL queries, especially when filtering data. The NOT IN clause, commonly used for filtering data, can encounter challenges when NULL values are present.

A NULL in the data or comparison list can cause an entire query to return incorrect or empty results. In this article, we will explain how to effectively handle NULL values in the NOT IN clause, ensuring accurate query results and avoiding unexpected behavior.

How to Use NULL Values Inside NOT IN Clause

To demonstrate how to handle NULL values in the NOT IN clause, let’s create a table and populate it with sample data. This will help us understand the challenges and solutions in a practical context.

Query:

CREATE TABLE demo_table(
NAME VARCHAR(20),
GENDER VARCHAR(20),
AGE INT,
CITY VARCHAR(20) );

INSERT INTO demo_table VALUES
('ROMY KUMARI', 'FEMALE', NULL, 'NEW DELHI'),
('PUSHKAR JHA', 'MALE',24, 'NEW DELHI'),
('RINKLE ARORA', 'FEMALE',23, 'PUNJAB'),
('AKASH GUPTA', 'MALE', NULL, 'UTTAR PRADESH'),
('NIKHIL KALRA', 'MALE', 23, 'PUNJAB'),
('SHALINI JHA','FEMALE', 22, 'DELHI');

SELECT * FROM demo_table;

Output

demo_table

demo_table

Example 1: Excluding Rows with NULL Values Using NOT IN

To correctly handle NULL values in a NOT IN clause, we can exclude them explicitly. This ensures that only non-NULL values are considered in the comparison. Explicitly filtering out NULL values avoids invalidating the entire NOT IN condition. This approach is essential to ensure the query yields accurate and expected results.

Query:

SELECT * FROM demo_table WHERE AGE NOT IN (SELECT AGE WHERE AGE IS NULL);

Output

Excluding-Rows-with-NULL-Values-Using-NOT-IN

Excluding-Rows-with-NULL-Values-Using-NOT-IN

Explanation:

  • The subquery selects AGE values that are NULL.
  • By using NOT IN, the main query excludes rows where AGE matches the NULL values explicitly filtered out.
  • This ensures that only rows with non-NULL AGE values are included.

Example 2: Challenges with NULL in NOT IN Clause

The NOT IN clause evaluates each value in the comparison list against the target column. If the comparison list includes NULL, the entire condition becomes invalid, returning no results.

Query:

SELECT * FROM demo_table WHERE AGE NOT IN ((SELECT AGE WHERE AGE IS NULL),24);

Output

Explanation:

  • 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.

Example 3: Excluding Additional Specific Values Along with NULL

To exclude additional specific values along with NULL, we can use multiple NOT IN clauses combined with the AND operator.

Query:

SELECT * FROM demo_table 
WHERE AGE NOT IN (SELECT AGE WHERE AGE IS NULL) 
AND AGE NOT IN (24);

Output

NAME GENDER AGE CITY
RINKLE ARORA FEMALE 23 PUNJAB
NIKHIL KALRA MALE 23 PUNJAB
SHALINI JHA FEMALE 22 DELHI

Explanation:

  • The first NOT IN clause excludes rows where AGE is NULL.
  • The second NOT IN clause explicitly excludes rows where AGE = 24.
  • Combining these conditions ensures precise filtering, returning only rows with AGE values that are not NULL or 24.

Conclusion

Handling NULL values in SQL requires careful attention, especially when using the NOT IN clause. Since NULL cannot be directly compared, it’s crucial to explicitly exclude or account for it in our queries. By using subqueries and adding separate conditions for NULL, we can avoid common pitfalls and achieve accurate results. Mastering this technique ensures our SQL queries handle missing data effectively, leading to reliable outcomes in our database operations.



Next Article
Article Tags :

Similar Reads