Skip to main content
Documents
Share
LinkedIn
Facebook
Twitter
Copy
ClausesStatementsKeywordsExpressionsFunctionsIndexesPerformance Optimization

MySQL IS NULL Keyword

The IS NULL keyword in MySQL is used to check whether a column contains a NULL value. It is often employed in conditional statements to filter or identify rows with missing data.

Usage

The IS NULL keyword is used in SELECT, UPDATE, or DELETE statements to test for NULL values in a specified column. It helps in handling data that might be incomplete or missing.

sql
SELECT column1, column2, ...
FROM table_name
WHERE column_name IS NULL;

In this syntax, column_name IS NULL checks whether column_name contains a NULL value.

Examples

1. Basic NULL Check

sql
SELECT *
FROM products
WHERE price IS NULL;

This example retrieves all records from the products table where the price column is NULL.

2. Updating NULL Values

sql
UPDATE employees
SET department = 'Unassigned'
WHERE department IS NULL;

Here, any employee records with a NULL department are updated to 'Unassigned'.

3. Deleting Rows with NULL

sql
DELETE FROM orders
WHERE shipment_date IS NULL;

This command removes all entries from the orders table where the shipment date is missing (NULL).

4. Combining IS NULL and IS NOT NULL

sql
SELECT *
FROM orders
WHERE shipment_date IS NULL OR delivery_date IS NOT NULL;

This example selects records where the shipment date is NULL or the delivery date is not NULL, illustrating the combined use of IS NULL and IS NOT NULL.

Tips and Best Practices

  • Use IS NULL for precision. Always use IS NULL instead of = NULL to ensure accurate checking for NULL values. Using = NULL will not yield correct results because, in SQL, NULL represents an unknown value and cannot be equated.
  • Consider data integrity. Use IS NULL to find and handle incomplete data, ensuring data integrity and consistency.
  • Combine with IS NOT NULL. Pair IS NULL with IS NOT NULL to manage both missing and non-missing data effectively.
  • Index consideration. Remember that NULL values are not indexed in MySQL, which can lead to full table scans and affect query performance. Be mindful of this, especially in large datasets.