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.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.
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.