5.2 — NULL Value Solutions
What You'll Learn
- What
NULL
means in SQL - How to filter, replace, and work with missing values
- Excel blanks vs. SQL
NULL
What is NULL?
In SQL, NULL
means missing or unknown data—similar to a blank cell in Excel.
Filtering for NULL Values
Find rows with missing Amount:
sql
SELECT *
FROM sales
WHERE Amount IS NULL;
Exclude rows with missing Amount:
sql
SELECT *
FROM sales
WHERE Amount IS NOT NULL;
Replacing NULL Values
Use COALESCE()
to provide a default value if a column is NULL
:
sql
SELECT Product, COALESCE(Amount, 0) AS Amount_Filled
FROM sales;
- This will show
0
instead ofNULL
for missing Amounts.
Using CASE for NULL Handling
You can also use CASE
to handle NULL
:
sql
SELECT Product,
CASE
WHEN Amount IS NULL THEN 'Missing'
ELSE CAST(Amount AS VARCHAR)
END AS Amount_Status
FROM sales;
Key Points
NULL
is not the same as zero or an empty string.- Use
IS NULL
/IS NOT NULL
to filter. - Use
COALESCE()
orCASE
to replace or flag missing values.
Next Steps
Next, you’ll learn about subqueries—using queries inside other queries for dynamic filtering.