DbSchema Database Designer

DbSchema Tutorial | SQL NULL FUNCTIONS



SQL, the backbone of many databases, often deals with data that may be absent or not yet available. Such data is represented by a special marker called NULL. This comprehensive guide is tailored to provide a deeper understanding of SQL’s NULL functions and their applications.

Introduction to NULL in SQL

In SQL, NULL symbolizes the absence or unavailability of data. It’s not equivalent to zero, an empty string, or any predetermined value. Instead, NULL uniquely marks that data is missing.

SQL NULL Functions: An Overview

SQL boasts a variety of functions to address NULL values. The most prominent among them are IFNULL(), ISNULL(), COALESCE(), and NVL(). Let’s dissect each.

1. IFNULL() Function

Syntax:

IFNULL(expression1, expression2)

IFNULL() checks the first expression. If it’s non-NULL, the function returns it; otherwise, it returns the second expression.

Example:

Consider the sample Students table:

ID Name Age Grade
1 Alice 20 NULL
2 Bob NULL B
3 Carol 22 C

Query:

SELECT Name, IFNULL(Age, "Unknown") AS Age FROM Students;

Result:

Name Age
Alice 20
Bob Unknown
Carol 22

Explanation:

For Bob, since the Age is NULL, the function returns “Unknown”.

2. ISNULL() Function

Syntax:

ISNULL(expression, replacement_value)

ISNULL() is akin to IFNULL(). It returns the first expression if it isn’t NULL; otherwise, it provides the replacement value.

Example:

Query:

SELECT Name, ISNULL(Age, "Not Specified") AS Age FROM Students;

Result:

Name Age
Alice 20
Bob Not Specified
Carol 22

Explanation:

Here, Bob’s age is NULL, so “Not Specified” is displayed.

3. COALESCE() Function

Syntax:

COALESCE(expression1, expression2, ...)

This function scans the list of expressions and returns the first non-NULL expression.

Example:

Query:

SELECT Name, COALESCE(Age, Grade, "Unknown") AS Detail FROM Students;

Result:

Name Detail
Alice 20
Bob B
Carol 22

Explanation:

For Bob, since the Age is NULL, the function moves to the next expression, Grade, and returns “B”.

4. NVL() Function

Syntax:

NVL(expression1, expression2)

Specific to Oracle databases, it returns the first expression if it’s non-NULL; else, it provides the second.

Counting NULL Values

To enumerate the NULL values in a column, combine COUNT() with IS NULL.

Example:

Query:

SELECT COUNT(*) FROM Students WHERE Age IS NULL;

Result:

Count
1

Explanation:

Only Bob has a NULL age, so the count is 1.

AVG() Function with NULL Values

When employing AVG(), SQL excludes NULL values, computing the average solely based on non-NULL entries.

Example:

Query:

SELECT AVG(Age) FROM Students;

Result:

AVG(Age)
21

Explanation:

Only Alice’s and Carol’s ages (20 and 22) are considered. Their average is 21.

Differentiating ‘IS NULL’ and ‘= NULL’

Criteria IS NULL = NULL
Usage Correct Incorrect
Represents Absence Comparison
SQL Standard Yes No

NULLs in String Concatenations

When concatenating a string with a NULL, the result is typically NULL.

Example:

Query:

SELECT Name, Age || ' years' AS AgeDetail FROM Students;

Result:

Name AgeDetail
Alice 20 years
Bob NULL
Carol 22 years

Explanation:

Bob’s age is NULL, so the concatenated result is also NULL.

Common Pitfalls

  1. Equating NULL to empty strings or zero.
  2. Using = NULL instead of IS NULL.
  3. Neglecting that operations with NULL yield NULL.

FAQs

  1. Is NULL a value?
    No, it signifies the absence of a value.
  2. Can NULL participate in mathematical operations?
    Yes, but the result remains NULL.
  3. How to default NULL to another value?
    Utilize IFNULL(), ISNULL(), COALESCE(), or NVL().

Practice Exercises

  1. Identify students with a NULL grade.
  2. Replace all NULL grades with “Not Assessed”.
  3. Compute the mean age, excluding NULL values.
  4. Merge the name and age of students, using “Unknown” for NULL ages.

Wrapping Up

Managing NULL values in SQL is paramount for precise data operations and analytics. By mastering these functions and methodologies, you’ll be adept at handling unavailable or missing data in SQL.

Remember: A NULL in SQL isn’t just a placeholder—it’s a significant marker of data that’s yet to be known.

DbSchema → Your Trusted Partner in Database Design

Simplify complex database workflows and improve productivity with DbSchema's advanced design and management tools

Visual Design & Modeling
Visual Design & Schema Layout

➤ Create and manage your database schema visually through a user-friendly graphical interface.

➤ Easily arrange tables, columns, and foreign keys to simplify complex database structures, ensuring clarity and accessibility.

GIT & Collaboration
Version Control & Collaboration

➤ Manage schema changes through version control with built-in Git integration, ensuring every update is tracked and backed up.

➤ Collaborate efficiently with your team to maintain data integrity and streamline your workflow for accurate, consistent results.

Data Explorer & Query Builder
Relational Data & Query Builder

➤ Seamlessly navigate and visually explore your database, inspecting tables and their relationships.

➤ Build complex SQL queries using an intuitive drag-and-drop interface, providing instant results for quick, actionable insights.

Interactive Documentation & Reporting
HTML5 Documentation & Reporting

➤ Generate HTML5 documentation that provides an interactive view of your database schema.

➤ Include comments for columns, use tags for better organization, and create visually reports.