NULL in AND, OR, NOT logic in SQL - Time & Space Complexity
We want to understand how SQL evaluates logical expressions involving NULL values.
How does the presence of NULL affect the number of checks the database performs?
Analyze the time complexity of evaluating logical expressions with NULL.
SELECT * FROM table_name
WHERE (column1 = 10 AND column2 IS NULL)
OR (column3 > 5 AND NOT (column4 = 20));
This query filters rows based on combined AND, OR, and NOT conditions that include NULL checks.
Look at what happens for each row in the table.
- Primary operation: Evaluating logical conditions (AND, OR, NOT) including NULL checks.
- How many times: Once per row, for each condition in the WHERE clause.
Each row requires checking all conditions once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 sets of condition checks |
| 100 | About 100 sets of condition checks |
| 1000 | About 1000 sets of condition checks |
Pattern observation: The number of checks grows directly with the number of rows.
Time Complexity: O(n)
This means the time to evaluate the query grows linearly with the number of rows.
[X] Wrong: "NULL in logical expressions causes the query to run slower exponentially."
[OK] Correct: Each row is checked once; NULL just affects the logic result, not the number of checks.
Understanding how NULL affects logical checks helps you reason about query performance and correctness in real databases.
"What if the query had nested subqueries with NULL checks? How would that affect time complexity?"
Practice
TRUE AND NULL?Solution
Step 1: Understand AND with NULL
In SQL,ANDreturnsFALSEif any side isFALSE. If one side isTRUEand the other isNULL, the result isNULLbecause the unknown value affects the outcome.Step 2: Apply to
Since the first operand isTRUE AND NULLTRUEand the second isNULL, the result isNULL.Final Answer:
NULL -> Option BQuick Check:
TRUE AND NULL = NULL [OK]
- Assuming TRUE AND NULL is TRUE
- Confusing NULL with FALSE
- Thinking NULL causes error
col is NOT NULL?Solution
Step 1: Recall NULL comparison rules
In SQL, you cannot use=or!=to compare withNULL. Instead, useIS NULLorIS NOT NULL.Step 2: Identify correct syntax
The correct way to check ifcolis not null iscol IS NOT NULL.Final Answer:
col IS NOT NULL -> Option CQuick Check:
Use IS NOT NULL for null checks [OK]
- Using != or <> to compare with NULL
- Using NOT col = NULL which is invalid
- Confusing NULL with empty string
Test with a column flag that can be TRUE, FALSE, or NULL, what is the result of this query?SELECT * FROM Test WHERE flag OR FALSE;
Solution
Step 1: Understand OR with FALSE
In SQL,OR FALSEreturns the value of the first operand because FALSE does not affect the OR result.Step 2: Evaluate
Ifflag OR FALSEflagis TRUE, the expression is TRUE. Ifflagis FALSE, expression is FALSE. Ifflagis NULL, expression is NULL (unknown), which is treated as FALSE in WHERE filtering.Final Answer:
Rows where flag is TRUE -> Option AQuick Check:
flag OR FALSE = flag; WHERE filters TRUE only [OK]
- Including NULL rows in result
- Thinking OR FALSE always returns TRUE
- Confusing OR with AND behavior
WHERE NOT (col = NULL)
What is the problem with this condition?
Solution
Step 1: Understand comparison with NULL
In SQL,col = NULLalways returnsNULL(unknown), never TRUE or FALSE.Step 2: Apply NOT to NULL
NOT NULLis stillNULL, so the condition never evaluates to TRUE, filtering out all rows.Final Answer:
It always returns NULL (no rows match) -> Option AQuick Check:
NOT (col = NULL) = NULL, no matches [OK]
- Expecting NOT NULL to be TRUE
- Using = NULL instead of IS NULL
- Assuming syntax error occurs
Employees where the ManagerID is either NULL or the employee is active (Active = TRUE). Which WHERE clause correctly handles NULL logic?Solution
Step 1: Correctly check for NULL
UseIS NULLto check ifManagerIDis NULL. Using= NULLis invalid and returns NULL.Step 2: Combine conditions with OR
The condition requires rows whereManagerIDis NULL orActiveis TRUE, so useORbetween these checks.Final Answer:
WHERE ManagerID IS NULL OR Active = TRUE -> Option DQuick Check:
Use IS NULL and OR for correct logic [OK]
- Using = NULL instead of IS NULL
- Using AND instead of OR
- Using IS NOT NULL which is opposite
Ready to prove it?
Pass the test and this pattern moves unknown
