WHERE with IS NULL and IS NOT NULL in SQL - Time & Space Complexity
We want to understand how the time to run a query changes when we use conditions that check for NULL values.
Specifically, how does checking for NULL or NOT NULL affect the work the database does?
Analyze the time complexity of the following code snippet.
SELECT *
FROM employees
WHERE manager_id IS NULL;
SELECT *
FROM employees
WHERE manager_id IS NOT NULL;
This code finds all employees who either have no manager or do have a manager.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The database scans each row in the employees table to check the manager_id column.
- How many times: Once for every row in the table.
As the number of employees grows, the database must check more rows for NULL or NOT NULL.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of rows.
Time Complexity: O(n)
This means the time to run the query grows in a straight line as the table gets bigger.
[X] Wrong: "Checking for NULL is faster because NULL is special and fewer rows match."
[OK] Correct: The database still looks at every row to decide if the value is NULL or not, so it does the same amount of work.
Understanding how conditions like IS NULL affect query time helps you explain how databases handle data filtering efficiently.
"What if we add an index on the manager_id column? How would the time complexity change?"
Practice
email column has no value stored (is missing)?Solution
Step 1: Understand NULL meaning in SQL
NULL means no value or missing data, not an empty string or zero.Step 2: Use correct syntax to check NULL
To check for NULL, useIS NULL, not= NULLor empty string.Final Answer:
WHERE email IS NULL -> Option CQuick Check:
Check NULL with IS NULL = WHERE email IS NULL [OK]
- Using = NULL instead of IS NULL
- Confusing empty string '' with NULL
- Using IS NOT NULL to find missing values
phone column is NOT missing?Solution
Step 1: Recall correct syntax for NOT NULL check
SQL usesIS NOT NULLto find rows where value exists.Step 2: Identify syntax errors in other options
Options B, C, D use invalid syntax like = NOT NULL or != NULL or missing IS.Final Answer:
SELECT * FROM contacts WHERE phone IS NOT NULL; -> Option AQuick Check:
Use IS NOT NULL to find existing values = SELECT * FROM contacts WHERE phone IS NOT NULL; [OK]
- Using = NOT NULL instead of IS NOT NULL
- Using != NULL which is invalid
- Omitting IS keyword
employees with columns id, name, and manager_id where some employees have no manager (NULL), what will this query return?SELECT name FROM employees WHERE manager_id IS NULL;
Solution
Step 1: Understand IS NULL condition
The conditionmanager_id IS NULLfilters rows wheremanager_idhas no value (missing).Step 2: Interpret query result
The query returns names of employees without any manager assigned (NULL manager_id).Final Answer:
All employees who do not have a manager assigned -> Option BQuick Check:
IS NULL filters missing values = All employees who do not have a manager assigned [OK]
- Thinking IS NULL finds rows with values
- Confusing NULL with empty string
- Assuming syntax error with IS NULL
address is NOT NULL:SELECT * FROM customers WHERE address != NULL;
Solution
Step 1: Check NULL comparison syntax
In SQL, NULL cannot be compared with = or != operators.Step 2: Use correct syntax for NOT NULL
The correct way isIS NOT NULLto check for non-missing values.Final Answer:
Using != NULL is invalid; should use IS NOT NULL -> Option DQuick Check:
Use IS NOT NULL, not != NULL [OK]
- Using != NULL or = NULL
- Adding quotes around NULL
- Assuming != NULL works like other values
orders with columns order_id, customer_id, and shipped_date. Some orders are not shipped yet and have shipped_date as NULL. Which query lists all orders that have been shipped?Solution
Step 1: Understand meaning of shipped_date NULL
NULL means order not shipped yet, so shipped orders have non-NULL dates.Step 2: Use correct syntax to find shipped orders
UseIS NOT NULLto find rows whereshipped_datehas a value.Final Answer:
SELECT * FROM orders WHERE shipped_date IS NOT NULL; -> Option AQuick Check:
IS NOT NULL finds shipped orders = SELECT * FROM orders WHERE shipped_date IS NOT NULL; [OK]
- Using = NULL or != NULL which are invalid
- Using IS NULL to find shipped orders
- Confusing NULL with empty string
Ready to prove it?
Pass the test and this pattern moves unknown
