Why does comparing something to 'nothing' break your queries? Discover the secret of NULL behavior!
Why NULL behavior in comparisons in SQL? - Purpose & Use Cases
Imagine you have a list of customer orders, but some orders have missing delivery dates. You try to find orders not delivered on a specific date by scanning the list manually or using simple checks.
Manually checking each order for a delivery date is slow and confusing because missing dates (NULLs) don't behave like normal values. Comparing NULLs directly often gives unexpected results, causing you to miss important orders or include wrong ones.
Understanding how NULL behaves in comparisons helps you write queries that correctly handle missing data. This way, you can accurately filter, join, or analyze data without errors or surprises.
SELECT * FROM orders WHERE delivery_date != '2024-06-01';SELECT * FROM orders WHERE delivery_date != '2024-06-01' OR delivery_date IS NULL;It enables you to handle missing or unknown data safely and get reliable query results every time.
A store wants to find all orders not delivered on June 1st, but some orders have no delivery date recorded yet. Correct NULL handling ensures these orders are not mistakenly excluded or included.
NULL means unknown or missing data, not a value.
Comparisons with NULL don't behave like normal comparisons.
Use special checks like IS NULL to handle NULLs correctly.
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
