Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
MySQL + SQL · Lesson 50

IS NULL and IS NOT NULL

What NULL Really Means

NULL represents missing, unknown or not-applicable information. It is not zero, FALSE, a blank string or the word “NULL”. A student whose result is not declared may have marks = NULL; a student scoring zero has a known value.

Common learning data: students contains Aarav (X-A, 86.50, Active), Meera (X-A, 91.00, Active), Kabir (X-B, 74.00, Inactive) and Sana (X-B, 88.50, Active).
Model first: allow NULL only when absence has a clear meaning. If every student must have a status, keep status NOT NULL with a valid default or constraint.

IS NULL and IS NOT NULL

SELECT student_id, full_name
FROM students
WHERE marks IS NULL;

SELECT student_id, full_name, marks
FROM students
WHERE marks IS NOT NULL
ORDER BY student_id;

The first query finds missing marks; the second finds recorded marks. Do not write marks = NULL or marks <> NULL.

NULL and Three-Valued Logic

ExpressionResult
NULL = NULLUNKNOWN
NULL <> 80UNKNOWN
NULL IS NULLTRUE
NULL IS NOT NULLFALSE
NULL <=> NULLTRUE in MySQL

UNKNOWN does not pass WHERE. It also explains why NOT IN can surprise when its list contains NULL.

COUNT, Arithmetic and Fallback Values

SELECT COUNT(*) AS total_rows,
       COUNT(marks) AS recorded_marks,
       AVG(marks) AS average_recorded_marks
FROM students;
total_rows = 4 recorded_marks = 4 average_recorded_marks = 85.00

Most aggregates ignore NULL inputs; COUNT(*) counts rows. Arithmetic involving NULL commonly returns NULL. To display a label without changing storage:

SELECT full_name,
       COALESCE(CAST(marks AS CHAR), 'Not recorded') AS marks_display
FROM students;

Design and Safe Updates

Use NULL when “unknown” is valid, NOT NULL when a value is mandatory, and a CHECK/foreign key for valid choices. Before filling missing data, preview exact rows:

START TRANSACTION;
SELECT student_id, full_name FROM students
WHERE marks IS NULL FOR UPDATE;

UPDATE students SET marks = 0
WHERE marks IS NULL;
-- Verify whether zero truly means the same thing.
ROLLBACK;
Never replace unknown values with zero merely to simplify a report. That changes meaning and can lower averages incorrectly.

Mistakes and Practice

  • Writing = NULL.
  • Using empty text to mean unknown in every column.
  • Forgetting that aggregates ignore NULL differently.
  • Applying COALESCE before deciding the business meaning.
  1. Add one practice row with NULL marks and compare COUNT(*) with COUNT(marks).
  2. Find only recorded marks.
  3. Show “Pending” without modifying the stored NULL.
  4. Explain why zero and NULL change AVG differently.

Quick Summary

  • NULL means no known value.
  • Test it with IS NULL or IS NOT NULL.
  • WHERE rejects UNKNOWN; most aggregates ignore NULL.
  • Fallback functions change display, not the underlying meaning.

Official References

References reviewed 14 August 2026.

Frequently Asked Questions

Is NULL the same as zero or an empty string?
No. NULL means no known value. Zero and an empty string are known values of numeric and string types.
Why does = NULL return no rows?
Ordinary comparison with NULL returns UNKNOWN, and WHERE keeps only TRUE. Use IS NULL.
Does COUNT(column) count NULL values?
No. COUNT(column) counts non-NULL values. COUNT(*) counts rows regardless of NULLs in individual columns.
How can I display a fallback for NULL?
Use COALESCE(expression, fallback) or MySQL IFNULL(expression, fallback), but do not overwrite the stored meaning unless the business rule requires it.
Can a NOT NULL column still need validation?
Yes. NOT NULL only prevents NULL; it does not reject an empty string, zero or an invalid business value unless other constraints do so.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.