NULL Values

What is a NULL Value?

A field with a NULL value has no value. It is not the same as zero or an empty string: it means "unknown" or "missing".

In the demo data, Golden Spoon has a NULL city, order 105 has a NULL employee, and Almonds has a NULL stock.

How to Test for NULL Values?

You cannot use = or <> with NULL; any comparison with NULL gives "unknown", which is treated as false. Use IS NULL and IS NOT NULL.

Wrong

This returns no rows, even though a NULL city exists:

SELECT * FROM Customers
WHERE City = NULL;

Result: 0 rows

CustomerIDCustomerNameContactNameCityCountry

Example (IS NULL)

The correct way: find customers whose city is missing:

SELECT CustomerName, City FROM Customers
WHERE City IS NULL;

Result: 1 row

CustomerNameCity
Golden SpoonNULL

IS NOT NULL

Example

Orders that were handled by an employee:

SELECT OrderID, EmployeeID FROM Orders
WHERE EmployeeID IS NOT NULL;

Result: 7 rows

OrderIDEmployeeID
1012
1023
1032
1043
1062
1073
1082

NULL in Calculations

Any arithmetic with NULL gives NULL. Aggregate functions like SUM and AVG skip NULL values.

Example

The stock value of Almonds is NULL because its stock is NULL:

SELECT ProductName, Stock, Price * Stock AS StockValue FROM Products;

Result: 6 rows

ProductNameStockStockValue
Basmati Rice 5kg1201500
Green Tea 100 bags3002025
Olive Oil 1L80799.2
Dark Chocolate00
Espresso Beans 1kg45810
Almonds 500gNULLNULL

Tip: Use COALESCE(Stock, 0) to treat missing values as zero. See NULL Functions in the Functions module.