Toolvito

Works on this device

SQL Null Comparison Tester

See what SQL really does with NULL. Compare two values and get TRUE, FALSE or UNKNOWN, with the way PostgreSQL, MySQL, SQL Server, Oracle and SQLite write the null-safe comparison. Check how COUNT, SUM, AVG, ORDER BY, IN, NOT IN and joins treat NULLs. No SQL is run; the rules are worked out here.

Enter your values

Type two values and choose a comparison. You get TRUE, FALSE or UNKNOWN, what WHERE and CHECK do with it, and how each engine writes it.

Your values

A number such as 42, a text in single quotes such as 'abc', or NULL.

A number, a text in single quotes, or NULL. Compare a number with a number, or a text with a text.

Your values are worked out in this browser. They are not sent to a server.

Your result will show here

Fill in the values, then choose Test.

How to use this tester

  1. Choose what to test. Compare two values that may be NULL is the first choice.
  2. Type each value as a number such as 42, a text in single quotes such as 'abc', or NULL.
  3. Choose a comparison and then Test. The table shows how each engine writes it and its result there.
  4. For a column of values, choose COUNT, SUM, AVG, IN and NOT IN with NULLs and type one value on each line.

The rules

  • A comparison such as = or <> with NULL on either side is UNKNOWN. Even NULL = NULL is UNKNOWN.
  • WHERE and JOIN ON keep a row only when the condition is TRUE. A CHECK constraint rejects a row only when it is FALSE, so UNKNOWN passes.
  • NOT UNKNOWN is UNKNOWN. FALSE AND UNKNOWN is FALSE. TRUE OR UNKNOWN is TRUE.
  • IS NULL, IS NOT NULL and the null-safe comparisons are always TRUE or FALSE.
  • COUNT(col), SUM, AVG, MIN and MAX leave NULLs out. COUNT(*) counts every row. SUM of only NULLs is NULL.
  • x NOT IN (1, 2, NULL) is never TRUE. NOT EXISTS does not have this problem.

Null-safe comparison in each engine

Null-safe equal and not equal
Engine Null-safe equal Null-safe not equal
PostgreSQL 16 and 17 a IS NOT DISTINCT FROM b a IS DISTINCT FROM b
MySQL 8.0 and 8.4 a <=> b NOT (a <=> b)
SQL Server 2022 a IS NOT DISTINCT FROM b a IS DISTINCT FROM b
SQL Server 2019 CASE WHEN a = b OR (a IS NULL AND b IS NULL) THEN 1 ELSE 0 END = 1 the same with THEN 0 ELSE 1
Oracle Database 19c and 23ai DECODE(a, b, 1, 0) = 1 DECODE(a, b, 0, 1) = 1
SQLite 3.39 and later a IS b or a IS NOT DISTINCT FROM b a IS NOT b or a IS DISTINCT FROM b

Where engines differ

  • Oracle stores the empty text '' as NULL. So '' = '' is UNKNOWN and '' IS NULL is TRUE there.
  • ORDER BY puts NULLs last in ascending order in PostgreSQL and Oracle, and first in MySQL, SQL Server and SQLite. NULLS FIRST and NULLS LAST work in PostgreSQL, Oracle and SQLite.
  • AVG of an int column in SQL Server is an int: the AVG of 1 and 2 is 1. Cast the column to decimal first.
  • Collation: MySQL and SQL Server usually compare text without letter case, so 'abc' = 'ABC' is TRUE there. This tool compares text exactly.

Worked examples

1 = NULL is UNKNOWN, so WHERE 1 = NULL keeps no row. 1 IS DISTINCT FROM NULL is TRUE.

For the values 10, NULL, 20, NULL, 30: COUNT(*) is 5, COUNT(col) is 3, SUM is 60 and AVG is 20 (60 ÷ 3, not 60 ÷ 5). 5 NOT IN (the values) is UNKNOWN because of the NULLs.

Left keys 1, 2, NULL, 4 and right keys 1, 1, NULL, 3: INNER JOIN ON l.key = r.key gives 2 rows (the key 1 twice). The NULL keys match only with IS NOT DISTINCT FROM, which gives 3 rows.

Limits you should know

  • No database is used. The results follow the rules of standard SQL and the documented rules of each engine.
  • A number is compared only with a number, and a text with a text. How engines convert one kind to another is not covered.
  • Text is compared exactly, character by character. Collations that ignore case, accents or spaces at the end give other results.

Related tools