0
0
SQLquery~10 mins

Three-valued logic (TRUE, FALSE, UNKNOWN) in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Three-valued logic (TRUE, FALSE, UNKNOWN)
Evaluate Condition
Is condition TRUE?
YesResult TRUE
No
Is condition FALSE?
YesResult FALSE
No
Result UNKNOWN
SQL conditions can be TRUE, FALSE, or UNKNOWN (NULL). The logic checks TRUE first, then FALSE, else UNKNOWN.
Execution Sample
SQL
SELECT * FROM users WHERE age > 30 AND name IS NULL;
This query filters rows where age is greater than 30 and name is NULL, demonstrating three-valued logic.
Execution Table
StepConditionEvaluationResult
1age > 30TRUETRUE
2name IS NULLTRUETRUE
3TRUE AND TRUETRUERow included
4Next rowRepeat evaluationDepends on values
5No more rows-Query ends
💡 Rows where condition evaluates to UNKNOWN or FALSE are excluded from results.
Variable Tracker
VariableRow 1Row 2Row 3Final
age > 30TRUEFALSEUNKNOWNVaries per row
name IS NULLTRUEFALSEUNKNOWNVaries per row
Combined ConditionTRUEFALSEUNKNOWNDetermines row inclusion
Key Moments - 2 Insights
Why does 'name = NULL' evaluate to UNKNOWN instead of TRUE or FALSE?
In SQL, NULL means unknown value, so any comparison with NULL using '=' results in UNKNOWN, as shown in execution_table step 2.
Why are rows with UNKNOWN condition results excluded from the query output?
SQL WHERE clause only includes rows where condition is TRUE; UNKNOWN and FALSE exclude rows, as seen in execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of 'TRUE AND UNKNOWN' at step 3?
ATRUE
BUNKNOWN
CFALSE
DNULL
💡 Hint
Refer to execution_table row 3 under 'Result' column.
At which step does the query decide to exclude a row due to condition evaluation?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Check execution_table where 'Row excluded' is noted.
If 'name = NULL' was replaced with 'name IS NULL', how would the evaluation change at step 2?
AIt would evaluate to TRUE or FALSE, not UNKNOWN
BIt would still be UNKNOWN
CIt would cause an error
DIt would evaluate to NULL
💡 Hint
Consider how 'IS NULL' differs from '=' in SQL logic, check variable_tracker for condition changes.
Concept Snapshot
SQL uses three-valued logic: TRUE, FALSE, UNKNOWN (NULL).
Comparisons with NULL using '=' yield UNKNOWN.
WHERE clause filters only TRUE rows.
UNKNOWN acts like FALSE in filtering.
Use IS NULL to check NULL values explicitly.
Full Transcript
In SQL, conditions can be TRUE, FALSE, or UNKNOWN due to NULL values. When evaluating a condition like 'age > 30 AND name = NULL', the first part may be TRUE, but 'name = NULL' results in UNKNOWN because NULL means unknown value. SQL treats UNKNOWN as neither TRUE nor FALSE, so rows with UNKNOWN conditions are excluded from query results. This three-valued logic ensures that NULLs are handled carefully. Using 'IS NULL' instead of '=' checks for NULL explicitly and returns TRUE or FALSE, avoiding UNKNOWN. Understanding this helps avoid unexpected query results.