IS NULL in SQL
π The IS NULL operator is used to check whether a column contains a NULL value. Since NULL represents missing, unknown, or undefined data, it cannot be compared using standard comparison operators such as = or <>.
π What is NULL?
A NULL value indicates that a column has no value assigned. It is different from an empty string ( ''), the number 0, or the Boolean value FALSE.
Information
Because NULL represents an unknown value, expressions such as column = NULL or column <> NULL do not work as expected. Use IS NULL or IS NOT NULL instead.
π― Why Use IS NULL?
The IS NULL operator helps identify records with missing or undefined values.
- π Find incomplete records.
- π Validate imported data.
- π Filter missing information.
- π Improve data quality checks.
- π Support reporting and data cleanup.
π Sample Table
| EmployeeID | EmployeeName | ManagerID | |
|---|---|---|---|
| 101 | Alice | alice@example.com | 201 |
| 102 | Bob | NULL | 201 |
| 103 | Charlie | charlie@example.com | NULL |
π Basic Syntax
IS NULL Syntax
SELECT column_name
FROM table_name
WHERE column_name IS NULL;π‘ Example: Find Employees Without an Email
Using IS NULL
SELECT EmployeeID, EmployeeName
FROM Employees
WHERE Email IS NULL;This query returns employees whose Email column has no value.
π‘ Example: Find Employees Without a Manager
Find NULL Manager IDs
SELECT EmployeeName
FROM Employees
WHERE ManagerID IS NULL;π« Incorrect Way to Compare NULL
The following query does not correctly find NULL values.
Incorrect NULL Comparison
SELECT *
FROM Employees
WHERE Email = NULL;Warning
Never compare NULL using = or <>. Always use IS NULL or IS NOT NULL.
π Example Output
| EmployeeID | EmployeeName |
|---|---|
| 102 | Bob |
βοΈ 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
- π§ Find customers who have not provided an email address.
- π€ Identify employees without assigned managers.
- π¦ Locate orders without a shipping date.
- π₯ Detect patient records missing emergency contact information.
- π Audit incomplete business data.
ποΈ Database Compatibility
| Database System | IS 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 NULL.
- β Assuming an empty string and NULL are always the same.
- β Ignoring NULL values in reports and calculations.
- β Forgetting that aggregate functions often ignore NULL values.
Best Practice
Use IS NULL whenever you need to identify missing values. Define appropriate NOT NULL constraints where data is mandatory, and regularly check for unexpected NULL values to improve data quality.
π Key Points to Remember
- π NULL represents missing or unknown data.
- π Use IS NULL to find rows with NULL values.
- π Do not use = NULL or <> NULL.
- π IS NOT NULL finds rows containing actual values.
- π NULL is different from an empty string or zero.
- π Proper NULL handling is essential for accurate SQL queries.
>>"NULL doesn't mean 'nothing'βit means the value is unknown or missing."
Summary
β
The IS NULL operator is the standard SQL way to identify missing values in a database. It plays a critical role in data validation, reporting, and filtering by allowing you to accurately detect records that contain undefined or unavailable information.