Lesson 24 of 60 – NULL Values
40%

NULL Values in SQL

NULL represents a missing, unknown, or unavailable value in a database. NULL is different from zero, an empty string, or a blank space.

Note: You should use IS NULL and IS NOT NULL to check for NULL values. Do not use = NULL or != NULL.

1. What is NULL?

NULL means that a value is missing, unknown, or not available.

SELECT *
FROM Students
WHERE mobile IS NULL;

This returns students whose mobile number has no value stored.

2. NULL is Not Zero

NULL and zero are completely different values.

SELECT *
FROM Students
WHERE fee = 0;

This finds students whose fee is exactly zero. It does not find students whose fee is NULL.

3. NULL is Not an Empty String

An empty string '' is different from NULL.

SELECT *
FROM Students
WHERE mobile = '';

This searches for an empty string, not a NULL value.

4. IS NULL

Use IS NULL when you want to find records containing NULL.

SELECT *
FROM Students
WHERE email IS NULL;

This returns students whose email value is NULL.

5. IS NOT NULL

Use IS NOT NULL to find records where a value exists.

SELECT *
FROM Students
WHERE email IS NOT NULL;

This returns students whose email column contains a non-NULL value.

6. NULL with SELECT

SELECT name, mobile
FROM Students
WHERE mobile IS NULL;

This displays the names and mobile numbers of students whose mobile number is missing.

7. NULL with WHERE

IS NULL is normally used with the WHERE clause.

SELECT *
FROM Students
WHERE address IS NULL;

This finds students whose address has not been stored.

8. Finding Missing Email Addresses

SELECT name, email
FROM Students
WHERE email IS NULL;

This can be useful for finding students whose email addresses need to be collected.

9. Finding Available Email Addresses

SELECT name, email
FROM Students
WHERE email IS NOT NULL;

This returns students who have an email value stored.

10. NULL with AND

SELECT *
FROM Students
WHERE email IS NULL
AND mobile IS NULL;

This finds students whose email and mobile values are both NULL.

11. NULL with OR

SELECT *
FROM Students
WHERE email IS NULL
OR mobile IS NULL;

This returns students who have either a missing email or a missing mobile number.

12. IS NULL vs = NULL

A common mistake is using the equality operator with NULL.

Incorrect:

SELECT *
FROM Students
WHERE email = NULL;

Correct:

SELECT *
FROM Students
WHERE email IS NULL;

Use IS NULL to test for NULL values.

13. IS NOT NULL vs != NULL

Do not use != NULL to check for non-NULL values.

Incorrect:

SELECT *
FROM Students
WHERE email != NULL;

Correct:

SELECT *
FROM Students
WHERE email IS NOT NULL;

14. NULL with COUNT()

COUNT(column_name) counts non-NULL values in that column.

SELECT COUNT(email)
FROM Students;

This counts the students whose email column contains a value.

15. COUNT(*) and NULL

COUNT(*) counts rows regardless of whether individual columns contain NULL.

SELECT COUNT(*)
FROM Students;

This counts all rows in the Students table.

16. NULL with ORDER BY

SELECT name, fee
FROM Students
ORDER BY fee ASC;

When sorting a column containing NULL values, the exact position of NULL depends on the database system and sorting rules.

17. NULL with UPDATE

You can set a column to NULL using UPDATE when the column allows NULL.

UPDATE Students
SET email = NULL
WHERE student_id = 101;

This removes the stored email value for the specified student by setting it to NULL.

18. Replacing NULL with a Value

You can use COALESCE() to display an alternative value when a column is NULL.

SELECT name,
COALESCE(email, 'Not Available') AS email
FROM Students;

If email is NULL, the query displays "Not Available".

19. COALESCE with Multiple Values

COALESCE returns the first non-NULL expression.

SELECT name,
COALESCE(mobile, alternate_mobile, 'No Number')
AS contact
FROM Students;

The query first checks mobile, then alternate_mobile, and finally displays "No Number" if both are NULL.

20. NULL in Calculations

Calculations involving NULL generally result in NULL.

SELECT fee, fee + 1000 AS new_fee
FROM Students;

If fee is NULL, the calculated new_fee will also be NULL.

21. Handling NULL in Calculations

COALESCE can be used when you want to treat NULL as a specific value during a calculation.

SELECT fee,
COALESCE(fee, 0) + 1000 AS new_fee
FROM Students;

Here, a NULL fee is treated as 0 for the calculation.

22. NULL with GROUP BY

SELECT city, COUNT(*)
FROM Students
GROUP BY city;

Depending on the database system, rows with NULL city values can form a group representing NULL.

23. Finding Students Without Address

SELECT name, mobile
FROM Students
WHERE address IS NULL;

This is useful for finding student records that need address information.

24. Finding Incomplete Student Records

SELECT *
FROM Students
WHERE name IS NULL
OR mobile IS NULL
OR email IS NULL
OR address IS NULL;

This query finds records where at least one important field is NULL.

25. Finding Complete Student Records

SELECT *
FROM Students
WHERE name IS NOT NULL
AND mobile IS NOT NULL
AND email IS NOT NULL
AND address IS NOT NULL;

This returns records where all four specified columns contain non-NULL values.

26. NULL with NOT

You can use NOT with an IS NULL condition.

SELECT *
FROM Students
WHERE NOT email IS NULL;

This identifies rows where email is not NULL. The clearer and more common form is:

SELECT *
FROM Students
WHERE email IS NOT NULL;

27. NULL with LIKE

A LIKE condition does not match a NULL value.

SELECT *
FROM Students
WHERE email LIKE '%gmail.com';

Rows where email is NULL are not returned by this condition.

28. NULL with IN

IN should not be used as a replacement for IS NULL.

SELECT *
FROM Students
WHERE city IN ('Patna', 'Delhi');

Rows where city is NULL do not match this IN condition.

To find NULL cities, use:

SELECT *
FROM Students
WHERE city IS NULL;

29. Practical Example

Suppose an institute wants to find students who have not provided an email address or mobile number.

SELECT name, mobile, email
FROM Students
WHERE mobile IS NULL
OR email IS NULL;

This query finds student records with missing contact information.

30. Complete NULL Example

SELECT name, age, course, city, mobile, email
FROM Students
WHERE mobile IS NOT NULL
AND email IS NULL
AND city IS NOT NULL
AND course IN ('Python', 'Java')
ORDER BY name ASC;

This query:

  • Finds students who have a mobile number.
  • Finds students whose email is NULL.
  • Requires the city to be available.
  • Allows Python or Java courses.
  • Sorts the results alphabetically by name.

📌 Key Points

  • NULL represents a missing, unknown, or unavailable value.
  • NULL is different from zero.
  • NULL is different from an empty string.
  • Use IS NULL to find NULL values.
  • Use IS NOT NULL to find non-NULL values.
  • Do not use = NULL to check for NULL.
  • Do not use != NULL to check for non-NULL values.
  • COUNT(column_name) ignores NULL values.
  • COUNT(*) counts rows regardless of NULL values in individual columns.
  • COALESCE() can be used to provide a replacement value for NULL.
  • NULL can affect calculations and comparisons.
  • LIKE and IN conditions do not match NULL values.

🧠 Quick Quiz

Question: Which SQL condition is used to check whether a column contains a NULL value?