IS NULL and IS NOT NULL in MySQL - Time & Space Complexity
Checking for NULL values in a database is common when filtering data.
We want to understand how the time to run these checks grows as the data size increases.
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 do not have a manager (NULL) and those who do have a manager (NOT NULL).
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 per row, so as many times as there are rows in the table.
As the number of employees grows, the database must check more rows for NULL or NOT NULL values.
| 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 check for NULL or NOT NULL grows linearly with the number of rows.
[X] Wrong: "Checking for NULL is instant and does not depend on table size."
[OK] Correct: Each row must be checked individually, so more rows mean more work.
Understanding how simple filters like IS NULL scale helps you explain query performance clearly and confidently.
"What if the column checked with IS NULL has an index? How would the time complexity change?"
Practice
column_name IS NULL check for in a table?Solution
Step 1: Understand the meaning of NULL in SQL
NULL means the value is missing or unknown, not zero or empty string.Step 2: Analyze the condition
This condition returns true only when the column has no value (NULL).IS NULLFinal Answer:
It checks if the column value is missing or unknown. -> Option AQuick Check:
IS NULL means missing value check [OK]
- Confusing NULL with zero or empty string
- Using = NULL instead of IS NULL
- Thinking NULL means zero
email is NOT NULL?Solution
Step 1: Recall correct syntax for NOT NULL check
In SQL, to check for NOT NULL, useIS NOT NULL.Step 2: Evaluate each option
SELECT * FROM users WHERE email IS NOT NULL; uses correct syntax. Others use invalid or incorrect operators with NULL.Final Answer:
SELECT * FROM users WHERE email IS NOT NULL; -> Option AQuick Check:
Use IS NOT NULL to check non-missing values [OK]
- Using = NULL or != NULL instead of IS NULL/IS NOT NULL
- Forgetting IS keyword
- Using NOT email = NULL which is invalid
orders with columns order_id and shipped_date, what will this query return?SELECT order_id FROM orders WHERE shipped_date IS NULL;
Solution
Step 1: Understand what IS NULL filters
The conditionshipped_date IS NULLselects rows where shipped_date is missing.Step 2: Interpret the meaning in context
Missing shipped_date means the order has not been shipped yet.Final Answer:
All orders that have not been shipped yet. -> Option BQuick Check:
IS NULL finds missing shipped dates = unshipped orders [OK]
- Thinking IS NULL returns shipped orders
- Confusing NULL with zero or empty string
- Assuming syntax error with IS NULL
SELECT * FROM employees WHERE manager_id = NULL;
What is the error and how to fix it?
Solution
Step 1: Identify incorrect NULL comparison
Using= NULLalways returns false because NULL is not equal to anything.Step 2: Correct syntax to check NULL
UseIS NULLto check for NULL values properly.Final Answer:
Use IS NULL instead of = NULL to check for NULL values. -> Option CQuick Check:
Use IS NULL, not = NULL, for NULL checks [OK]
- Using = NULL or != NULL instead of IS NULL/IS NOT NULL
- Adding quotes around NULL
- Confusing IS NULL with IS NOT NULL
IS NULL and IS NOT NULL to do this?SELECT * FROM customers WHERE phone IS NOT NULL AND email IS NULL;
Solution
Step 1: Analyze the conditions
phone IS NOT NULLselects customers who provided a phone number.Step 2: Check the email condition
email IS NULLselects customers who did not provide an email.Step 3: Combine conditions with AND
Both conditions together find customers with phone but no email.Final Answer:
This query returns customers with phone and missing email. -> Option DQuick Check:
IS NOT NULL and IS NULL combined filter correctly [OK]
- Mixing AND with OR incorrectly
- Using = NULL instead of IS NULL
- Confusing which column to check for NULL
Ready to prove it?
Pass the test and this pattern moves unknown
