Bird
Raised Fist0
SQLquery~10 mins

NULL in AND, OR, NOT logic in SQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - NULL in AND, OR, NOT logic
Start with two boolean values
↓
Apply AND operation
↓
Check if any operand is NULL
↓
Result may be NULL
↓
Apply OR operation
↓
Check if any operand is NULL
↓
Result may be NULL
↓
Apply NOT operation
↓
If operand is NULL, result is NULL
↓
End
This flow shows how SQL evaluates AND, OR, and NOT when NULL values are involved, resulting in TRUE, FALSE, or NULL outcomes.
Execution Sample
SQL
SELECT TRUE AND NULL AS and_result,
       FALSE OR NULL AS or_result,
       NOT NULL AS not_result;
This query shows how SQL returns results when NULL is used with AND, OR, and NOT operators.
Execution Table
StepExpressionOperandsEvaluationResult
1TRUE AND NULLTRUE, NULLTRUE AND unknownNULL
2FALSE OR NULLFALSE, NULLFALSE OR unknownNULL
3NOT NULLNULLNOT unknownNULL
4TRUE AND FALSETRUE, FALSETRUE AND FALSEFALSE
5FALSE OR TRUEFALSE, TRUEFALSE OR TRUETRUE
6NOT TRUETRUENOT TRUEFALSE
7NOT FALSEFALSENOT FALSETRUE
8TRUE AND TRUETRUE, TRUETRUE AND TRUETRUE
9FALSE OR FALSEFALSE, FALSEFALSE OR FALSEFALSE
10EndExecution complete
💡 All logical operations evaluated; NULL propagates as unknown in AND, OR, NOT.
Variable Tracker
ExpressionInitialAfter EvaluationFinal
and_resultTRUE AND NULLEvaluated as TRUE AND unknownNULL
or_resultFALSE OR NULLEvaluated as FALSE OR unknownNULL
not_resultNOT NULLEvaluated as NOT unknownNULL
Key Moments - 3 Insights
Why does TRUE AND NULL result in NULL instead of TRUE or FALSE?
Because NULL means unknown, TRUE AND unknown is unknown, so the result is NULL as shown in execution_table row 1.
Why does FALSE OR NULL result in NULL and not FALSE?
Since NULL is unknown, FALSE OR unknown is unknown, so the result is NULL, as seen in execution_table row 2.
What happens when NOT is applied to NULL?
NOT NULL is also unknown, so the result is NULL, demonstrated in execution_table row 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of TRUE AND NULL at step 1?
ATRUE
BNULL
CFALSE
DError
💡 Hint
Check the 'Result' column in execution_table row 1.
At which step does FALSE OR NULL evaluate to NULL?
AStep 9
BStep 5
CStep 2
DStep 4
💡 Hint
Look at the 'Expression' and 'Result' columns in execution_table.
If NOT NULL was changed to NOT TRUE, what would be the result according to the table?
AFALSE
BTRUE
CNULL
DError
💡 Hint
See execution_table row 6 for NOT TRUE evaluation.
Concept Snapshot
SQL NULL in logic:
- TRUE AND NULL = NULL (unknown)
- FALSE OR NULL = NULL (unknown)
- NOT NULL = NULL (unknown)
- NULL means unknown, propagates in AND, OR, NOT
- Results can be TRUE, FALSE, or NULL
- NULL is not TRUE or FALSE, but unknown
Full Transcript
This visual execution shows how SQL handles NULL in logical operations AND, OR, and NOT. When NULL is involved, the result often becomes NULL, meaning unknown. For example, TRUE AND NULL results in NULL because the second operand is unknown. Similarly, FALSE OR NULL results in NULL. Applying NOT to NULL also results in NULL. The execution table traces these evaluations step-by-step, showing how SQL treats NULL as unknown in logic. This helps understand why logical expressions with NULL do not simply return TRUE or FALSE but can return NULL to represent uncertainty.

Practice

(1/5)
1. What is the result of the SQL expression TRUE AND NULL?
easy
A. TRUE
B. NULL
C. FALSE
D. Error

Solution

  1. Step 1: Understand AND with NULL

    In SQL, AND returns FALSE if any side is FALSE. If one side is TRUE and the other is NULL, the result is NULL because the unknown value affects the outcome.
  2. Step 2: Apply to TRUE AND NULL

    Since the first operand is TRUE and the second is NULL, the result is NULL.
  3. Final Answer:

    NULL -> Option B
  4. Quick Check:

    TRUE AND NULL = NULL [OK]
