SQLite - NULL Values
SQLite - Understanding NULL Values
Section titled “SQLite - Understanding NULL Values”In SQLite, NULL represents the absence of a value. It’s a state, not a value itself. A column containing NULL is empty, signifying that no data has been entered for that field.
It is crucial to understand that NULL is distinct from zero (0) or a field containing an empty string (''). Treating them as the same is a common source of bugs in applications.
The Challenge of Three-Valued Logic
Section titled “The Challenge of Three-Valued Logic”Comparisons involving NULL introduce a concept called three-valued logic (TRUE, FALSE, and UNKNOWN). Any direct comparison to NULL using standard operators like =, !=, or > results in UNKNOWN, not TRUE or FALSE. For example, NULL = NULL is UNKNOWN.
Because of this, you must use special operators IS NULL and IS NOT NULL to check for the presence of a missing value.
Syntax for Defining Nullable Columns
Section titled “Syntax for Defining Nullable Columns”When creating a table, you can specify whether a column can store NULL values. If you don’t specify NOT NULL, the column can hold NULLs by default.
CREATE TABLE employees ( id INTEGER PRIMARY KEY, -- Implicitly NOT NULL first_name TEXT NOT NULL, last_name TEXT NOT NULL, age INTEGER NOT NULL, department TEXT, -- Can be NULL salary REAL -- Can be NULL);Here, the department and salary columns are nullable, meaning they can be left empty when a new record is inserted.
Practical Examples
Section titled “Practical Examples”Let’s consider an employees table with the following records:
id | first_name | last_name | age | department | salary---|------------|-----------|-----|------------|--------1 | 'John' | 'Doe' | 32 | 'Sales' | 50000.02 | 'Jane' | 'Smith' | 25 | 'HR' | 45000.03 | 'Peter' | 'Jones' | 23 | 'Sales' | 48000.04 | 'Mary' | 'Williams'| 35 | 'IT' | 75000.05 | 'Chris' | 'Green' | 28 | NULL | NULLThe last record for ‘Chris Green’ has NULL for department and salary, perhaps because they are a new hire whose details are not yet finalized.
To find all employees with a known salary, use IS NOT NULL:
SELECT id, first_name, last_name, salaryFROM employeesWHERE salary IS NOT NULL;This query returns the first four records, excluding Chris.
id | first_name | last_name | salary---|------------|-----------|--------1 | 'John' | 'Doe' | 50000.02 | 'Jane' | 'Smith' | 45000.03 | 'Peter' | 'Jones' | 48000.04 | 'Mary' | 'Williams'| 75000.0To find all employees with a missing department, use IS NULL:
SELECT id, first_name, last_name, departmentFROM employeesWHERE department IS NULL;This query returns only the record for Chris Green.
id | first_name | last_name | department---|------------|-----------|------------5 | 'Chris' | 'Green' | NULLCommon Pitfalls and Best Practices
Section titled “Common Pitfalls and Best Practices”-
Incorrect Comparisons: Never use
column = NULLorcolumn != NULL. It will not work as expected. Always useIS NULLandIS NOT NULL. -
Aggregate Functions: Functions like
COUNT(column_name),SUM(), andAVG()ignoreNULLvalues.COUNT(*)counts all rows, whileCOUNT(salary)would only count rows wheresalaryis notNULL. Be mindful of this difference. -
Providing Default Values: When querying data,
NULLcan be inconvenient. Use theCOALESCE()function to provide a default value if a column isNULL. It returns the first non-NULL value in its argument list.
-- Display 'Unassigned' if department is NULLSELECT first_name, COALESCE(department, 'Unassigned') as departmentFROM employees;This powerful function simplifies handling NULL values in your application code.