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

EmployeeIDEmployeeNameEmailManagerID
101Alicealice@example.com201
102BobNULL201
103Charliecharlie@example.comNULL

πŸ“ 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

EmployeeIDEmployeeName
102Bob

βš–οΈ IS NULL vs IS NOT NULL

OperatorPurpose
IS NULLFinds rows where the column has no value.
IS NOT NULLFinds 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 SystemIS 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.