Quick revision
The short answer
SQL uses three truth values: TRUE, FALSE and UNKNOWN, and any ordinary comparison with NULL gives UNKNOWN. So NULL = NULL is UNKNOWN, NULL <> 1 is UNKNOWN, and NOT (NULL = 1) is still UNKNOWN, because NOT UNKNOWN is UNKNOWN. WHERE keeps only rows whose condition is TRUE, so all three filter the row out, exactly as FALSE would.
What to remember
In this answer, we saw that SQL logic has three values and that any comparison with NULL is UNKNOWN, so NULL = NULL, NULL <> 1 and NOT (NULL = 1) are all UNKNOWN. WHERE and JOIN ON keep only TRUE, so all three drop the row, while CHECK rejects only FALSE and lets UNKNOWN through. TRUE OR UNKNOWN and FALSE AND UNKNOWN still resolve, and IS NULL, IS DISTINCT FROM and MySQL's <=> give us null-safe comparisons.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
SQL uses three truth values: TRUE, FALSE and UNKNOWN, and any ordinary comparison with NULL gives UNKNOWN. So NULL = NULL is UNKNOWN, NULL <> 1 is UNKNOWN, and NOT (NULL = 1) is still UNKNOWN, because NOT UNKNOWN is UNKNOWN. WHERE keeps only rows whose condition is TRUE, so all three filter the row out, exactly as FALSE would.
Why this question is asked
NULL causes more silent bugs in SQL than any other feature. A query returns fewer rows than expected, a report total does not match, a NOT IN finds nothing, and nobody gets an error. All of it comes from one idea: SQL logic has a third value. Interviewers ask about it because once we understand three-valued logic, these bugs become predictable. In this answer, we will recap what NULL means, evaluate the three expressions from the question, see how WHERE, JOIN and CHECK treat UNKNOWN differently, look at the cases where UNKNOWN still resolves to TRUE or FALSE, and finish with the null-safe comparisons we should use.
1A Quick Recap of What NULL Means
In SQL, NULL is not a value like 0 or the empty string. It is a marker that says the value is missing or unknown. If a customer's birth_date is NULL, we do not know when they were born. That has an important consequence: if we compare an unknown value with 1, we cannot say whether it equals 1. It might, or it might not. SQL represents this honest answer with a third truth value, UNKNOWN.
So SQL's logic is three-valued: every condition evaluates to TRUE, FALSE or UNKNOWN. In PostgreSQL, a boolean expression that is UNKNOWN is shown as NULL, which is why we see NULL in the output below.
2Evaluating the Three Expressions
Let us run the three expressions from the question directly.
The first one surprises people most. Two unknown birth dates are not known to be equal; they are two separate unknowns. The third shows that negation does not rescue us. NOT turns TRUE into FALSE and FALSE into TRUE, but there is nothing to flip in UNKNOWN, so it stays UNKNOWN.
3How WHERE and JOIN Treat UNKNOWN
A WHERE clause keeps a row only when its condition is TRUE. FALSE rows are dropped, and so are UNKNOWN rows. The ON condition of a JOIN follows the same rule: two rows are matched only when the condition is TRUE. This is why a row with a NULL never appears in the results of an equality or inequality filter.
| Condition | Value for row 3 | Kept by WHERE? | Allowed by CHECK? |
|---|---|---|---|
| x = 1 | UNKNOWN | No | Yes |
| x <> 1 | UNKNOWN | No | Yes |
| NOT (x = 1) | UNKNOWN | No | Yes |
| x IS NULL | TRUE | Yes | Yes |
Notice that x = 1 and x <> 1 together do not return all three rows. Row 3 falls through both, because neither condition is TRUE for it. Many people expect a condition and its negation to split a table into two complete halves, and with NULLs that expectation is wrong.
4CHECK Constraints Work the Other Way
A CHECK constraint is a rule the database enforces on every row we insert or update. Here the standard makes the opposite choice: a CHECK rejects a row only when the condition is FALSE. UNKNOWN is accepted. The reasoning is that we cannot prove the row breaks the rule, so we let it in.
So a CHECK does not prevent NULLs. If the column must always have a positive price, we need NOT NULL as well as the CHECK.
5When UNKNOWN Still Resolves
UNKNOWN does not always spread through an expression. If the answer is the same whatever the unknown turns out to be, SQL gives that answer. TRUE OR anything is TRUE, so TRUE OR UNKNOWN is TRUE. FALSE AND anything is FALSE, so FALSE AND UNKNOWN is FALSE. Only the remaining combinations stay UNKNOWN.
A good way to reason about it is to replace UNKNOWN with both TRUE and FALSE in turn. If both give the same result, that is the answer; if they differ, the answer is UNKNOWN.
6Comparing Values in a Null-Safe Way
When we really want NULL to be treated as a comparable value, for example to detect whether a column changed, we use the null-safe operators. IS NULL and IS NOT NULL test for the marker. PostgreSQL, and the SQL standard, provide IS DISTINCT FROM and IS NOT DISTINCT FROM, which treat two NULLs as equal and a NULL and a value as different. MySQL has the operator <=>, which does the same as IS NOT DISTINCT FROM.
These are the right tool for change detection, such as UPDATE ... WHERE price IS DISTINCT FROM new_price, which would otherwise silently skip rows whose old price was NULL. MySQL 8.4 does not support the IS DISTINCT FROM spelling, so there we write a <=> b for the null-safe equality and NOT (a <=> b) for the null-safe difference.
7Follow-up questions
Why do aggregates like COUNT(col) skip NULLs but COUNT(*) does not?
COUNT(*) counts rows, and a row exists whether or not some of its columns are NULL. COUNT(col) counts known values of that column, so it skips the rows where the value is missing. The other aggregates follow the same idea: SUM, AVG, MIN and MAX ignore NULLs, so AVG(rating) is the average of the known ratings, not of all rows. This is usually what we want, but it means COUNT(*) and COUNT(col) can differ, and SUM over only NULLs returns NULL rather than 0.
Does ORDER BY treat NULLs as equal?
For sorting and grouping, SQL treats all NULLs as one group, so GROUP BY puts them together and DISTINCT keeps one NULL. In ordering, PostgreSQL puts NULLs last in ascending order and first in descending order by default, and we can say NULLS FIRST or NULLS LAST explicitly. MySQL sorts NULLs first in ascending order.
Why does a UNIQUE constraint allow several NULLs?
Because two NULLs are not known to be equal, so they do not count as duplicates. PostgreSQL 15 and later let us write UNIQUE NULLS NOT DISTINCT when we want at most one NULL.
8Conclusion
In this answer, we saw that SQL logic has three values and that any comparison with NULL is UNKNOWN, so NULL = NULL, NULL <> 1 and NOT (NULL = 1) are all UNKNOWN. WHERE and JOIN ON keep only TRUE, so all three drop the row, while CHECK rejects only FALSE and lets UNKNOWN through. TRUE OR UNKNOWN and FALSE AND UNKNOWN still resolve, and IS NULL, IS DISTINCT FROM and MySQL's <=> give us null-safe comparisons.
Open with
SQL has three-valued logic: a condition is TRUE, FALSE or UNKNOWN, and any normal comparison involving NULL gives UNKNOWN, because NULL means the value is not known. So NULL equals NULL is UNKNOWN, NULL not equal to 1 is UNKNOWN, and NOT of NULL equals 1 is still UNKNOWN, since negating an unknown is still unknown.
Then, if they want more
WHERE and JOIN ON keep only rows where the condition is TRUE, so in all three cases the row is filtered out just like FALSE. That's why x equals 1 and x not equal to 1 together don't return the NULL rows.
CHECK constraints are the opposite: they reject only FALSE, so a NULL passes a CHECK unless the column is also NOT NULL. Some expressions still resolve: TRUE OR UNKNOWN is TRUE and FALSE AND UNKNOWN is FALSE.
When I need to compare values including NULLs, I use IS NULL, IS NOT NULL, and in PostgreSQL IS DISTINCT FROM or IS NOT DISTINCT FROM, or the <=> operator in MySQL. Aggregates follow a related rule: COUNT of a column skips NULLs, while COUNT star counts rows.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- MySQL 8.4, PostgreSQL 15
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, The SQL Language
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain three-valued logic in SQL. What do NULL = NULL, NULL <> 1, and NOT (NULL = 1)...": https://scrivio-interview-library.netlify.app/databases/sql-fundamentals/83106aeb6b/