Hint: AND with NULL returns NULL unless FALSE present [OK]
Common Mistakes:
  • Assuming TRUE AND NULL is TRUE
  • Confusing NULL with FALSE
  • Thinking NULL causes error
2. Which of the following is the correct SQL syntax to check if a column col is NOT NULL?
easy
A. col != NULL
B. col <> NULL
C. col IS NOT NULL
D. NOT col = NULL

Solution

  1. Step 1: Recall NULL comparison rules

    In SQL, you cannot use = or != to compare with NULL. Instead, use IS NULL or IS NOT NULL.
  2. Step 2: Identify correct syntax

    The correct way to check if col is not null is col IS NOT NULL.
  3. Final Answer:

    col IS NOT NULL -> Option C
  4. Quick Check:

    Use IS NOT NULL for null checks [OK]
Hint: Use IS NOT NULL, not != or <> [OK]
Common Mistakes:
  • Using != or <> to compare with NULL
  • Using NOT col = NULL which is invalid
  • Confusing NULL with empty string
3. Given a table Test with a column flag that can be TRUE, FALSE, or NULL, what is the result of this query?
SELECT * FROM Test WHERE flag OR FALSE;
medium
A. Rows where flag is TRUE
B. Rows where flag is TRUE or NULL
C. Rows where flag is TRUE or FALSE
D. No rows returned

Solution

  1. Step 1: Understand OR with FALSE

    In SQL, OR FALSE returns the value of the first operand because FALSE does not affect the OR result.
  2. Step 2: Evaluate flag OR FALSE

    If flag is TRUE, the expression is TRUE. If flag is FALSE, expression is FALSE. If flag is NULL, expression is NULL (unknown), which is treated as FALSE in WHERE filtering.
  3. Final Answer:

    Rows where flag is TRUE -> Option A
  4. Quick Check:

    flag OR FALSE = flag; WHERE filters TRUE only [OK]
Hint: OR FALSE returns first operand value [OK]
Common Mistakes:
  • Including NULL rows in result
  • Thinking OR FALSE always returns TRUE
  • Confusing OR with AND behavior
4. Consider this SQL WHERE clause:
WHERE NOT (col = NULL)

What is the problem with this condition?
medium
A. It always returns NULL (no rows match)
B. It always returns TRUE
C. It always returns FALSE
D. It causes a syntax error

Solution

  1. Step 1: Understand comparison with NULL

    In SQL, col = NULL always returns NULL (unknown), never TRUE or FALSE.
  2. Step 2: Apply NOT to NULL

    NOT NULL is still NULL, so the condition never evaluates to TRUE, filtering out all rows.
  3. Final Answer:

    It always returns NULL (no rows match) -> Option A
  4. Quick Check:

    NOT (col = NULL) = NULL, no matches [OK]
Hint: NOT NULL remains NULL, no rows pass filter [OK]
Common Mistakes:
  • Expecting NOT NULL to be TRUE
  • Using = NULL instead of IS NULL
  • Assuming syntax error occurs
5. You want to select rows from a table Employees where the ManagerID is either NULL or the employee is active (Active = TRUE). Which WHERE clause correctly handles NULL logic?
hard
A. WHERE ManagerID IS NOT NULL OR Active = TRUE
B. WHERE ManagerID = NULL OR Active = TRUE
C. WHERE NOT ManagerID = NULL AND Active = TRUE
D. WHERE ManagerID IS NULL OR Active = TRUE

Solution

  1. Step 1: Correctly check for NULL

    Use IS NULL to check if ManagerID is NULL. Using = NULL is invalid and returns NULL.
  2. Step 2: Combine conditions with OR

    The condition requires rows where ManagerID is NULL or Active is TRUE, so use OR between these checks.
  3. Final Answer:

    WHERE ManagerID IS NULL OR Active = TRUE -> Option D
  4. Quick Check:

    Use IS NULL and OR for correct logic [OK]
Hint: Use IS NULL, combine with OR for either condition [OK]
Common Mistakes:
  • Using = NULL instead of IS NULL
  • Using AND instead of OR
  • Using IS NOT NULL which is opposite
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard