This lesson on NULL VALUES is hands-on and example-driven. You will define what a SQL NULL value represents and distinguish it from zero or an empty string. You will learn the specific operators required to accurately locate or exclude NULL values in any SQL query.
What You'll Be Able To Do
- Define the meaning of a NULL value in a database field.
- Differentiate between NULL, zero, and an empty string (space).
- Write a WHERE clause using the IS NULL operator.
- Construct a query to exclude records containing missing data.
- Explain why standard comparison operators fail with NULL values.
Topics Covered in NULL VALUES
- Introduction to NULL (0:00 - 0:15) — The lesson introduces SQL null values as a potentially mysterious topic.
- Defining NULL (0:15 - 0:35) — A null value is defined as a field with no value, like an empty box or blank space.
- NULL vs Zero or Space (0:35 - 0:55) — It is emphasized that NULL is distinct from zero or a space, representing 'I don't know what goes here'.
- How NULL is created (0:55 - 1:10) — If an optional field is skipped during insertion or update, SQL saves the value as null.
- Special Comparison Needed (1:10 - 1:30) — Standard comparison operators cannot be used to find null values, requiring special treatment.
- Using IS NULL (1:30 - 1:50) — The IS NULL operator is used in the WHERE clause to find all records where a value is missing.
- Using IS NOT NULL (1:50 - 2:10) — The IS NOT NULL operator is used to find records where the specified field contains a value.
- Summary and Reminder (2:10 - 2:30) — The lesson concludes by reminding the learner to always use IS NULL and IS NOT NULL for handling missing data.
SQL Cheat Sheet
-
NULL— Represents a field that currently holds no value -
IS NULL— Checks if a specific field contains a missing valueSELECT * FROM customers WHERE address IS NULL; -
IS NOT NULL— Checks if a specific field contains any valueSELECT * FROM customers WHERE address IS NOT NULL; -
WHERE <condition>— Filters records based on the specified criteriaSELECT * FROM customers WHERE address IS NULL;
Comparison Table
| NULL | Zero (0) | Space (' ') |
|---|---|---|
| Represents 'no value' or 'unknown' | Represents a numerical quantity | Represents an empty string value |
| Requires IS NULL/IS NOT NULL | Uses standard operators (=, <, >) | Uses standard operators (=, LIKE) |
| Applicable to all data types | Only applicable to numeric types | Only applicable to string types |
Common Pitfalls
- Mistake: Using
=or!=to check for missing data. Avoid: Always use the specificIS NULLorIS NOT NULLoperators. - Mistake: Assuming NULL is the same as an empty string or zero. Avoid: Remember NULL means "unknown" or "no value," not a quantity.
- Mistake: Forgetting to use a WHERE clause when filtering for NULLs.
Avoid: Place
IS NULLorIS NOT NULLdirectly after the WHERE keyword.
FAQs
- Can I use
=to check if a value is NOT NULL? No. Standard comparison operators fail because NULL is not a value; it's a state. You must use IS NOT NULL. - If a field is optional, does it automatically become NULL if I skip it? Yes, if the field is not defined as NOT NULL, SQL will save the record with a NULL value in that field.
- Why does NULL need special operators? NULL is treated as an unknown state, meaning it cannot logically equal or not equal any other value, including another NULL.