NULL Handling

This page summarizes how NULL values are handled in CockroachDBSQL. Each topic is demonstrated via the built-in SQLclient.

Note:

When using the built-in client, NULL values are displayed using the word NULL. This distinguishes them from a character field that contains an empty string ("").

NULLs and simple comparisons

Any simple comparison between a value and NULL results inNULL. The remaining cases are described in the next question.

This behavior is consistent with PostgreSQL as well as all other major RDBMS's.

  1. > INSERT INTO customers (customer_id, cust_name, cust_email) VALUES (1, 'Smith', NULL);
  1. > CREATE TABLE t1(
  2. a INT,
  3. b INT,
  4. c INT
  5. );
  1. > INSERT INTO t1 VALUES(1, 0, 0);
  1. > INSERT INTO t1 VALUES(2, 0, 1);
  1. > INSERT INTO t1 VALUES(3, 1, 0);
  1. > INSERT INTO t1 VALUES(4, 1, 1);
  1. > INSERT INTO t1 VALUES(5, NULL, 0);
  1. > INSERT INTO t1 VALUES(6, NULL, 1);
  1. > INSERT INTO t1 VALUES(7, NULL, NULL);
  1. > SELECT * FROM t1;
  1. +---+------+------+
  2. | a | b | c |
  3. +---+------+------+
  4. | 1 | 0 | 0 |
  5. | 2 | 0 | 1 |
  6. | 3 | 1 | 0 |
  7. | 4 | 1 | 1 |
  8. | 5 | NULL | 0 |
  9. | 6 | NULL | 1 |
  10. | 7 | NULL | NULL |
  11. +---+------+------+
  1. > SELECT * FROM t1 WHERE b < 10;
  1. +---+---+---+
  2. | a | b | c |
  3. +---+---+---+
  4. | 1 | 0 | 0 |
  5. | 2 | 0 | 1 |
  6. | 3 | 1 | 0 |
  7. | 4 | 1 | 1 |
  8. +---+---+---+
  1. > SELECT * FROM t1 WHERE NOT b > 10;
  1. +---+---+---+
  2. | a | b | c |
  3. +---+---+---+
  4. | 1 | 0 | 0 |
  5. | 2 | 0 | 1 |
  6. | 3 | 1 | 0 |
  7. | 4 | 1 | 1 |
  8. +---+---+---+
  1. > SELECT * FROM t1 WHERE b < 10 OR c = 1;
  1. +---+------+---+
  2. | a | b | c |
  3. +---+------+---+
  4. | 1 | 0 | 0 |
  5. | 2 | 0 | 1 |
  6. | 3 | 1 | 0 |
  7. | 4 | 1 | 1 |
  8. | 6 | NULL | 1 |
  9. +---+------+---+
  1. > SELECT * FROM t1 WHERE b < 10 AND c = 1;
  1. +---+---+---+
  2. | a | b | c |
  3. +---+---+---+
  4. | 2 | 0 | 1 |
  5. | 4 | 1 | 1 |
  6. +---+---+---+
  1. > SELECT * FROM t1 WHERE NOT (b < 10 AND c = 1);
  1. +---+------+---+
  2. | a | b | c |
  3. +---+------+---+
  4. | 1 | 0 | 0 |
  5. | 3 | 1 | 0 |
  6. | 5 | NULL | 0 |
  7. +---+------+---+
  1. > SELECT * FROM t1 WHERE NOT (c = 1 AND b < 10);
  1. +---+------+---+
  2. | a | b | c |
  3. +---+------+---+
  4. | 1 | 0 | 0 |
  5. | 3 | 1 | 0 |
  6. | 5 | NULL | 0 |
  7. +---+------+---+

Use the IS NULL or IS NOT NULL clauses when checking for NULL values.

  1. > SELECT * FROM t1 WHERE b IS NULL AND c IS NOT NULL;
  1. +---+------+---+
  2. | a | b | c |
  3. +---+------+---+
  4. | 5 | NULL | 0 |
  5. | 6 | NULL | 1 |
  6. +---+------+---+

NULLs and conditional operators

The conditionaloperators(including IF, COALESCE, IFNULL) only evaluate someoperands depending on the value of a condition operand, so theirresult is not always NULL depending on the given operands.

For example, COALESCE(1, NULL) will always return 1 even thoughthe second operand is NULL.

NULLs and ternary logic

