DSA Factory
Free SQL queries lessonsSQL queries · Stage 1 · Shaping the results · Step 2

NULL means unknown

Test for missing values with IS NULL, and fill them in with COALESCE. About 9 minutes.

A blank is not a zero

A member joined the library but never gave a phone number. That box isn't empty text and it isn't zero. It is unknown, and SQL has a name for that: NULL.

Comparing with an unknown gives an unknown answer. phone = NULL is never true, not even for the rows where phone is NULL, and phone <> NULL isn't either. WHERE keeps a row only when its test is true, so you ask with IS NULL or IS NOT NULL instead.

= NULL: never matches. Write IS NULL.
In code
SELECT name
FROM members
WHERE phone IS NULL;
WHERE phone IS NULL
members
idnamephone
1Asha98450
2RaviNULL
3Meera91234
4KabirNULL

Asha has a phone number, so phone IS NULL is false. Dropped.

Move 1 of 4

Unknown rows fall out of both sides

A row where cgpa is NULL fails the test "7 or more", and it fails the test "under 7" too. Unknown is neither, so it appears in neither answer. That is how "not equal" tests quietly lose rows.

To keep them, say so with an extra OR cgpa IS NULL. And COALESCE gives the first value that isn't NULL, handy for showing a default.

COALESCE(x, y): x, or y when x is unknown.
In code
WHERE cgpa < 7 OR cgpa IS NULL

SELECT name, COALESCE(phone, 'none')
FROM members;
Quick check

A table has 10 rows. Six have cgpa 8 or more, two have cgpa under 8, and two have cgpa NULL. How many rows does WHERE cgpa < 8 return?

  1. A2
  2. B4
  3. C8
Show the answer

2. Only the two rows with a real cgpa below 8 pass. NULL is unknown, so it fails < 8.

Your problem

Missing phone numbers

The members table has one row per library member. Show the name of every member who has no phone number on file.

Tables
members(id, name, phone)

Up to 2,000 members

Write the query in your browserFree account, no card. Your query runs on real tables, with hints if you get stuck, and your progress is saved.
Sign up free to solve