Sub topic 5
Data Types and the Meaning of NULL
This chapter assumes you know that a database stores rows in tables and that each column describes one property of a row. You should also be comfortable reading short SQL statements.
Why data types matter
A data type tells the database what kind of value a column can store. It helps the database validate input, compare values correctly, and store data efficiently.
Common types include:
| Data type | Typical use | Example |
|---|---|---|
| INTEGER | Whole numbers | 42 |
| DECIMAL | Exact amounts | 19.95 |
| VARCHAR | Text with a maximum length | Ada Lovelace |
| DATE | A calendar date | 2026-01-15 |
| BOOLEAN | True or false | TRUE |
Choose a type based on the meaning of the value. Use DECIMAL for money instead of a floating-point type, because floating-point arithmetic can introduce small rounding errors. Use DATE when you need a date, rather than storing dates as arbitrary text.
A type does not replace validation. A VARCHAR column can store text, but it does not guarantee that the text is a valid email address. Use constraints or application logic for rules that the type cannot express.
NULL means missing or unknown
NULL is a special database value that means a value is absent, unknown, or not applicable. It is not the same as zero, an empty string, or FALSE.
For example, a customer might not have provided a phone number. The phone column could contain NULL. A phone number of 0 would mean something different, and an empty string might mean that someone deliberately recorded no text.
SQL uses three-valued logic when NULL is involved:
- TRUE
- FALSE
- UNKNOWN
Comparing a value with NULL produces UNKNOWN, even when the value is also NULL. Therefore, this condition does not find missing phone numbers:
WHERE phone = NULLUse IS NULL or IS NOT NULL instead:
SELECT customer_id, name
FROM customers
WHERE phone IS NULL;The following diagram shows the basic decision.
NULL in expressions and aggregates
Many expressions involving NULL also produce NULL. For example, an unpaid invoice with a NULL discount does not produce a known total when you subtract the discount. You can replace NULL with a default value by using COALESCE:
SELECT invoice_id,
amount - COALESCE(discount, 0) AS amount_due
FROM invoices;COALESCE returns the first non-NULL expression. Here, a missing discount is treated as zero for this calculation. This does not change the stored value.
NULL also affects aggregate functions. COUNT(*) counts rows, while COUNT(phone) counts only rows where phone is not NULL. SUM, AVG, MIN, and MAX generally ignore NULL values. If every value is NULL, these functions can return NULL rather than zero.
Choosing whether NULL is allowed
Columns allow NULL by default in many database systems. Add NOT NULL when every row must have a value:
CREATE TABLE tasks (
task_id INTEGER PRIMARY KEY,
description VARCHAR(200) NOT NULL,
due_date DATE,
completed BOOLEAN NOT NULL DEFAULT FALSE
);Here, every task needs a description and completion status. A due date is optional, so due_date can be NULL. The DEFAULT value is used when an INSERT omits completed, but DEFAULT does not prevent someone from explicitly inserting NULL. Use NOT NULL when missing values are invalid.
Exercise
Which condition correctly finds rows where duedate has no value, and why does duedate = NULL fail?
Show answer
Use WHERE duedate IS NULL. Equality with NULL produces UNKNOWN rather than TRUE, so duedate = NULL does not match missing values.