Skip to main content

NULL Values

Learning Focus

Use this lesson to understand NULL Values with practical syntax and examples.

Concept Overview

Definition:

In MySQL, NULL represents a missing, undefined, or unknown value in a database column. It is not the same as 0, '' (empty string), or FALSE. It simply indicates that no value has been assigned.

Why It's Important:

Handling NULL values correctly is crucial because they can affect the results of queries, calculations, and comparisons. Ignoring or misusing NULL can lead to unexpected behavior or inaccurate reports.

Context in Database Operations:

NULLs are an essential part of relational database systems and are used across:

  • Data insertion and updates
  • Filtering (WHERE clause)
  • Aggregations (SUM, AVG, COUNT, etc.)
  • Joins and conditions

They help represent optional or missing data, which is common in real-world applications.


Basic Syntax & Rules

Syntax:

-- Checking for NULL
SELECT * FROM table_name WHERE column_name IS NULL;

-- Checking for NOT NULL
SELECT * FROM table_name WHERE column_name IS NOT NULL;

-- Setting a column to NULL
UPDATE table_name SET column_name = NULL WHERE id = 1;

Key Notes:

  • = and != do not work with NULL. Use IS NULL and IS NOT NULL.
  • Aggregate functions like COUNT(column_name) ignore NULLs.
  • Expressions involving NULL return NULL unless explicitly handled.

Limitations:

  • NULLs can complicate logical expressions.
  • Cannot directly compare NULLs in conditions (e.g., NULL = NULL is NULL, not TRUE).
  • Some functions or constraints may not behave as expected with NULLs unless accounted for.

Step-by-Step Examples

Example 1: Basic NULL Filter

-- Table: employees
-- Columns: id, name, manager_id

SELECT * FROM employees WHERE manager_id IS NULL;

Output:

+----+---------+------------+
| id | name | manager_id|
+----+---------+------------+
| 1 | Alice | NULL |
+----+---------+------------+

Comment: This query returns employees who don't have a manager.


Example 2: Updating Values to NULL

UPDATE employees SET manager_id = NULL WHERE name = 'Bob';

Comment: This sets Bob's manager to NULL.


Example 3: Using IFNULL() for Default Value

SELECT name, IFNULL(manager_id, 'No Manager') AS manager
FROM employees;

Output:

+--------+-------------+
| name | manager |
+--------+-------------+
| Alice | No Manager |
| Bob | 2 |
+--------+-------------+


Example 4: Aggregate Function Handling

SELECT COUNT(manager_id) FROM employees;

Comment: Only counts non-NULL manager_id values.


Example 5: Filtering with NULL in JOIN

SELECT e.name, m.name AS manager_name
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.id
WHERE m.name IS NULL;

Comment: Finds employees with no manager.


Practical Use Cases

  1. Employee Hierarchies:

    Track top-level managers where manager_id IS NULL.

  2. Optional Customer Fields:

    Filter customers who haven't provided a phone number:

    SELECT * FROM customers WHERE phone IS NULL;

  3. Product Inventory:

    Identify products not yet priced:

    SELECT * FROM products WHERE price IS NULL;

  4. Form Submissions:

    Detect partially filled records.

  5. Audit Trails:

    Find records that have not yet been updated or verified.


Common Mistakes & Troubleshooting

MistakeExplanationFix
Using = with NULLNULL = NULL returns NULL, not TRUEUse IS NULL instead
Miscounting with COUNT()COUNT(column) ignores NULLsUse COUNT(*) or IFNULL()
Logic ErrorsWHERE column != value excludes NULLsUse column != value OR column IS NULL
Joins failing silentlyNULLs don't match in JOINsUse LEFT JOIN and check for IS NULL

Best Practices

  • Always use IS NULL / IS NOT NULL for NULL comparisons.
  • Use COALESCE() or IFNULL() to replace NULLs with defaults in SELECTs.
  • Normalize your data design: Don't overuse NULLs unnecessarily; provide defaults if applicable.
  • Document NULL-ability: Ensure your schema reflects if a column allows NULLs (NOT NULL constraint).
  • Avoid NULLs in composite keys or JOIN keys when possible for better integrity and performance.

Hands-On Practice

Sample Data:

CREATE TABLE employees (
id INT,
name VARCHAR(100),
manager_id INT
);

INSERT INTO employees (id, name, manager_id) VALUES
(1, 'Alice', NULL),
(2, 'Bob', 1),
(3, 'Carol', NULL),
(4, 'David', 2);


Exercise 1: Find employees with no manager

SELECT name FROM employees WHERE manager_id IS NULL;

Answer:

Alice
Carol


Exercise 2: Count employees who have managers

SELECT COUNT(*) FROM employees WHERE manager_id IS NOT NULL;

Answer:

2


Exercise 3: Replace NULL manager_id with "Top Level"

SELECT name, IFNULL(manager_id, 'Top Level') AS manager
FROM employees;

Answer:

Alice   | Top Level
Bob | 1
Carol | Top Level
David | 2


Connection to Other Concepts

Related Concepts:

  • IS NULL / IS NOT NULL: Logical operators for NULL checks
  • COALESCE(), IFNULL(): Replace NULLs with a default
  • JOINs: Properly handling NULLs in LEFT/RIGHT JOINs
  • DEFAULT values: Prevent NULLs during inserts

Prerequisites:

  • Basic SELECT and WHERE clause
  • Understanding of table structure and data types

What to Learn Next:

  • Constraints (NOT NULL, DEFAULT)
  • Triggers to auto-fill missing values
  • Advanced aggregate handling (e.g., GROUP BY with NULLs)

Concept Map

flowchart LR
A[Schema Context] --> B[NULL Values]
B --> C[Query Pattern]
C --> D[Validation]
D --> E[Production Use]

Common Pitfalls

PitfallConsequencePrevention
Executing queries without validating sample rowsLogic errors reach production data or reportsStart with SELECT ... LIMIT 10 and inspect edge cases
Ignoring NULL and duplicate behaviorAggregations and filters return misleading resultsTest with NULL, duplicates, and empty sets explicitly
Using advanced syntax before checking schemaQueries fail due to missing columns/indexesVerify structure with DESCRIBE table_name; and adapt query design

Quick Reference

SELECT * FROM table_name WHERE column_name IS NULL;
SELECT * FROM table_name WHERE column_name IS NOT NULL;
SELECT COALESCE(column_name, "fallback") FROM table_name;

What's Next