// conditional reference

SQL three-valued logic

The logic system behind every NULL comparison: three truth values instead of two.

Категория
Условные конструкции
Returns
TRUE / FALSE / UNKNOWN
In this category
21 of 43

Синтаксис

a AND b | a OR b | NOT a

Поддержка движка

ДвижокSupportЗаметки
SQL Server Да Supported under this name.
PostgreSQL Да Supported under this name.
MySQL Да Supported under this name.
SQLite Да Supported under this name.
Oracle Да Supported under this name.

Примеры

PostgreSQL

SELECT CASE WHEN NULL = NULL THEN 'equal' ELSE 'unknown' END AS result;

MySQL

SELECT CASE WHEN (NULL = 1) OR TRUE THEN 'kept' ELSE 'dropped' END AS result;

Полезно знать

  • Truth tables: TRUE AND UNKNOWN = UNKNOWN; TRUE OR UNKNOWN = TRUE; FALSE OR UNKNOWN = UNKNOWN; FALSE AND UNKNOWN = FALSE; NOT UNKNOWN = UNKNOWN.
  • WHERE, HAVING and ON keep only TRUE rows: UNKNOWN and FALSE are dropped the same way.
  • CHECK constraints pass UNKNOWN: a row violates the constraint only when the condition is FALSE, which is why NULL columns sneak past checks.
  • Aggregates like BOOL_AND skip NULL inputs rather than using UNKNOWN internally.

Questions about NULL

Is NULL = NULL true?

No, it is UNKNOWN; use IS NULL or IS NOT DISTINCT FROM when you need NULLs to compare equal.

Why did my WHERE drop NULL rows?

Comparisons against NULL yield UNKNOWN and WHERE keeps only TRUE; there is no way to 'match' NULL with =.

Does TRUE OR NULL save a row?

Yes: TRUE OR UNKNOWN = TRUE, so the row survives.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.