← Back to Questions
SQL

How do you handle NULL values in SQL?

Learn How do you handle NULL values in SQL? with simple explanations, real-time examples, interview tips and practical use cases.

How Do You Handle NULL Values in SQL?

NULL values in SQL are handled using special functions, operators, and conditions to safely process missing or unknown data.

In simple words:

NULL handling ensures queries work correctly even when some data is missing.


Why NULL Handling is Important

Enterprise databases frequently contain:

  • Missing information
  • Incomplete records
  • Optional fields
  • Unknown values

Without proper NULL handling:

  • Calculations may fail
  • Reports may become inaccurate
  • Queries may return unexpected results

Simple Real-Life Example

Think about:

  • A student database where some students have not provided phone numbers

Example Table

Student Phone Number
Naresh 9876543210
Rahul NULL

Problem Without NULL Handling

Applications may display:

NULL

instead of meaningful information.


NULL Handling Internal Architecture

Read Data
    |
    v
NULL Value Found?
 /            \
Yes            No
 |              |
Apply NULL      Process Normally
Handling Logic

Main Ways to Handle NULL Values

  • IS NULL
  • IS NOT NULL
  • COALESCE()
  • NULLIF()
  • CASE Statement
  • IFNULL() / ISNULL()

1. Using IS NULL

IS NULL checks:

  • Whether a value is NULL

Syntax

SELECT *

FROM table_name

WHERE column_name IS NULL;

Example

SELECT *

FROM employees

WHERE email IS NULL;

Purpose

Retrieve employees:

  • Without email addresses

2. Using IS NOT NULL

IS NOT NULL checks:

  • Whether a value exists

Example

SELECT *

FROM employees

WHERE email IS NOT NULL;

Purpose

Retrieve employees:

  • Having email addresses

Why '=' Cannot Be Used with NULL?

Wrong Query:

WHERE email = NULL

Problem

NULL represents:

  • Unknown value

So direct comparison:

  • Does not work properly

Correct Approach

WHERE email IS NULL

3. Using COALESCE()

COALESCE replaces:

  • NULL values with alternative values

Syntax

COALESCE(value1,
         value2,
         default_value)

Example

SELECT employee_name,

COALESCE(phone,
         'Not Available')

FROM employees;

Result

Employee Phone
Naresh 9876543210
Rahul Not Available

How COALESCE Works

Returns:

  • First non-NULL value

4. Using NULLIF()

NULLIF converts:

  • Matching values into NULL

Syntax

NULLIF(expression1,
       expression2)

Example

SELECT NULLIF(quantity, 0)

FROM products;

Purpose

Convert:

0 → NULL

Common Use Case

Prevent:

  • Division by zero errors

Example

SELECT total_amount /
       NULLIF(quantity, 0)

FROM orders;

5. Using CASE Statement

CASE provides:

  • Conditional NULL handling

Example

SELECT employee_name,

CASE

WHEN phone IS NULL
THEN 'Not Available'

ELSE phone

END AS contact_info

FROM employees;

Purpose

Replace NULL phone numbers with:

Not Available

6. Using IFNULL() or ISNULL()

Database-specific NULL handling functions.


MySQL Example

SELECT IFNULL(phone,
              'No Phone')

FROM employees;

SQL Server Example

SELECT ISNULL(phone,
              'No Phone')

FROM employees;

Difference Between COALESCE and IFNULL

Feature COALESCE IFNULL
Standard SQL Yes No
Multiple Expressions Yes No
Portability High Database-specific

NULL Handling in Calculations

Arithmetic operations with NULL:

  • Usually return NULL

Problem Example

SELECT salary + bonus

FROM employees;

Issue

If bonus is NULL:

  • Total becomes NULL

Solution

SELECT salary +
       COALESCE(bonus, 0)

FROM employees;

What Happens?

NULL bonus replaced with:

0

NULL Handling in Aggregate Functions

Most aggregate functions:

  • Ignore NULL values

Example

SELECT AVG(salary)

FROM employees;

Important Point

NULL salaries:

  • Ignored in average calculation

COUNT and NULL

COUNT(*)

Counts:

  • All rows

COUNT(column_name)

Counts:

  • Only non-NULL values

Example

SELECT COUNT(email)

FROM employees;

Purpose

Count employees:

  • Having email addresses

NULL Handling Query Flow

Read Column
    |
    v
NULL?
 /         \
Yes         No
 |           |
Apply NULL   Process Normal
Handling     Value
Logic

NULL and Three-Valued Logic

SQL uses:

  • TRUE
  • FALSE
  • UNKNOWN

Example

NULL = 10

Result

UNKNOWN

NOT NULL Constraint

NOT NULL prevents:

  • NULL storage in columns

Example

CREATE TABLE students (

    id INT,
    name VARCHAR(100) NOT NULL

);

Purpose

Ensure mandatory fields:

  • Always contain values

Performance Consideration

NULL handling may:

  • Affect query optimization
  • Impact indexing

Example

Large NULL-heavy columns may:

  • Reduce index efficiency

Real-Time Banking Example

Banking systems handle NULL values for:

  • Optional nominee information
  • Pending transaction details
  • Incomplete customer profiles

Example

COALESCE(transaction_fee, 0)

Real-Time E-Commerce Example

E-commerce platforms handle NULL values for:

  • Missing discounts
  • Optional product descriptions
  • Shipping details

Example

COALESCE(discount, 0)

Real-Time Learning Platform Example

Learning platforms handle NULL values for:

  • Incomplete student profiles
  • Pending certificates
  • Optional contact information

Example

COALESCE(certificate_status,
         'Pending')

NULL Handling in Microservices

Microservices handle NULL values for:

  • API responses
  • Partial updates
  • Optional request fields

Advantages of Proper NULL Handling

  • Prevents query failures
  • Improves report quality
  • Supports flexible schemas
  • Reduces runtime errors

Disadvantages of Poor NULL Handling

  • Incorrect calculations
  • Unexpected query results
  • Application failures

Best Practices

  • Use IS NULL for NULL checks
  • Use COALESCE for default values
  • Use NULLIF for division safety
  • Use NOT NULL for mandatory columns
  • Avoid excessive NULL values in critical fields

Common Interview Mistake

Many developers think:

  • NULL behaves like zero or empty string

Reality

NULL means:

  • Unknown or missing value

Related Learning Topics


Professional Interview Answer

NULL values in SQL are handled using special operators and functions such as IS NULL, IS NOT NULL, COALESCE, NULLIF, CASE statements, and database-specific functions like IFNULL or ISNULL. Proper NULL handling is important because NULL represents missing or unknown data and can affect calculations, filtering, and query logic. Functions like COALESCE are used to replace NULL values with default values, while NULLIF helps convert matching values into NULL to avoid issues such as division-by-zero errors. Enterprise applications such as banking systems, e-commerce platforms, analytics dashboards, and microservices-based APIs use NULL handling extensively to ensure reliable query execution, accurate reporting, and robust data processing.


Why Interviewers Like This Answer

  • Clearly explains NULL handling methods
  • Includes practical SQL functions
  • Shows enterprise-level understanding
  • Explains calculation and filtering issues
  • Provides real-world examples

Frequently Asked Questions

How do you check NULL values in SQL?

Using IS NULL or IS NOT NULL.

How do you replace NULL values?

Using COALESCE, IFNULL, or ISNULL functions.

Why is NULL handling important?

It prevents incorrect calculations and query failures.

How do you prevent divide-by-zero errors?

Using NULLIF function.

Does COUNT include NULL values?

COUNT(column_name) ignores NULL values, while COUNT(*) counts all rows.

Why this SQL question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.