Why NULL is not a value in SQL - Performance Analysis
When working with databases, NULL represents missing or unknown information, not a regular value.
We want to understand how operations involving NULL affect the time it takes to run queries.
Analyze the time complexity of a query filtering rows with NULL checks.
SELECT *
FROM employees
WHERE department_id IS NULL;
This query finds all employees without a department assigned (NULL department_id).
Look at what repeats when the database checks for NULL values.
- Primary operation: Scanning each row to test if the department_id is NULL.
- How many times: Once per row in the employees table.
As the number of employees grows, the database must check more rows for NULL.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks for NULL |
| 100 | 100 checks for NULL |
| 1000 | 1000 checks for NULL |
Pattern observation: The number of checks grows directly with the number of rows.
Time Complexity: O(n)
This means the time to find NULL values grows linearly with the number of rows.
[X] Wrong: "NULL is just another value, so checking for NULL is faster or simpler than other values."
[OK] Correct: NULL means unknown or missing, so the database must explicitly test each row, which takes time like any other check.
Understanding how NULL affects query time helps you explain database behavior clearly and shows you know how data quality impacts performance.
"What if we added an index on department_id? How would that change the time complexity when checking for NULL?"
Practice
NULL represent in a database?Solution
Step 1: Understand the meaning of NULL
NULL means that the data is missing or unknown, not a specific value like zero or empty string.Step 2: Differentiate NULL from other values
Zero, empty string, and false are actual values, but NULL means no value is present.Final Answer:
Missing or unknown data -> Option AQuick Check:
NULL means missing data = C [OK]
- Thinking NULL is zero
- Confusing NULL with empty string
- Assuming NULL is false
age is NULL?Solution
Step 1: Recall correct NULL comparison syntax
In SQL, you cannot use = or != to check for NULL because NULL is not a value.Step 2: Use IS NULL to check for NULL
The correct way is to useIS NULLto find rows where the column is NULL.Final Answer:
WHERE age IS NULL -> Option CQuick Check:
Use IS NULL, not = NULL [OK]
- Using = NULL instead of IS NULL
- Using == NULL which is invalid in SQL
- Using != NULL which does not work
users with column email, what will this query return?SELECT * FROM users WHERE email = NULL;
Solution
Step 1: Understand comparison with NULL using =
Using= NULLdoes not work because NULL is not a value and comparisons with NULL return unknown.Step 2: Result of WHERE email = NULL
The conditionemail = NULLevaluates to unknown for all rows, so no rows match and none are returned.Final Answer:
No rows -> Option BQuick Check:
email = NULL returns no rows [OK]
- Expecting rows with NULL to be returned
- Thinking = NULL works like IS NULL
- Confusing NULL with empty string
salary is NULL, but this query returns no rows:SELECT * FROM employees WHERE salary = NULL;
What is the error and how to fix it?
Solution
Step 1: Identify incorrect NULL comparison
The query uses= NULLwhich does not work because NULL is not a value and cannot be compared with =.Step 2: Correct syntax to find NULL values
UseIS NULLto check for NULL values in SQL.Final Answer:
Use IS NULL instead of = NULL -> Option AQuick Check:
IS NULL finds NULLs, = NULL does not [OK]
- Using = NULL instead of IS NULL
- Trying != NULL which also fails
- Using 0 or empty string instead of NULL
phone_number in table contacts. Which query correctly counts NULLs?Solution
Step 1: Understand counting NULLs
COUNT(column) counts only non-NULL values, so it ignores NULLs.Step 2: Use WHERE phone_number IS NULL to filter NULL rows
To count rows with NULL phone_number, filter withIS NULLand count all rows with COUNT(*)Final Answer:
SELECT COUNT(*) FROM contacts WHERE phone_number IS NULL; -> Option DQuick Check:
Use IS NULL filter and COUNT(*) to count NULL rows [OK]
- Using = NULL in WHERE clause
- Using COUNT(column) which skips NULLs
- Filtering with COUNT(phone_number) which is invalid
Ready to prove it?
Pass the test and this pattern moves unknown