AND, OR and IS implement ternary logic, as follows.

ExpressionResult
FALSE AND FALSEFALSE
FALSE AND TRUEFALSE
FALSE AND NULLFALSE
TRUE AND FALSEFALSE
TRUE AND TRUETRUE
TRUE AND NULLNULL
NULL AND FALSEFALSE
NULL AND TRUENULL
NULL AND NULLNULL
ExpressionResult
FALSE OR FALSEFALSE
FALSE OR TRUETRUE
FALSE OR NULLNULL
TRUE OR FALSETRUE
TRUE OR TRUETRUE
TRUE OR NULLTRUE
NULL OR FALSENULL
NULL OR TRUETRUE
NULL OR NULLNULL
ExpressionResult
FALSE IS FALSETRUE
FALSE IS TRUEFALSE
FALSE IS NULLFALSE
TRUE IS FALSEFALSE
TRUE IS TRUETRUE
TRUE IS NULLFALSE
NULL IS FALSEFALSE
NULL IS TRUEFALSE
NULL IS NULLTRUE

NULLs and arithmetic

Arithmetic operations involving a NULL value will yield a NULL result.

  1. > SELECT a, b, c, b*0, b*c, b+c FROM t1;
  1. +---+------+------+-------+-------+-------+
  2. | a | b | c | b * 0 | b * c | b + c |
  3. +---+------+------+-------+-------+-------+
  4. | 1 | 0 | 0 | 0 | 0 | 0 |
  5. | 2 | 0 | 1 | 0 | 0 | 1 |
  6. | 3 | 1 | 0 | 0 | 0 | 1 |
  7. | 4 | 1 | 1 | 0 | 1 | 2 |
  8. | 5 | NULL | 0 | NULL | NULL | NULL |
  9. | 6 | NULL | 1 | NULL | NULL | NULL |
  10. | 7 | NULL | NULL | NULL | NULL | NULL |
  11. +---+------+------+-------+-------+-------+

NULLs and aggregate functions

Aggregate functions are those that operate on a set of rows and return a single value. The example data has been repeated here to make it easier to understand the results.

  1. > SELECT * FROM t1;
  1. +---+------+------+
  2. | a | b | c |
  3. +---+------+------+
  4. | 1 | 0 | 0 |
  5. | 2 | 0 | 1 |
  6. | 3 | 1 | 0 |
  7. | 4 | 1 | 1 |
  8. | 5 | NULL | 0 |
  9. | 6 | NULL | 1 |
  10. | 7 | NULL | NULL |
  11. +---+------+------+
  1. > SELECT COUNT(*), COUNT(b), SUM(b), AVG(b), MIN(b), MAX(b) FROM t1;
  1. +----------+----------+--------+--------------------+--------+--------+
  2. | COUNT(*) | COUNT(b) | SUM(b) | AVG(b) | MIN(b) | MAX(b) |
  3. +----------+----------+--------+--------------------+--------+--------+
  4. | 7 | 4 | 2 | 0.5000000000000000 | 0 | 1 |
  5. +----------+----------+--------+--------------------+--------+--------+

Note the following:

  • NULL values are not included in the COUNT() of a column. COUNT(*) returns 7 while COUNT(b) returns 4.

  • NULL values are not considered as high or low values in MIN() or MAX().

  • AVG(b) returns SUM(b)/COUNT(b), which is different than AVG(*) as NULL values are not considered in the COUNT(b) of rows. See NULLs as Other Values for more details.

NULL as a distinct value

NULL values are considered distinct from other values and are included in the list of distinct values from a column.

  1. > SELECT DISTINCT b FROM t1;
  1. +------+
  2. | b |
  3. +------+
  4. | 0 |
  5. | 1 |
  6. | NULL |
  7. +------+

However, counting the number of distinct values excludes NULLs, which is consistent with the COUNT() function.

  1. > SELECT COUNT(DISTINCT b) FROM t1;
  1. +-------------------+
  2. | count(DISTINCT b) |
  3. +-------------------+
  4. | 2 |
  5. +-------------------+

NULLs as other values

In some cases, you may want to include NULL values in arithmetic or aggregate function calculations. To do so, use the IFNULL() function to substitute a value for NULL during calculations.

