NULL behavior in comparisons in SQL - Time & Space Complexity
When SQL compares values, NULL behaves differently than normal values. Understanding how this affects query time is important.
We want to know how the presence of NULLs impacts the time it takes to run comparison operations.
Analyze the time complexity of the following SQL query.
SELECT *
FROM employees
WHERE salary = NULL;
-- or
SELECT *
FROM employees
WHERE salary IS NULL;
This code tries to find rows where the salary is NULL using two different comparison methods.
Look at what happens for each row in the table.
- Primary operation: Checking the salary value for each row to see if it matches NULL.
- How many times: Once per row in the employees table.
For each row, the database checks the salary column once.
| 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 complete the query grows in a straight line as the number of rows increases.
[X] Wrong: "Using = NULL works the same as IS NULL in comparisons."
[OK] Correct: In SQL, = NULL never returns true because NULL means unknown. You must use IS NULL to check for NULL values correctly.
Understanding how NULL behaves in comparisons shows you know how databases handle missing or unknown data, a key skill in writing correct queries.
"What if we replaced IS NULL with IS NOT NULL? How would the time complexity change?"
Practice
NULL represent in a database?Solution
Step 1: Understand the meaning of NULL
NULL in databases means the data is missing or unknown, not zero or empty.Step 2: Differentiate NULL from other values
Zero, empty string, and false are actual values, while NULL means no value is stored.Final Answer:
Missing or unknown data -> Option DQuick Check:
NULL means missing or unknown data [OK]
- Confusing NULL with zero
- Thinking NULL is an empty string
- Assuming NULL means false
col is NULL in SQL?Solution
Step 1: Recall SQL syntax for NULL checks
In SQL, to check for NULL, you must useIS NULLbecause= NULLdoes not work.Step 2: Eliminate incorrect syntax
col = NULLandcol == NULLare invalid for NULL checks;col <> NULLalso does not work.Final Answer:
WHERE col IS NULL -> Option CQuick Check:
Use IS NULL to check NULL values [OK]
- Using = NULL instead of IS NULL
- Using == NULL which is invalid in SQL
- Using <> NULL to check for NULL
users with a column age that contains some NULL values, what will this query return?SELECT * FROM users WHERE age <> 30;
Solution
Step 1: Understand comparison with NULL
Comparisons likeage <> 30return UNKNOWN for NULL values, so those rows are excluded.Step 2: Determine which rows are returned
Only rows where age is not 30 and age is not NULL will be returned.Final Answer:
Only rows where age is not 30 and not NULL -> Option BQuick Check:
Comparisons with NULL exclude NULL rows [OK]
- Assuming NULL rows are included
- Thinking NULL causes errors in comparisons
- Confusing <> with IS NOT NULL
status is not NULL:SELECT * FROM orders WHERE status <> NULL;
Why does it return no rows, and how to fix it?
Solution
Step 1: Understand NULL comparison behavior
Comparing with NULL using<>returns UNKNOWN, so no rows match.Step 2: Use correct syntax to check NOT NULL
Usestatus IS NOT NULLto find rows where status is not NULL.Final Answer:
Comparison with NULL returns UNKNOWN; use status IS NOT NULL -> Option AQuick Check:
Use IS NOT NULL to check for non-NULL values [OK]
- Using <> NULL instead of IS NOT NULL
- Assuming <> NULL works like != NULL
- Thinking NULL is zero or empty
products where discount is either NULL or greater than 10. Which query correctly returns these rows?Solution
Step 1: Understand NULL checks with OR condition
To include NULL values, useIS NULL, not= NULL.Step 2: Combine conditions correctly
Use OR to select rows where discount is greater than 10 or discount is NULL.Final Answer:
SELECT * FROM products WHERE discount > 10 OR discount IS NULL; -> Option AQuick Check:
Use IS NULL with OR to include NULLs [OK]
- Using = NULL instead of IS NULL
- Using AND instead of OR with NULL checks
- Assuming NULL compares like normal values
Ready to prove it?
Pass the test and this pattern moves unknown
