0
0
SQLquery~5 mins

Three-valued logic (TRUE, FALSE, UNKNOWN) in SQL - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Three-valued logic (TRUE, FALSE, UNKNOWN)
O(n)
Understanding Time 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.

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

As the number of rows grows, the database checks each row's condition once.

Input Size (n)Approx. Operations
1010 condition checks
100100 condition checks
10001000 condition checks

Pattern observation: The work grows directly with the number of rows, checking each once.

Final Time Complexity

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.

Common Mistake

[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.

Interview Connect

Understanding how SQL handles TRUE, FALSE, and UNKNOWN helps you explain how queries filter data efficiently, a useful skill in many database tasks.

Self-Check

What if we added an OR condition instead of AND? How would the time complexity change?