â The IS NOT NULL operator is used to check whether a column contains a value. It returns rows where the specified column is not NULL, making it useful for filtering records that contain valid or available data.
đ What is IS NOT NULL?
In SQL, NULL represents a missing, unknown, or undefined value. The IS NOT NULL operator identifies rows where a column contains an actual value instead of NULL. Since NULL cannot be compared using operators such as = or <>, SQL provides the special operators IS NULL and IS NOT NULL.
Information
đ¯ Why Use IS NOT NULL?
The IS NOT NULL operator helps retrieve records that contain meaningful data.
- đ Find complete records.
- đ Exclude missing or unknown values.
- đ Validate required information.
- đ Improve report accuracy.
- đ Filter data before calculations and analysis.
đ Sample Table
| EmployeeID | EmployeeName | ManagerID | |
|---|---|---|---|
| 101 | Alice | alice@example.com | 201 |
| 102 | Bob | NULL | 201 |
| 103 | Charlie | charlie@example.com | NULL |
đ Basic Syntax
IS NOT NULL Syntax
SELECT column_name
FROM table_name
WHERE column_name IS NOT NULL;đĄ Example: Find Employees with an Email Address
Using IS NOT NULL
SELECT EmployeeID, EmployeeName
FROM Employees
WHERE Email IS NOT NULL;This query returns only employees whose Email column contains a value.
đĄ Example: Find Employees with an Assigned Manager
Find Non-NULL Manager IDs
SELECT EmployeeName
FROM Employees
WHERE ManagerID IS NOT NULL;đĢ Incorrect Way to Compare NULL
The following query does not correctly identify rows with values.
Incorrect NULL Comparison
SELECT *
FROM Employees
WHERE Email <> NULL;Warning
đ Example Output
| EmployeeID | EmployeeName | |
|---|---|---|
| 101 | Alice | alice@example.com |
| 103 | Charlie | charlie@example.com |
âī¸ IS NULL vs IS NOT NULL
| Operator | Purpose |
|---|---|
| IS NULL | Finds rows where the column has no value. |
| IS NOT NULL | Finds rows where the column contains a value. |
đŧ Real-World Examples
- đ§ Retrieve customers who have provided an email address.
- đ¤ Find employees assigned to a manager.
- đĻ Display orders that have a shipping date.
- đĨ List patients with emergency contact information.
- đ Generate reports using only complete records.
đī¸ Database Compatibility
| Database System | IS NOT NULL Support |
|---|---|
| MySQL | â Fully supported. |
| PostgreSQL | â Fully supported. |
| SQL Server | â Fully supported. |
| Oracle | â Fully supported. |
| SQLite | â Fully supported. |
â ī¸ Common Mistakes
- â Using <> NULL instead of IS NOT NULL.
- â Assuming an empty string is always the same as NULL.
- â Forgetting that NULL values are ignored by many aggregate functions.
- â Ignoring NULL checks when validating required data.
Best Practice
đ Key Points to Remember
- đ IS NOT NULL returns rows containing actual values.
- đ It excludes rows where the column is NULL.
- đ Never use <> NULL to test for non-NULL values.
- đ NULL is different from an empty string or zero.
- đ Use IS NULL and IS NOT NULL for reliable NULL handling.
- đ Proper NULL checks improve data quality and query accuracy.