Paramclasses Academy - Blog

Tutorials Details

←
Post 16 of 40
→

SQL Server NULL Values Explained: IS NULL, IS NOT NULL and COALESCE with Examples

SQL Server NULL Values Explained: IS NULL, IS NOT NULL and COALESCE with Examples

November 17, 2026

SQL Server NULL Values Explained: IS NULL, IS NOT NULL and COALESCE with Examples

In Microsoft SQL Server, NULL represents a missing, unknown, or unavailable value in a database column. When working with real-world databases, some records may not contain complete information. For example, a student's phone number may not be provided, an employee's joining date may be unknown, or a customer's email address may not be available.

SQL Server handles these missing values differently from ordinary data. You cannot reliably compare NULL using the standard equality operator (=). Instead, SQL Server provides IS NULL, IS NOT NULL, and COALESCE() to check and handle missing values.

In this tutorial by Param Classes, you will learn how NULL values work in SQL Server, how to filter records containing NULL or non-NULL values, and how to replace missing values using COALESCE() with practical SQL examples.

1. What Is NULL in SQL Server?

NULL indicates that a value is missing, unknown, or not available. It is not the same as zero, an empty string, or a blank space.

Consider an Employees table:

EmployeeID EmployeeName PhoneNumber Salary
1 Rahul 9876543210 30000
2 Priya NULL 35000
3 Amit 9123456780 28000
4 Neha NULL 32000

In this table, Priya and Neha do not have phone numbers stored in the database. Their PhoneNumber values are NULL.

Important Difference Between NULL and Other Values

Value Meaning
NULL Missing or unknown value
0 Numeric zero
'' Empty string
' ' String containing a space

Important: NULL does not mean that a column contains zero or an empty string. It represents the absence of a known value.

2. How to Create a Sample Table

Before learning NULL conditions, let's create a sample Employees table and insert some records.

CREATE TABLE Employees
(
    EmployeeID INT PRIMARY KEY,
    EmployeeName VARCHAR(50),
    Email VARCHAR(100),
    PhoneNumber VARCHAR(15),
    Salary DECIMAL(10,2)
);

Insert sample records:

INSERT INTO Employees
(EmployeeID, EmployeeName, Email, PhoneNumber, Salary)
VALUES
(1, 'Rahul', 'rahul@gmail.com', '9876543210', 30000),
(2, 'Priya', 'priya@gmail.com', NULL, 35000),
(3, 'Amit', NULL, '9123456780', 28000),
(4, 'Neha', 'neha@gmail.com', NULL, 32000),
(5, 'Rohit', NULL, '9988776655', 40000);

View all records:

SELECT * FROM Employees;

Result: The table now contains five employees, including records with missing email addresses and phone numbers.

3. What Is IS NULL in SQL Server?

The IS NULL operator is used to find records where a column contains NULL values.

Syntax

SELECT column_name
FROM table_name
WHERE column_name IS NULL;

Example: Find Employees Without Phone Numbers

SELECT *
FROM Employees
WHERE PhoneNumber IS NULL;

Explanation:

  • SELECT * retrieves all columns.

  • FROM Employees specifies the table.

  • WHERE PhoneNumber IS NULL selects only employees whose phone number is missing.

Expected Result:

EmployeeID EmployeeName PhoneNumber
2 Priya NULL
4 Neha NULL

Find Employees Without Email Addresses

SELECT *
FROM Employees
WHERE Email IS NULL;

This query returns employees whose email address has not been provided.

Remember: Use IS NULL to check for missing values. Do not use = NULL.

4. What Is IS NOT NULL in SQL Server?

The IS NOT NULL condition is used to retrieve records where a column contains an actual, non-NULL value.

Syntax

SELECT column_name
FROM table_name
WHERE column_name IS NOT NULL;

Example: Find Employees Who Have Phone Numbers

SELECT *
FROM Employees
WHERE PhoneNumber IS NOT NULL;

Explanation: SQL Server returns only those employee records where PhoneNumber contains a value.

Find Employees Who Have Email Addresses

