What if your database could think beyond just yes or no and handle 'maybe' too?
Why Three-valued logic (TRUE, FALSE, UNKNOWN) in SQL? - Purpose & Use Cases
Imagine you have a list of people and their phone numbers, but some phone numbers are missing. You want to find everyone who has a phone number starting with '555'. You try to check each phone number manually, but what do you do when the phone number is missing? You get stuck.
Manually checking each entry is slow and confusing. If you treat missing data as false or true, you get wrong answers. You might miss people who actually have numbers or include those who don't. This makes your results unreliable and wastes time.
Three-valued logic helps by adding a third option: UNKNOWN. Instead of just TRUE or FALSE, it knows when data is missing. This way, your queries handle missing information correctly and give you accurate results without confusion.
WHERE phone LIKE '555%' OR phone IS NULLWHERE phone LIKE '555%'It lets your database understand and work with missing or unknown data safely and correctly.
When filtering customers who have made a purchase, some records might not have purchase dates yet. Three-valued logic helps you find only those who definitely made purchases, ignoring unknown or missing data without errors.
Manual checks fail with missing data.
Three-valued logic adds UNKNOWN to TRUE and FALSE.
This improves accuracy and trust in query results.
Practice
= operator?Solution
Step 1: Understand comparison with NULL
In SQL, any comparison with NULL using=does not return TRUE or FALSE but UNKNOWN.Step 2: Recall three-valued logic rules
Since NULL means unknown, the comparison result is UNKNOWN, not TRUE or FALSE.Final Answer:
UNKNOWN -> Option DQuick Check:
Comparison with NULL = UNKNOWN [OK]
- Assuming NULL equals NULL returns TRUE
- Thinking NULL comparison returns FALSE
- Confusing NULL with a string 'NULL'
col contains NULL values?Solution
Step 1: Identify correct NULL check syntax
In SQL, to check for NULL values, you must useIS NULLbecause= NULLdoes not work.Step 2: Evaluate each option
WHERE col IS NULL usesIS NULL, which is the correct syntax. Others compare with NULL using=or treat NULL as a string, which is incorrect.Final Answer:
WHERE col IS NULL -> Option CQuick Check:
Use IS NULL to check NULL values [OK]
- Using = NULL instead of IS NULL
- Comparing NULL as a string 'NULL'
- Using != NULL to find non-NULL values
Employees with a column Bonus that can be NULL, what will the query SELECT * FROM Employees WHERE Bonus > 1000; return for rows where Bonus is NULL?Solution
Step 1: Understand comparison with NULL in WHERE clause
Comparing NULL with any value using>results in UNKNOWN, which is treated as FALSE in WHERE filtering.Step 2: Determine which rows are returned
Rows whereBonusis NULL do not satisfyBonus > 1000condition, so they are excluded from the result.Final Answer:
Rows with NULL Bonus are excluded -> Option AQuick Check:
NULL comparison in WHERE excludes rows [OK]
- Assuming NULL > 1000 returns TRUE
- Expecting NULL rows to appear in results
- Thinking query throws error due to NULL
SELECT * FROM Orders WHERE Status = 'Shipped' OR Status <> 'Shipped'; but it returns fewer rows than expected. What is the likely cause?Solution
Step 1: Analyze the WHERE clause logic
The condition checks if Status equals 'Shipped' OR Status not equals 'Shipped'.Step 2: Consider NULL values in Status
For rows where Status is NULL, both comparisons return UNKNOWN, so those rows are excluded.Final Answer:
Rows with NULL Status are excluded due to three-valued logic -> Option AQuick Check:
NULL comparisons exclude rows in OR conditions [OK]
- Assuming NULL rows satisfy OR condition
- Thinking query has syntax error
- Believing OR always returns all rows
Products where Discount is either greater than 20 or unknown (NULL). Which WHERE clause correctly implements this using three-valued logic?Solution
Step 1: Identify correct NULL check syntax
To check for NULL values, useIS NULL, not= NULL.Step 2: Combine conditions correctly
You want rows where Discount is greater than 20 OR Discount is NULL, so use OR withDiscount > 20andDiscount IS NULL.Final Answer:
WHERE Discount > 20 OR Discount IS NULL -> Option BQuick Check:
Use IS NULL with OR for NULL checks [OK]
- Using = NULL instead of IS NULL
- Using AND instead of OR
- Checking IS NOT NULL instead of IS NULL
Ready to prove it?
Pass the test and this pattern moves unknown
