· SQL Guroo · SQL Tutorial · 3 min read
Handling NULL Values in SQL - A Practical Guide
Learn how to handle NULL values in SQL with simple, practical examples.
Introduction
NULL in SQL represents missing or unknown data. It’s not zero, empty string, or blank space - it’s the absence of a value.
Why NULL Handling Matters?
It’s important because:
- Data is often incomplete
- NULLs affect calculations
- NULLs need special handling
- Avoiding NULL errors
Main Content: Handling NULL Values
Let’s use this simple table:
-- Create a simple table
CREATE TABLE employee_data (
employee_name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10,2),
bonus DECIMAL(10,2)
);
-- Insert sample data
INSERT INTO employee_data VALUES
('Alice', 'Sales', 50000, 5000),
('Bob', 'Sales', 45000, NULL),
('Charlie', 'IT', 60000, 6000),
('David', 'IT', 55000, NULL),
('Eve', NULL, 48000, 4000);
Example 1: COALESCE Function
Let’s handle NULL bonuses with a default value:
SELECT
employee_name,
department,
salary,
COALESCE(bonus, 0) as bonus,
salary + COALESCE(bonus, 0) as total_compensation
FROM employee_data
ORDER BY total_compensation DESC;
This query:
- Replaces NULL bonus with 0
- Calculates total compensation
- Shows all employees’ earnings
Results:
employee_name | department | salary | bonus | total_compensation
--------------|-----------|---------|--------|-------------------
Charlie | IT | 60000 | 6000 | 66000
David | IT | 55000 | 0 | 55000
Alice | Sales | 50000 | 5000 | 55000
Eve | NULL | 48000 | 4000 | 52000
Bob | Sales | 45000 | 0 | 45000
Example 2: IFNULL/IS NULL
Let’s find employees with missing information:
SELECT
employee_name,
IFNULL(department, 'Unassigned') as department,
CASE
WHEN bonus IS NULL THEN 'No Bonus'
ELSE 'Has Bonus'
END as bonus_status
FROM employee_data
WHERE department IS NULL
OR bonus IS NULL;
This query:
- Shows employees with NULL values
- Replaces NULL department
- Indicates bonus status
Results:
employee_name | department | bonus_status
--------------|------------|-------------
Bob | Sales | No Bonus
David | IT | No Bonus
Eve | Unassigned | Has Bonus
Example 3: NULL in Aggregations
Let’s analyze department statistics:
SELECT
COALESCE(department, 'Unassigned') as department,
COUNT(*) as total_employees,
COUNT(bonus) as employees_with_bonus,
ROUND(AVG(COALESCE(bonus, 0)), 2) as avg_bonus
FROM employee_data
GROUP BY department
ORDER BY department;
This query shows:
- Employee count per department
- How many have bonuses
- Average bonus (including zeros)
Results:
department | total_employees | employees_with_bonus | avg_bonus
------------|----------------|---------------------|----------
IT | 2 | 1 | 3000.00
Sales | 2 | 1 | 2500.00
Unassigned | 1 | 1 | 4000.00
Best Practices
Using COALESCE
- Provide meaningful defaults
- Handle multiple NULL possibilities
- Use in calculations
- Keep default values consistent
NULL Comparisons
- Use IS NULL / IS NOT NULL
- Never use = NULL
- Consider NULL in WHERE clauses
- Handle NULL in joins
Aggregation Functions
- Understand NULL behavior
- Use COUNT(*) vs COUNT(column)
- Consider NULL in averages
- Handle GROUP BY with NULLs
Common Uses
- Default values
- Missing data handling
- Conditional logic
- Report generation
Things to Remember
- NULL ≠ zero or empty string
- NULL in calculations gives NULL
- COUNT(*) includes NULLs
- COUNT(column) excludes NULLs
Remember:
- Use COALESCE for defaults
- IS NULL for comparisons
- Handle NULLs in calculations
- Consider NULL impact on results
Common Mistakes to Avoid:
- Using = NULL
- Forgetting NULL in conditions
- Ignoring NULL in calculations
- Wrong aggregation handling
Happy querying! 🚀