SELECT *
FROM Employees
WHERE Email IS NOT NULL;

This query filters out records with missing email addresses.

IS NULL vs IS NOT NULL

IS NULL IS NOT NULL
Finds missing values Finds available values
Checks whether a value is NULL Checks whether a value is not NULL
Returns rows with NULL Returns rows with non-NULL values

5. Why Should You Not Use = NULL?

A common beginner mistake is writing:

SELECT *
FROM Employees
WHERE PhoneNumber = NULL;

This is not the correct way to find NULL values in SQL Server.

SQL uses three-valued logic. A comparison involving NULL generally evaluates to UNKNOWN rather than TRUE or FALSE. Therefore, the query above will not return the expected NULL rows.

Correct query:

SELECT *
FROM Employees
WHERE PhoneNumber IS NULL;

For non-NULL values, use:

SELECT *
FROM Employees
WHERE PhoneNumber IS NOT NULL;

Important Tip: Always use IS NULL and IS NOT NULL when checking for NULL values.

6. What Is COALESCE() in SQL Server?

The COALESCE() function returns the first non-NULL expression from a list of expressions.

It is useful when you want to display a default value instead of NULL in query results.

Syntax

COALESCE(expression1, expression2, expression3, ...)

SQL Server checks the expressions from left to right and returns the first expression that is not NULL.

Example: Replace NULL Phone Numbers

SELECT
    EmployeeID,
    EmployeeName,
    COALESCE(PhoneNumber, 'Not Available') AS PhoneNumber
FROM Employees;

Explanation:

  • SQL Server checks the PhoneNumber column.

  • If PhoneNumber contains a value, that value is returned.

  • If PhoneNumber is NULL, the text Not Available is displayed.

Example Output:

EmployeeID EmployeeName PhoneNumber
1 Rahul 9876543210
2 Priya Not Available
3 Amit 9123456780
4 Neha Not Available
5 Rohit 9988776655

Note: COALESCE() changes the query output; it does not update the original table data.

7. COALESCE() with Multiple Columns

COALESCE() can check multiple columns and return the first available value.

Suppose a Customers table contains MobileNumber, AlternateNumber, and OfficeNumber.

SELECT
    CustomerID,
    CustomerName,
    COALESCE(
        MobileNumber,
        AlternateNumber,
        OfficeNumber,
        'No Contact Number'
    ) AS ContactNumber
FROM Customers;

How it works:

  1. SQL Server checks MobileNumber first.

  2. If it is NULL, SQL Server checks AlternateNumber.

  3. If that is also NULL, it checks OfficeNumber.

  4. If all three are NULL, it returns No Contact Number.

This is helpful when customer contact information may be stored in different columns.

8. COALESCE() with Numeric Values

COALESCE() can also be used with numeric columns.

For example, suppose an employee's Bonus value may be NULL.

SELECT
    EmployeeID,
    EmployeeName,
    COALESCE(Bonus, 0) AS Bonus
FROM Employees;

If Bonus is NULL, the query displays 0 instead.

Important: The expressions supplied to COALESCE() must have compatible data types. SQL Server applies data type precedence when determining the returned type.

9. Difference Between COALESCE() and ISNULL()

SQL Server provides both COALESCE() and ISNULL() for handling NULL values. However, they are not identical.

COALESCE() ISNULL()
Accepts two or more expressions Accepts two expressions
Returns the first non-NULL expression Returns the first expression if it is not NULL; otherwise returns the replacement
Follows SQL standard syntax SQL Server-specific function
Result type follows data type precedence rules Result type is generally based on the first expression
Useful for checking multiple alternatives Useful for a simple two-value replacement

COALESCE() Example

SELECT COALESCE(NULL, NULL, 'SQL Server') AS Result;

Output: SQL Server

ISNULL() Example

SELECT ISNULL(NULL, 'Not Available') AS Result;

Output: Not Available

Tip: Use COALESCE() when you need to check multiple possible values. Use ISNULL() when a simple two-expression replacement is sufficient.

10. Using NULL Conditions with AND and OR

