How does SQL Server handle NULL values in comparisons?
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Pankajkumar PatelRank #181 · 13.4K reputationPosted Aug 13, 2025, 2:12 AM EDT
Hi Sandhiya Priya,
In SQL Server, NULL represents unknown or missing value. It behaves differently in comparisons than regular values.
Comparison with NULL using operators such as =, <>, <, >, etc. will returns UNKNOWN. It will not return TRUE or FALSE.
SELECT CASE WHEN NULL = NULL THEN 'True' ELSE 'False' END AS Result;Output:
Using IS NULL and IS NOT NULL is the only reliable ways to test for NULL
WHERE column IS NULL WHERE column IS NOT NULLHope this will help you!
Uttam ChaturvediRank #477 · 5.2K reputationPosted Aug 12, 2025, 1:10 PM EDT
SQL Server, NULL represents an unknown or missing value, and it behaves differently from regular values.
SELECT * FROM Student WHERE StudentID = NULL;
You might expect it to find rows where StudentID is NULL — but it won’t return anything.
That’s because NULL = NULL is not TRUE; it’s UNKNOWN (since you can’t be sure two unknowns are equal).
So the correct way to check null values is
IS NULL -- to check if a value is NULL
IS NOT NULL -- to check if a value is not NULL
Example:
SELECT * FROM Student WHERE StudentID IS NULL;