IS NOT NULL Operator in MySQL
Introduction
Complete records require excluding rows with missing critical data.
THE IS NOT NULL operator identifies rows with actual values present.
It is widely used for:
- Data quality filtering
- Valid record selection
- Report generation with complete data
What is the IS NOT NULL Operator
IS NOT NULL returns TRUE for columns containing actual values (not NULL).
Opposite of IS NULL, essential for data validation.
Basic Syntax
sql
SELECT column_name
FROM table_name
WHERE column_name IS NOT NULL;
Example
sql
SELECT * FROM Students
WHERE marks IS NOT NULL;
Returns only students with recorded marks.
Multiple IS NOT NULL Conditions
Ensure multiple fields have values.
sql
SELECT * FROM Students
WHERE marks IS NOT NULL, AND phone IS NOT NULL;
IS NOT NULL with AND Logic
Combine with other conditions.
sql
SELECT * FROM Students
WHERE marks IS NOT NULL AND marks > 50;
IS NOT NULL vs != NULL
Only IS NOT NULL works correctly.
sql
SELECT * FROM Students
WHERE marks IS NOT NULL; -- Correct
-- WRONG: marks != NULL; -- Returns no rows
IS NOT NULL with ORDER BY
Valid data first, then sort.
sql
SELECT * FROM Students
WHERE marks IS NOT NULL
ORDER BY marks DESC;
IS NOT NULL with GROUP BY
Aggregate only valid data.
sql
SELECT city, AVG(marks)
FROM Students
WHERE marks IS NOT NULL
GROUP BY city;
IS NOT NULL with JOIN
Ensure joined data completeness.
sql
SELECT s.name, c.course_name
FROM Students s
JOIN Courses c ON s.course_id = c.id
WHERE s.marks IS NOT NULL, AND c.duration IS NOT NULL;
IS NOT NULL Performance Advantage
Uses indexes, unlike IS NULL.
sql
-- Index-friendly (fast)
WHERE phone IS NOT NULL;
-- Full table scan (slow)
WHERE phone IS NULL;
IS NOT NULL with Empty Strings
Distinguishes NULL from empty.
sql
SELECT * FROM Students
WHERE email IS NOT NULL AND email != '';
Email exists AND has content.
IS NOT NULL in Subqueries
Filter valid data in subqueries.
sql
SELECT * FROM Students
WHERE marks > (
SELECT AVG(marks)
FROM Students
WHERE marks IS NOT NULL
);
IS NOT NULL with HAVING
Post-grouping validation.
sql
SELECT city, COUNT(*)
FROM Students
GROUP BY city
HAVING AVG(marks) IS NOT NULL;
Execution Order with IS NOT NULL
Evaluated in WHERE after FROM.
FROM → WHERE (IS NOT NULL) → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT
Performance Considerations
- IS NOT NULL uses indexes effectively
- Essential for data quality pipelines
- Combine with other indexed conditions
- Reduces aggregate computation time
Important Notes
- IS NOT NULL ≠ != NULL (latter never works)
- Empty string IS NOT NULL but =.''
- 0 IS NOT NULL (zero is a value)
- Indexes work with IS NOT NULL
- Aggregates skip NULLs automatically
Example Scenario
Generate a report with complete student data:
sql
SELECT name, city, marks, phone, email
FROM Students
WHERE marks IS NOT NULL
AND city IS NOT NULL
AND city != ''
AND phone IS NOT NULL
AND email IS NOT NULL
AND email != ''
ORDER BY marks DESC, name
LIMIT 1000;
Common Mistakes
- Using != NULL instead of IS NOT NULL
- Confusing an empty string with NULL
- IS NOT NULL on text fields, including spaces
- Performance issues from missing IS NOT NULL filters
- Forgetting NULLs affects aggregate results
Key Points to Remember
- IS NOT NULL finds actual values (not missing data)
- Uses indexes (performance advantage)
- != NULL never works (use IS NOT NULL)
- Distinguishes NULL from an empty string
- Essential for data quality validation
- Aggregates ignore NULLs automatically