SQL NULL Handling: Master IS NULL, IS NOT NULL, Three-Valued Logic & Comprehensive Examples

Table of Contents

1. Understanding NULL

SQL Three-Valued Logic Truth Tables for AND, OR, NOT with TRUE, FALSE, UNKNOWN

Three-Valued Logic Truth Tables Illustrating UNKNOWN Behavior in SQL

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:

Q: Why is NULL important in SQL?

Q: How does NULL differ from other languages (e.g., C/C++)?

2. Filtering with IS NULL and IS NOT NULL

SQL IS NULL and IS NOT NULL Filtering Examples on Sample Table

IS NULL vs = NULL Comparison Illustration Showing Why = NULL Fails

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:

4. Common Mistakes and Best Practices

SQL NULL Handling Best Practices Infographic: COALESCE, IS NULL Tips

Q: What are common mistakes when working with NULLs in SQL?

Q: What are best practices for working with NULLs in SQL?

NULL Handling:

Query Design:

Performance:

Schema Design:

General: