In SQL, three-value logic is an important concept, and its existence is mainly due toNULL
Introduction of values.NULL
Represents an unknown value, which is neither an empty string nor a number 0, but a special tag indicating that data is missing or unavailable.
In SQL, becauseNULL
The existence of a value leads to it using a special logical method:Three-Valued Logic (3VL)
It includes three possible results of any logical calculation:
TRUE (true)
FALSE (Fake)
UNKNOWN (Unknown)
NULL
In SQL, it means "unknown" or "missed value", which is very different from normal values. becauseNULL
Indicates unknown value, so any withNULL
The result of the calculation should beUNKNOWN
, not TRUE or FALSE.
Features of NULL
NULL
It is not a specific value, but a special state, which has the following characteristics:
NULL
Can't compare with each other:NULL = NULL
The result is not TRUE, but UNKNOWN.NULL
Participate in numerical operations, the result isNULL
:NULL + 10
The result is stillNULL
。-
NULL
Participating in logical operations will affect the logical results:TRUE AND NULL
,turn outUNKNOWN
FALSE OR NULL
,turn outUNKNOWN
If the WHERE condition result isUNKNOWN
, then the record will not be included in the query result.
The influence of NULL in SQL logical operations
1. Logical operations (AND, OR, NOT)
AND operation
expression | result |
---|---|
TRUE AND TRUE | TRUE |
TRUE AND FALSE | FALSE |
TRUE AND UNKNOWN | UNKNOWN |
FALSE AND UNKNOWN | FALSE |
UNKNOWN AND UNKNOWN | UNKNOWN |
OR operation
expression | result |
---|---|
TRUE OR UNKNOWN | TRUE |
FALSE OR UNKNOWN | UNKNOWN |
UNKNOWN OR UNKNOWN | UNKNOWN |
NOT operation
expression | result |
---|---|
NOT TRUE | FALSE |
NOT FALSE | TRUE |
NOT UNKNOWN | UNKNOWN |
2. NULL participate in comparison (=, !=, >, <, etc.)
expression | result |
---|---|
NULL = NULL | UNKNOWN |
NULL != NULL | UNKNOWN |
NULL > 10 | UNKNOWN |
NULL < 10 | UNKNOWN |
NULL IS NULL | TRUE |
NULL IS NOT NULL | FALSE |
3. The impact of NULL in IN and NOT IN
ifNULL
Appears inIN
orNOT IN
In a statement, unpredictable results will be caused:
SELECT * FROM users WHERE age IN (20, 30, NULL);
becauseNULL
It is an unknown value, SQL does not knowNULL
Whether it belongs toage
,lead toUNKNOWN
, the final query will only matchage=20
andage=30
, but will not matchNULL
。
More serious problems arise inNOT IN
middle:
SELECT * FROM users WHERE age NOT IN (20, 30, NULL);
becauseNULL
existIN
The statement will returnUNKNOWN
, the wholeNOT IN
BecomeUNKNOWN
, no data will be returned in the end.
Solution:
SELECT * FROM users WHERE age NOT IN (20, 30) OR age IS NULL;
4. NULL in DISTINCT, GROUP BY and ORDER BY
-
DISTINCTSee
NULL
is the same value:SELECT DISTINCT category FROM products;
if
category
There are multiple columnsNULL
,DISTINCT
Only one will be retainedNULL
。 -
GROUP BYSee
NULL
For a group:SELECT category, COUNT(*) FROM products GROUP BY category;
all
NULL
Values will be classified into the same group. -
ORDER BYdeal with
NULL
:SELECT * FROM employees ORDER BY salary ASC;
NULL
The default is the first or last, and the specific behavior depends on the database:PostgreSQL:
NULLS FIRST
orNULLS LAST
MySQL:
NULL
The default is the topSQL Server:
NULL
The default is the top
5. NULL is handled in COALESCE and IFNULL
To avoidNULL
Influence query, you can useCOALESCE
orIFNULL
Processing:
-
COALESCE(expr1, expr2, ..., exprN)
: Return the first non-NULL valueSELECT name, COALESCE(email, 'unknown') AS email FROM users;
-
IFNULL(expr, default_value)
(Special for MySQL)SELECT name, IFNULL(email, 'unknown') AS email FROM users;
The impact of NULL in JOIN
ifNULL
Exist inJOIN
In the associated column, the row will not be matched:
SELECT * FROM orders LEFT JOIN customers ON orders.customer_id = ;
iforders.customer_id
yesNULL
,= NULL
turn outUNKNOWN
,lead toINNER JOIN
fail.
LEFT JOIN
Can be retainedorders
butcustomers
The data isNULL
。
How to handle NULL correctly
-
Use IS NULL and IS NOT NULL when querying
SELECT * FROM users WHERE email IS NULL;
-
Avoid NULL affecting logical operations
SELECT * FROM orders WHERE discount IS NULL OR discount > 10;
-
Consider the possible problems that NULL can bring in JOIN
SELECT * FROM orders LEFT JOIN customers ON orders.customer_id = WHERE IS NOT NULL;
-
Use COALESCE() to process NULL
SELECT name, COALESCE(email, 'unknown') AS email FROM users;
-
Use NOT IN correctly
SELECT * FROM users WHERE age NOT IN (20, 30) OR age IS NULL;
Summary (key point)
- NULL represents unknown, not an empty string or 0.
- SQL uses three-value logic (TRUE, FALSE, UNKNOWN), which causes NULL to return UNKNOWN when participating in operations.
- NULL cannot be compared directly with =, but should use IS NULL and IS NOT NULL.
- NULL may affect queries such as JOIN, GROUP BY, ORDER BY, IN/NOT IN, and must be handled with caution.
- Using COALESCE(), IFNULL() and other functions can avoid the problems caused by NULL.
This is the article about the specific use of three-value logic and NULL in SQL. For more related SQL three-value logic and NULL content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!