Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Advanced Null Handling | Null Functions
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Functions in SQL

bookAdvanced Null Handling

When working with SQL, handling NULL values correctly is crucial, especially in more complex queries. NULL represents missing or unknown data, and its presence can significantly impact the results of aggregations and comparisons. For instance, when you perform operations like COUNT, SUM, or AVG, the way these functions treat NULL values can lead to results that might be surprising if you do not account for them.

1234567
-- Demonstrate how COUNT, SUM, and AVG handle NULL values in the 'bonus' column SELECT COUNT(bonus) AS count_bonus, -- Counts only non-NULL values COUNT(*) AS count_all, -- Counts all rows, including those with NULL SUM(bonus) AS sum_bonus, -- Ignores NULLs when summing AVG(bonus) AS avg_bonus -- Ignores NULLs when averaging FROM employees;
copy

In the example above, COUNT(bonus) only counts rows where bonus is not NULL, while COUNT(*) counts all rows regardless of NULLs. Both SUM and AVG also ignore NULL values, operating only on the available data. This behavior ensures that missing data does not skew totals or averages, but it also means you must be aware of how many values are actually present.

Comparisons involving NULL are also unique: any comparison with NULL (such as =, <>, <, >) returns unknown, not true or false. This can affect filtering and conditional logic in your queries.

A common problem arises when dividing by a value that might be zero or NULL. To avoid errors or unwanted results, you can use the NULLIF function. NULLIF compares two expressions; if they are equal, it returns NULL, otherwise it returns the first expression. This is especially useful to prevent division by zero, as dividing by NULL yields NULL instead of causing a runtime error.

12345678910111213
-- Safely calculate the average bonus per department, avoiding division by zero SELECT department, SUM(bonus) / NULLIF(COUNT(bonus), 0) AS avg_bonus FROM employees GROUP BY department; -- Combine COALESCE and NULLIF to provide a default value when division yields NULL SELECT department, COALESCE(SUM(bonus) / NULLIF(COUNT(bonus), 0), 0) AS avg_bonus FROM employees GROUP BY department;
copy

By combining COALESCE with NULLIF, you ensure that if the division results in NULL (such as when there are no bonuses in a department), a default value like 0 is returned instead. This approach makes your queries more robust and prevents unexpected NULL results in reports or calculations.

1. How does COUNT(*) treat NULL values?

2. What is the result of NULLIF when both arguments are equal?

3. Fill in the blanks to use NULLIF in a division operation. Complete the query to safely divide the sum of bonuses by the count of bonuses, avoiding division by zero.

question mark

How does COUNT(*) treat NULL values?

Select the correct answer

question mark

What is the result of NULLIF when both arguments are equal?

Select the correct answer

question-icon

Fill in the blanks to use NULLIF in a division operation. Complete the query to safely divide the sum of bonuses by the count of bonuses, avoiding division by zero.

SELECT SUM(bonus) / NULLIF(, ) FROM employees;
NULL

Click or drag`n`drop items and fill in the blanks

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 5. Hoofdstuk 4

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Suggested prompts:

Can you explain more about how NULL values affect JOIN operations?

What are some best practices for handling NULLs in WHERE clauses?

Can you show examples of using IS NULL and IS NOT NULL in SQL queries?

bookAdvanced Null Handling

Veeg om het menu te tonen

When working with SQL, handling NULL values correctly is crucial, especially in more complex queries. NULL represents missing or unknown data, and its presence can significantly impact the results of aggregations and comparisons. For instance, when you perform operations like COUNT, SUM, or AVG, the way these functions treat NULL values can lead to results that might be surprising if you do not account for them.

1234567
-- Demonstrate how COUNT, SUM, and AVG handle NULL values in the 'bonus' column SELECT COUNT(bonus) AS count_bonus, -- Counts only non-NULL values COUNT(*) AS count_all, -- Counts all rows, including those with NULL SUM(bonus) AS sum_bonus, -- Ignores NULLs when summing AVG(bonus) AS avg_bonus -- Ignores NULLs when averaging FROM employees;
copy

In the example above, COUNT(bonus) only counts rows where bonus is not NULL, while COUNT(*) counts all rows regardless of NULLs. Both SUM and AVG also ignore NULL values, operating only on the available data. This behavior ensures that missing data does not skew totals or averages, but it also means you must be aware of how many values are actually present.

Comparisons involving NULL are also unique: any comparison with NULL (such as =, <>, <, >) returns unknown, not true or false. This can affect filtering and conditional logic in your queries.

A common problem arises when dividing by a value that might be zero or NULL. To avoid errors or unwanted results, you can use the NULLIF function. NULLIF compares two expressions; if they are equal, it returns NULL, otherwise it returns the first expression. This is especially useful to prevent division by zero, as dividing by NULL yields NULL instead of causing a runtime error.

12345678910111213
-- Safely calculate the average bonus per department, avoiding division by zero SELECT department, SUM(bonus) / NULLIF(COUNT(bonus), 0) AS avg_bonus FROM employees GROUP BY department; -- Combine COALESCE and NULLIF to provide a default value when division yields NULL SELECT department, COALESCE(SUM(bonus) / NULLIF(COUNT(bonus), 0), 0) AS avg_bonus FROM employees GROUP BY department;
copy

By combining COALESCE with NULLIF, you ensure that if the division results in NULL (such as when there are no bonuses in a department), a default value like 0 is returned instead. This approach makes your queries more robust and prevents unexpected NULL results in reports or calculations.

1. How does COUNT(*) treat NULL values?

2. What is the result of NULLIF when both arguments are equal?

3. Fill in the blanks to use NULLIF in a division operation. Complete the query to safely divide the sum of bonuses by the count of bonuses, avoiding division by zero.

question mark

How does COUNT(*) treat NULL values?

Select the correct answer

question mark

What is the result of NULLIF when both arguments are equal?

Select the correct answer

question-icon

Fill in the blanks to use NULLIF in a division operation. Complete the query to safely divide the sum of bonuses by the count of bonuses, avoiding division by zero.

SELECT SUM(bonus) / NULLIF(, ) FROM employees;
NULL

Click or drag`n`drop items and fill in the blanks

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 5. Hoofdstuk 4
some-alt