What if your queries silently miss important data just because you misunderstood NULL?
Why NULL in AND, OR, NOT logic in SQL? - Purpose & Use Cases
Imagine you have a list of tasks with some missing information about their status. You want to find tasks that are both urgent and completed. Without understanding how missing data (NULL) behaves, you might try to check both conditions manually.
Manually checking each task's status and urgency is slow and confusing because NULL means 'unknown'. If you treat NULL as true or false without care, you get wrong results or miss tasks. This makes your filtering unreliable and error-prone.
Understanding how NULL works with AND, OR, and NOT lets you write queries that correctly handle unknown values. SQL uses three-valued logic to decide if a condition is true, false, or unknown, so your filters work as expected even with missing data.
SELECT * FROM tasks WHERE urgent = TRUE AND completed = TRUE;
SELECT * FROM tasks WHERE urgent = TRUE AND completed IS TRUE;
You can confidently filter and combine conditions even when some data is missing, making your queries accurate and trustworthy.
A project manager wants to see all tasks that are urgent and definitely completed. Some tasks have no completion info yet (NULL). Using proper NULL logic, the manager avoids showing tasks with unknown completion status.
NULL means unknown, not true or false.
AND, OR, NOT with NULL follow special three-valued logic.
Proper handling of NULL prevents wrong query results.
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
