How to Handle NULL Values in SQL Server

Galaxy Glossary

How do I handle NULL values in SQL Server?

NULL handling in SQL Server lets you test, replace, and safely aggregate missing values so queries return the expected results.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Description

Table of Contents

Why do NULLs matter in SQL Server?

NULL represents “unknown.” Arithmetic, concatenation, and comparisons that involve NULL return NULL, which can break filters, joins, and reports.

How do I test for NULL values?

Use IS NULL or IS NOT NULL. Comparing with = NULL never works because NULL is not a value.

Syntax of IS NULL / IS NOT NULL

column_name IS [NOT] NULL in WHERE, JOIN, or CASE clauses.

How do I turn NULL into a default value?

Wrap the column in ISNULL() or COALESCE(). Both return the first non-NULL argument, letting you supply a fallback.

What is the difference between ISNULL and COALESCE?

ISNULL() is SQL Server–specific and returns the data type of its first argument; COALESCE() is ANSI and evaluates multiple inputs. Prefer COALESCE() for portability.

Can aggregates ignore NULLs?

Yes. SUM(), COUNT(col), AVG(), MIN(), and MAX() automatically skip NULL rows, so no extra handling is required.

How do I sort NULLs last?

Use a CASE expression in ORDER BY: ORDER BY CASE WHEN column IS NULL THEN 1 ELSE 0 END, column.

Best practices for NULL handling

Declare columns NOT NULL whenever possible, document when NULL is meaningful, and use COALESCE() in output layers instead of core tables.

Why How to Handle NULL Values in SQL Server is important

How to Handle NULL Values in SQL Server Example Usage


-- Show each customer's last order date; use 'N/A' when no orders exist
SELECT c.id,
       c.name,
       COALESCE(MAX(o.order_date)::varchar, 'N/A') AS last_order
FROM Customers c
LEFT JOIN Orders o ON o.customer_id = c.id
GROUP BY c.id, c.name;

How to Handle NULL Values in SQL Server Syntax


-- Test for NULL
SELECT *
FROM Orders
WHERE total_amount IS NULL;

-- Replace NULL with default
SELECT COALESCE(total_amount, 0) AS total_amount
FROM Orders;

-- Multiple fallbacks
SELECT COALESCE(Orders.total_amount, Products.price, 0) AS amount
FROM Orders
JOIN OrderItems ON Orders.id = OrderItems.order_id
JOIN Products    ON Products.id = OrderItems.product_id;

Common Mistakes

Frequently Asked Questions (FAQs)

Does COUNT(*) include NULLs?

Yes. COUNT(*) counts rows, not column values. Use COUNT(column) to skip NULLs.

Can I compare two NULLs for equality?

No. NULL = NULL returns NULL. Use IS NULL on both columns or set ANSI_NULLS OFF (not recommended).

Is COALESCE slower than ISNULL?

Performance is virtually identical for two arguments. COALESCE adds negligible overhead for more inputs.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.