SQL NULL Handling: Master IS NULL, IS NOT NULL, Three-Valued Logic & Comprehensive Examples
Table of Contents
1. Understanding NULL


Q: What is NULL in SQL?
NULL in SQL represents the absence of a value in a column. It is not equivalent to zero, an empty string (''), or any specific value. NULL indicates that data is unknown, missing, or not applicable.
Key Points:
NULLis not equal to anything, including anotherNULL(NULL = NULLis false).- Operations involving
NULL(e.g.,NULL + 5,NULL = 'text') typically result inNULL. NULLhandling varies slightly across RDBMS (e.g., MySQL, PostgreSQL, SQL Server, SQLite).
Q: Why is NULL important in SQL?
- Represents real-world scenarios where data is missing (e.g., an employee's email not provided).
- Affects query results, aggregations, and joins (e.g., rows with
NULLmay be excluded in comparisons). - Requires special handling with operators like
IS NULLandIS NOT NULLfor accurate filtering.
Q: How does NULL differ from other languages (e.g., C/C++)?
- SQL NULL: A database-specific marker for missing data, evaluated in three-valued logic (true, false, unknown). Comparisons with
=or<>don't work; useIS NULL. - C/C++ null: Typically a null pointer (
nullptrorNULL), used for memory references, not data absence in databases. - SQL Advantage: Explicit
NULLhandling ensures data integrity in relational models.
2. Filtering with IS NULL and IS NOT NULL


Q: How do you filter rows with NULL values in SQL?
Use the IS NULL operator in the WHERE clause to find rows where a column contains NULL. Use IS NOT NULL to find rows where a column has a non-NULL value.
Syntax:
SELECT column1, column2 FROM table_name WHERE column_name IS NULL;
SELECT column1, column2 FROM table_name WHERE column_name IS NOT NULL;
Note: Standard comparison operators (=, <>) do not work with NULL; column = NULL is invalid.
Q: Can you give an example of filtering with IS NULL and IS NOT NULL?
-- Create sample table
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Department VARCHAR(50),
Email VARCHAR(100),
Salary DECIMAL(10, 2)
);
-- Insert sample data with some NULLs
INSERT INTO Employees (EmployeeID, FirstName, LastName, Department, Email, Salary)
VALUES
(1, 'John', 'Doe', 'IT', '[email protected]', 60000.00),
(2, 'Jane', 'sahil', 'HR', NULL, 55000.00),
(3, 'kristal', 'Johnson', 'IT', '[email protected]',65000.00),
(4, 'Ram', 'Williams', 'HR', NULL, 55000.00),
(5, 'hari', 'Brown', 'Marketing', '[email protected]', NULL);
-- Query 1: Find employees with no email (IS NULL)
SELECT FirstName, LastName, Department
FROM Employees
WHERE Email IS NULL;
-- Query 2: Find employees with an email (IS NOT NULL)
SELECT FirstName, LastName, Email
FROM Employees
WHERE Email IS NOT NULL;
-- Query 3: Combine with other conditions
SELECT FirstName, LastName, Department, Salary
FROM Employees
WHERE Email IS NULL AND Department = 'HR';
Output (Query 1 - IS NULL):
FirstName | LastName | Department
----------|----------|------------
Jane | sahil | HR
Ram | Williams | HR
Output (Query 2 - IS NOT NULL):
FirstName | LastName | Email
----------|---------|-----------------------------
John | Doe | [email protected]
kristal | Johnson | [email protected]
hari | Brown | [email protected]
Output (Query 3 - Combined):
FirstName | LastName | Department | Salary
----------|----------|------------|----------
Jane | sahil | HR | 55000.00
Ram | Williams | HR | 55000.00
3. Comprehensive Example Combining NULL Handling with Prior Concepts
Q: Can you provide a comprehensive example using IS NULL, IS NOT NULL, and prior filtering concepts (WHERE, BETWEEN, IN, LIKE)?
-- Comprehensive query combining IS NULL, IS NOT NULL, and prior filtering
SELECT FirstName, LastName, Department, Email, Salary
FROM Employees
WHERE
(Department IN ('IT', 'HR') OR Salary IS NULL)
AND FirstName LIKE 'J%'
AND (Salary IS NULL OR Salary BETWEEN 50000 AND 65000)
ORDER BY Salary DESC
LIMIT 3; -- MySQL/PostgreSQL/SQLite (use TOP 3 for SQL Server)
Output:
FirstName | LastName | Department | Email | Salary
----------|----------|------------|----------------------|----------
John | Doe | IT | [email protected] | 60000.00
Jane | sahil | HR | NULL | 55000.00
Description:
- Filters employees:
- In IT or HR, or with
NULLsalary. - With names starting with 'J' (using
LIKE). - With salary between 50000 and 65000 or
NULL.
- In IT or HR, or with
- Sorts by salary (descending) and limits to 3 rows.
- Combines
IS NULL,IS NOT NULL,IN,LIKE,BETWEEN,AND/OR,ORDER BY, andLIMIT.
4. Common Mistakes and Best Practices

Q: What are common mistakes when working with NULLs in SQL?
- Using
=or<>withNULL:WHERE column = NULLorcolumn <> NULLis invalid; always useIS NULLorIS NOT NULL. - Assuming
NULLis a value:NULLis not equal to anotherNULLor any value, leading to unexpected results in comparisons. - Ignoring
NULLin aggregations: Functions likeSUMorAVGignoreNULLs, which may skew results if not handled. - Forgetting
NULLin joins: Rows withNULLmay be excluded inINNER JOINif compared improperly. - Overusing
IS NULL: UsingIS NULLwithout indexing or combining with other conditions can slow queries on large tables. - Misusing logical operators with
NULL:NULLinAND/ORconditions produces "unknown" results (e.g.,TRUE AND NULLis unknown, not true).
Q: What are best practices for working with NULLs in SQL?
NULL Handling:
- Use
IS NULLandIS NOT NULLexplicitly for filteringNULLvalues. - Avoid
NULLin primary keys or critical columns; useNOT NULLconstraints where possible. - Use
COALESCE(column, default_value)orIFNULL(column, default_value)(MySQL) to replaceNULLs with a default value in queries.
Query Design:
- Combine
IS NULLwith other conditions (e.g.,WHERE column IS NULL AND other_condition) to narrow results. - Use parentheses with logical operators to clarify
NULLhandling (e.g.,(column IS NULL OR condition)). - Test queries with
NULLs to ensure expected behavior.
Performance:
- Index columns frequently used in
IS NULLorIS NOT NULLfilters, but note that some RDBMS (e.g., PostgreSQL) may not indexNULLs efficiently. - Avoid excessive
IS NULLchecks in large datasets; consider redesigning schema to minimizeNULLs.
Schema Design:
- Define
NOT NULLconstraints for mandatory fields to reduceNULLhandling. - Use meaningful default values (e.g.,
DEFAULT ''for strings) instead ofNULLwhere appropriate.
General:
- Comment queries involving
NULLs for clarity (e.g.,-- Filter employees with missing emails). - Validate
NULLbehavior across RDBMS (e.g., SQL Server treatsNULLs differently in some cases). - Use
EXPLAINorEXPLAIN ANALYZEto check query performance withNULLfilters.