bhylu.com

Advertisements

SQL Null Values

NULL means a value is missing or unknown. It is not zero, an empty string, or the word NULL. A database keeps these cases separate because they can mean different things in a real application.

Understand the idea

An equality comparison with NULL does not establish that two values are equal. Use IS NULL or IS NOT NULL to ask about missingness. Asha has no recorded email in the seed data. Noor has an email but no recorded city. Deciding which columns may be missing is part of designing the data model.

Where to try it

Run this example in the SQL playground. The lesson link loads the shop tables and sample rows before this query. Select Run when you are ready. Every run starts fresh, so your changes do not affect the website or a previous run.

SQLite sample data · MySQL / MariaDB sample data · See the sample tables

A worked example

SQLite

SELECT name, email
FROM customers
WHERE email IS NULL
ORDER BY customer_id;

Read the result

Asha is returned. Her email is displayed as NULL. The query is checking whether an email is recorded, not whether it contains particular text.

Try one change

Find customers whose city IS NULL. Then find customers with a recorded email using IS NOT NULL.