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?