Three-valued logic (TRUE, FALSE, UNKNOWN) in SQL - Time & Space Complexity
When working with SQL, conditions can be TRUE, FALSE, or UNKNOWN. Understanding how this affects query checks helps us see how much work the database does.
We want to know how the database handles these three logic values when filtering rows.
Analyze the time complexity of the following SQL WHERE clause using three-valued logic.
SELECT *
FROM employees
WHERE salary > 50000 AND department IS NOT NULL;
This query selects employees with salary over 50000 and a known department value, using TRUE, FALSE, and UNKNOWN logic.
Look at what repeats as the database checks each row.
- Primary operation: Evaluating the WHERE condition for each row.
- How many times: Once per row in the employees table.
As the number of rows grows, the database checks each row's condition once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 condition checks |
| 100 | 100 condition checks |
| 1000 | 1000 condition checks |
Pattern observation: The work grows directly with the number of rows, checking each once.
Time Complexity: O(n)
This means the database checks each row one time, so the work grows in a straight line with the number of rows.
[X] Wrong: "The UNKNOWN results cause extra repeated checks or loops."
[OK] Correct: The database treats UNKNOWN as a simple result like TRUE or FALSE and moves on, so it does not add extra work per row.
Understanding how SQL handles TRUE, FALSE, and UNKNOWN helps you explain how queries filter data efficiently, a useful skill in many database tasks.
What if we added an OR condition instead of AND? How would the time complexity change?
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
