What is NULL in SQL?
NULL denotes missing or unknown. SQL NULL refers missing or unknown value in a column of a table.
Note that zero (‘0’) and NULL are not same, they are completely different.
Regarding NULL there could be two conditions:
- SQL NULL
- SQL NOT NULL
We will check each of these in details.
SELECT SQL NULL
During the table creation if you make a column nullable, you can easily exclude that column while inserting data into the other columns. If you do not specify NOT NULL exclusively in the column attribute, the column is said nullable.
Let’s consider a
Student table with the following data.
You can that for Roll No
104 marks are missing that means these are NULL values.
How to select the records where only NULL value exists?
The below query returns only those records which has null value in their marks column.
SELECT * FROM Student WHERE Marks IS NULL;
It will returns the following records.
SQL SELECT NOT NULL
You can also select those records which are having only NOT NULL values in a particular column. In our case case we will select only those records which have NOT NULL values in their
SELECT * FROM Student WHERE Marks IS NOT NULL;
The above query returns the following records.
Summary: In this tutorial, you have learned what is NULL and NOT NULL values in SQL and how to select them from a table when needed.