SELECT NULL values in SQL

SQL

SELECT NULL is essential for identifying missing or undefined values in specific columns and providing flexibility in SQL queries.

Syntax

The basic syntax of the SELECT NULL command is as follows:

SELECT NULL;

Database Table: students

student_idstudent_namedate_of_birth
1John Doe1990-05-15
2Jane SmithNULL
3Bob Johnson1992-09-08

Example 1: Finding Null values

To retrieve students with an unknown date of birth:

SELECT student_id, student_name
FROM students
WHERE date_of_birth IS NULL;

Result Set

student_idstudent_name
2Jane Smith

Example 2: Identifying Students with Known Information

On the flip side, what if you want to find students with a recorded date of birth? You can use a similar approach, swapping IS NULL with IS NOT NULL.

SELECT student_id, student_name
FROM students
WHERE date_of_birth IS NOT NULL;

Result Set:

student_idstudent_name
1John Doe
3Bob Johnson