Published on 2021-08-13 15:54:47

NULL in SQL, as well as programming in general, means literally "nothing". In SQL, it is easier to understand as "the absence of any value".

It is important to distinguish it from seemingly empty values, such as the empty string '' or the number 0, neither of which are actually NULL.

It is also important to be careful not to enclose NULL in quotes, like 'NULL', which is allowed in columns that accept text, but is not NULL and can cause errors and incorrect data sets.

Filtering for NULL in queries

The syntax for filtering for NULL (i.e. the absence of a value) in WHERE blocks is slightly different than filtering for specific values.

SELECT * FROM Employees WHERE ManagerId IS NULL ;

Note that because NULL is not equal to anything, not even to itself, using equality operators = NULL or <> NULL (or != NULL) will always yield the truth value of UNKNOWN which will be rejected by WHERE.

WHERE filters all rows that the condition is FALSE or UKNOWN and keeps only rows that the condition is TRUE.

Nullable columns in tables

When creating tables it is possible to declare a column as nullable or non-nullable.

MyCol1 INT NOT NULL, -- non-nullable
MyCol2 INT NULL -- nullable
) ;

By default every column (except those in primary key constraint) is nullable unless we explicitly set NOT NULL constraint.

Attempting to assign NULL to a non-nullable column will result in an error.

INSERT INTO MyTable (MyCol1, MyCol2) VALUES (1, NULL) ; -- works fine
INSERT INTO MyTable (MyCol1, MyCol2) VALUES (NULL, 2) ;
-- cannot insert
-- the value NULL into column 'MyCol1', table 'MyTable';
-- column does not allow nulls. INSERT fails.

Updating fields to NULL

Setting a field to NULL works exactly like with any other value:

UPDATE Employees
SET ManagerId = NULL
WHERE Id = 4

Inserting rows with NULL fields

For example inserting an employee with no phone number and no manager into the Employees example table:

INSERT INTO Employees (Id, FName, LName, PhoneNumber, ManagerId, DepartmentId, Salary, HireDate) VALUES (5, 'Jane', 'Doe', NULL, NULL, 2, 800, '2016-07-22') ;

ATutorialHub Related Guide

Comments (9)

Leave a Comment

Your email address will not be published. Required fields are marked*

User Comments

html tutorial comments

panduranga gupta

2021-07-05 07:03:13

good website for learning and help me a lot

html tutorial comments


2021-09-25 14:58:47

The awsome website i am looking like for a long time, good work atutorialhub team keep doing

html tutorial comments


2021-09-01 15:03:56

Learning a lot from the courses present on atutorialhub. The courses are very well explained. Great experience

html tutorial comments


2021-09-10 15:05:45

It is very helpful to students and easy to learn the concepts

html tutorial comments


2021-09-14 15:06:41

Great job Tutorials are easy to understand Please make use of it

html tutorial comments

Zain Khan

2021-09-18 15:07:23

Great content and customized courses.

html tutorial comments

Rudrakshi Bhatt

2021-09-09 15:08:10

Well structured coursed and explained really well!

html tutorial comments

Pavana Somashekar

2021-09-11 15:09:08

Good platform for beginners and learn a lot on this website

html tutorial comments


2021-09-25 19:35:50

Nice website