Menu

1 / 2

Why WHERE phone = NULL Returns Nothing in SQL

DEV Community·Dechive·6 months ago
#kqkTGmed
#null#beginners#database#sql#enter
Reading 0:00
15s threshold

If you are new to SQL, this query can look perfectly reasonable at first:

SELECT name FROM users WHERE phone = NULL ;

Enter fullscreen mode

Exit fullscreen mode

You have a users table.

Some users did not enter their phone number.

The phone column looks empty.

So you try to find those rows with phone = NULL .

But the result comes back empty.

The table is not necessarily wrong.

The column name may be correct.

The query may even look logical.

The problem is that NULL is not a normal value.

To understand why this query returns nothing, we need to understand what NULL really means.

NULL is not an empty value

At first, NULL looks like an empty cell.

It feels similar to 0 or an empty string.

But in SQL, these are different things:

0 -> a number '' -> an empty string NULL -> an unknown or unrecorded state

Enter fullscreen mode

Exit fullscreen mode

0 is a value.

It means the number zero.

'' is also a value.

It means a string with no characters.

But NULL is different.

NULL does not mean…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.