You can combine IS NULL and IS NOT NULL with other filtering conditions.

Example 1: Employees Without Phone Numbers and Salary Above 30000

SELECT *
FROM Employees
WHERE PhoneNumber IS NULL
AND Salary > 30000;

This query returns employees who have no phone number and earn more than 30000.

Example 2: Employees With Missing Email or Phone Number

SELECT *
FROM Employees
WHERE Email IS NULL
OR PhoneNumber IS NULL;

This query returns employees whose email address or phone number is missing.

Example 3: Employees With Available Email and Salary Above 30000

SELECT *
FROM Employees
WHERE Email IS NOT NULL
AND Salary > 30000;

These conditions help filter incomplete records in practical database applications.

11. Practical Example: Display Missing Values with Default Text

Suppose you are developing a student management system. Some students may not have submitted their email address or contact number.

You can display default text using COALESCE():

SELECT
    StudentID,
    StudentName,
    COALESCE(Email, 'Email Not Provided') AS Email,
    COALESCE(PhoneNumber, 'Contact Not Provided') AS PhoneNumber
FROM Students;

This makes reports and application screens easier to understand because users see meaningful messages instead of blank or NULL values.

12. Common Mistakes to Avoid

  1. Using = NULL: Use IS NULL instead.

  2. Confusing NULL with zero: Zero is a valid numeric value, whereas NULL represents missing or unknown information.

  3. Confusing NULL with an empty string: An empty string is a value; NULL is not.

  4. Using incompatible data types in COALESCE(): Ensure the expressions can be converted to a compatible result type.

  5. Assuming COALESCE() updates table data: It only changes the returned query result unless you explicitly run an UPDATE statement.

  6. Ignoring NULL values in conditions: Always consider how missing data affects filtering and reporting.

13. Practice Questions

Try these SQL queries yourself:

  1. Write a query to find all employees whose email address is NULL.

  2. Write a query to find employees who have a phone number.

  3. Display NULL email addresses as Email Not Available using COALESCE().

  4. Find employees whose salary is greater than 30000 and phone number is NULL.

  5. Display the first available contact number from MobileNumber, AlternateNumber, and OfficeNumber.

  6. Explain the difference between IS NULL and IS NOT NULL.

  7. What is the difference between COALESCE() and ISNULL()?

14. Frequently Asked Questions (FAQs)

Q1. What does NULL mean in SQL Server?

NULL represents a missing, unknown, or unavailable value in a database column.

Q2. How do I find NULL values in SQL Server?

Use the IS NULL condition with the WHERE clause.

SELECT *
FROM Employees
WHERE PhoneNumber IS NULL;

Q3. How do I find records that do not contain NULL values?

Use IS NOT NULL.

SELECT *
FROM Employees
WHERE PhoneNumber IS NOT NULL;

Q4. Can I use = NULL in SQL Server?

No. Use IS NULL to check for NULL values because comparisons with NULL do not evaluate to TRUE in the usual way.

Q5. What is the purpose of COALESCE()?

COALESCE() returns the first non-NULL expression from the supplied list.

Q6. Does COALESCE() permanently replace NULL values?

No. It changes the value displayed in the query result. It does not modify the stored table data.

Q7. Can COALESCE() accept multiple values?

Yes. It can accept multiple expressions and returns the first one that is not NULL.

15. Conclusion

In this tutorial, we learned how NULL values work in Microsoft SQL Server and why they are important when managing incomplete database information. We explored the difference between NULL, zero, and empty strings, and practised using IS NULL and IS NOT NULL to filter records.

We also learned how the COALESCE() function returns the first available non-NULL value, how it can display default text or numeric values, and how it differs from ISNULL(). Finally, we combined NULL conditions with AND and OR to create practical database queries.

At Param Classes, we focus on teaching SQL Server through simple explanations, structured lessons, and practical examples. Practise these queries in SQL Server Management Studio (SSMS) to build confidence in handling missing data and writing reliable database queries.

Keep practising with Param Classes and continue learning SQL Server step by step!

←
Post 16 of 40
→