0
0
SQLquery~10 mins

NULL behavior in comparisons in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - NULL behavior in comparisons
Start Comparison
Is any operand NULL?
NoEvaluate comparison normally
Yes
Result is UNKNOWN (treated as FALSE in WHERE)
Use IS NULL or IS NOT NULL to check NULL explicitly
When comparing values, if any side is NULL, the result is UNKNOWN, not TRUE or FALSE. Use IS NULL or IS NOT NULL to check for NULL explicitly.
Execution Sample
SQL
SELECT * FROM users WHERE age = NULL;
SELECT * FROM users WHERE age IS NULL;
Shows that 'age = NULL' returns no rows, but 'age IS NULL' returns rows where age is NULL.
Execution Table
StepExpressionOperandsEvaluationResult
1age = 25age=25, 2525 = 25TRUE
2age = NULLage=25, NULL25 = NULLUNKNOWN (treated as FALSE)
3age = NULLage=NULL, NULLNULL = NULLUNKNOWN (treated as FALSE)
4age IS NULLage=NULLCheck if age is NULLTRUE
5age IS NULLage=25Check if age is NULLFALSE
6age <> NULLage=25, NULL25 <> NULLUNKNOWN (treated as FALSE)
7age IS NOT NULLage=25Check if age is NOT NULLTRUE
8age IS NOT NULLage=NULLCheck if age is NOT NULLFALSE
💡 Comparisons with NULL result in UNKNOWN, which acts like FALSE in WHERE filters; use IS NULL or IS NOT NULL for explicit NULL checks.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8
age25 or NULL25 or NULL25 or NULL25 or NULL25 or NULL25 or NULL25 or NULL25 or NULL
comparison_resultN/AUNKNOWNUNKNOWNTRUE or FALSETRUE or FALSEUNKNOWNTRUE or FALSETRUE or FALSE
Key Moments - 3 Insights
Why does 'age = NULL' not return rows even if some ages are NULL?
Because any comparison with NULL results in UNKNOWN, which is treated as FALSE in WHERE clauses. See execution_table rows 2 and 3.
How can I correctly check if a column is NULL?
Use 'IS NULL' or 'IS NOT NULL' operators, which explicitly test for NULL values. See execution_table rows 4 and 5.
Is NULL equal to NULL in SQL?
No, 'NULL = NULL' results in UNKNOWN, not TRUE. Use 'IS NULL' to check for NULL. See execution_table row 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of 'age = NULL' when age is 25?
ATRUE
BFALSE
CUNKNOWN
DNULL
💡 Hint
Check row 2 in the execution_table where age=25 and comparison is 'age = NULL'.
At which step does the expression correctly identify NULL values?
AStep 2
BStep 4
CStep 6
DStep 7
💡 Hint
Look for 'IS NULL' usage in the execution_table.
If you replace 'age = NULL' with 'age IS NULL', how does the result change?
AFrom UNKNOWN to TRUE
BFrom TRUE to FALSE
CFrom UNKNOWN to TRUE or FALSE
DNo change
💡 Hint
Compare rows 2, 3 with rows 4, 5 in the execution_table.
Concept Snapshot
NULL in SQL means unknown value.
Comparisons with NULL return UNKNOWN, not TRUE or FALSE.
WHERE treats UNKNOWN as FALSE, so no rows match.
Use IS NULL or IS NOT NULL to check for NULL explicitly.
NULL = NULL is UNKNOWN, not TRUE.
Full Transcript
In SQL, NULL represents an unknown or missing value. When you compare any value to NULL using standard operators like '=', '<>', or '<', the result is not TRUE or FALSE but UNKNOWN. This UNKNOWN behaves like FALSE in WHERE clauses, so no rows match such conditions. To properly check if a column contains NULL, you must use the IS NULL or IS NOT NULL operators. For example, 'age = NULL' returns no rows even if some ages are NULL, but 'age IS NULL' returns those rows. Also, NULL is not equal to NULL; 'NULL = NULL' is UNKNOWN. Remember to use IS NULL for explicit NULL checks.