Wednesday, December 18, 2024

SQL IS NULL Keyword

 In SQL, the IS NULL keyword is used to check whether a column contains a NULL value. NULL is a special marker used to indicate that a data value does not exist or is unknown.

Usage of IS NULL:

1. Basic Syntax:

SELECT column_name
FROM table_name
WHERE column_name IS NULL;

This query will return rows where the column_name contains a NULL value.

2. Example:

If you have a students table and want to find students who haven't enrolled in any courses (assuming course_id is nullable), you would use:

SELECT student_id, student_name
FROM students
WHERE course_id IS NULL;

3. Checking Non-Null Values:

To find rows where a column is not NULL, use IS NOT NULL:

SELECT column_name
FROM table_name
WHERE column_name IS NOT NULL;

Important Points:

  • NULL is not the same as an empty string or zero. It's a special value used to represent the absence of any data.
  • You cannot use regular comparison operators like = or != with NULL. You must use IS NULL or IS NOT NULL for checking NULL values.

No comments:

Post a Comment