For example, let's say you want to calculate the average value of column b as being the SUM() of all numbers in b divided by the total number of rows, regardless of whether b's value is NULL. In this case, you would use AVG(IFNULL(b, 0)), where IFNULL(b, 0) substitutes a value of zero (0) for NULLs during the calculation.

  1. > SELECT COUNT(*), COUNT(b), SUM(b), AVG(b), AVG(IFNULL(b, 0)), MIN(b), MAX(b) FROM t1;
  1. +----------+----------+--------+--------------------+--------------------+--------+--------+
  2. | COUNT(*) | COUNT(b) | SUM(b) | AVG(b) | AVG(IFNULL(b, 0)) | MIN(b) | MAX(b) |
  3. +----------+----------+--------+--------------------+--------------------+--------+--------+
  4. | 7 | 4 | 2 | 0.5000000000000000 | 0.2857142857142857 | 0 | 1 |
  5. +----------+----------+--------+--------------------+--------------------+--------+--------+

NULLs and set operations

NULL values are considered as part of a UNION set operation.

  1. > SELECT b FROM t1 UNION SELECT b FROM t1;
  1. +------+
  2. | b |
  3. +------+
  4. | 0 |
  5. | 1 |
  6. | NULL |
  7. +------+

NULLs and sorting

When sorting a column containing NULL values, CockroachDB sorts NULL values first with ASC and last with DESC. This differs from PostgreSQL, which sorts NULL values last with ASC and first with DESC.

Note that the NULLS FIRST and NULLS LAST options of the ORDER BY clause are not implemented in CockroachDB, so you cannot change where NULL values appear in the sort order.

  1. > SELECT * FROM t1 ORDER BY b ASC;
  1. +---+------+------+
  2. | a | b | c |
  3. +---+------+------+
  4. | 6 | NULL | 1 |
  5. | 5 | NULL | 0 |
  6. | 7 | NULL | NULL |
  7. | 1 | 0 | 0 |
  8. | 2 | 0 | 1 |
  9. | 4 | 1 | 1 |
  10. | 3 | 1 | 0 |
  11. +---+------+------+
  1. > SELECT * FROM t1 ORDER BY b DESC;
  1. +---+------+------+
  2. | a | b | c |
  3. +---+------+------+
  4. | 4 | 1 | 1 |
  5. | 3 | 1 | 0 |
  6. | 2 | 0 | 1 |
  7. | 1 | 0 | 0 |
  8. | 7 | NULL | NULL |
  9. | 6 | NULL | 1 |
  10. | 5 | NULL | 0 |
  11. +---+------+------+

NULLs and unique constraints

NULL values are not considered unique. Therefore, if a table has a Unique constraint on one or more columns that are optional (nullable), it is possible to insert multiple rows with NULL values in those columns, as shown in the example below.

  1. > CREATE TABLE t2(a INT, b INT UNIQUE);
  1. > INSERT INTO t2 VALUES(1, 1);
  1. > INSERT INTO t2 VALUES(2, NULL);
  1. > INSERT INTO t2 VALUES(3, NULL);
  1. > SELECT * FROM t2;
  1. +---+------+
  2. | a | b |
  3. +---+------+
  4. | 1 | 1 |
  5. | 2 | NULL |
  6. | 3 | NULL |
  7. +---+------+

NULLs and CHECK Constraints

A CHECK constraint expression that evaluates to NULL is considered to pass, allowing for concise expressions like discount < price without worrying about adding OR discount IS NULL clauses. When non-null validation is desired, the usual NOT NULL constraint can be used along side a Check constraint.

  1. > CREATE TABLE products (id STRING PRIMARY KEY, price INT NOT NULL CHECK (price > 0), discount INT, CHECK (discount <= price));
  1. > INSERT INTO products (id, price) VALUES ('ncc-1701-d', 100);
  1. > INSERT INTO products (id, price, discount) VALUES ('ncc-1701-a', 100, 50);
  1. > SELECT * FROM products;
  1. +----------+-------+----------+
  2. | id | price | discount |
  3. +----------+-------+----------+
  4. | ncc1701a | 100 | 50 |
  5. | ncc1701d | 100 | NULL |
  6. +----------+-------+----------+
  1. > INSERT INTO products (id, price) VALUES ('ncc-1701-b', -5);
  1. failed to satisfy CHECK constraint (price > 0)
  1. > INSERT INTO products (id, price, discount) VALUES ('ncc-1701-b', 100, 150);
  1. failed to satisfy CHECK constraint (discount <= price)

Was this page helpful?
YesNo