Bird
Raised Fist0
SQLquery~10 mins

WHERE with IS NULL and IS NOT NULL in SQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - WHERE with IS NULL and IS NOT NULL
Start Query
↓
Scan Table Rows
↓
Check WHERE Condition
↓
Is value IS NULL?
Yes→Include Row
↓
Is value IS NOT NULL?
Yes→Include Row
↓
Exclude Row
↓
Return Filtered Rows
↓
End Query
The query scans each row, checks if a column is NULL or NOT NULL based on the WHERE clause, and includes or excludes rows accordingly.
Execution Sample
SQL
SELECT * FROM employees
WHERE manager_id IS NULL;
This query selects all employees who do not have a manager assigned (manager_id is NULL).
Execution Table
StepRow Data (id, name, manager_id)Condition CheckedCondition ResultRow Included?
1(1, 'Alice', NULL)manager_id IS NULLTrueYes
2(2, 'Bob', 1)manager_id IS NULLFalseNo
3(3, 'Charlie', 1)manager_id IS NULLFalseNo
4(4, 'Diana', NULL)manager_id IS NULLTrueYes
5(5, 'Eve', 3)manager_id IS NULLFalseNo
6End of rowsQuery ends
💡 All rows checked; only rows with manager_id NULL included.
Variable Tracker
VariableStartAfter Row 1After Row 2After Row 3After Row 4After Row 5Final
Current RowNone(1, 'Alice', NULL)(2, 'Bob', 1)(3, 'Charlie', 1)(4, 'Diana', NULL)(5, 'Eve', 3)End
Condition ResultN/ATrueFalseFalseTrueFalseN/A
Rows Included[][(1, 'Alice', NULL)][(1, 'Alice', NULL)][(1, 'Alice', NULL)][(1, 'Alice', NULL), (4, 'Diana', NULL)][(1, 'Alice', NULL), (4, 'Diana', NULL)][(1, 'Alice', NULL), (4, 'Diana', NULL)]
Key Moments - 2 Insights
Why does the condition 'manager_id IS NULL' only include rows where manager_id is exactly NULL?
Because IS NULL checks specifically for NULL values, not zero or empty strings. Rows with any other value fail the condition, as shown in execution_table rows 2, 3, and 5.
What happens if we use 'IS NOT NULL' instead?
The query would include only rows where manager_id has a value (not NULL). This is the opposite of the current condition, so rows included would be those excluded here (rows 2, 3, and 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the condition result for row 4?
ATrue
BFalse
CUnknown
DError
💡 Hint
Check the 'Condition Result' column for step 4 in the execution_table.
At which step does the query exclude a row because the condition is false?
AStep 1
BStep 4
CStep 2
DStep 6
💡 Hint
Look for the first 'False' in the 'Condition Result' column and see if the row was included.
If we change the condition to 'manager_id IS NOT NULL', which rows would be included?
ARows 1 and 4
BRows 2, 3, and 5
CAll rows
DNo rows
💡 Hint
Refer to the explanation in key_moments about IS NOT NULL including rows with values.
Concept Snapshot
WHERE with IS NULL and IS NOT NULL:
- Use IS NULL to filter rows where a column has no value (NULL).
- Use IS NOT NULL to filter rows where a column has any value.
- NULL is not equal to anything, so use IS NULL, not '= NULL'.
- The query checks each row and includes it if the condition matches.
- Useful to find missing or present data in columns.
Full Transcript
This visual execution shows how a SQL query with WHERE and IS NULL works. The query scans each row of the employees table. For each row, it checks if the manager_id column is NULL. If yes, the row is included in the result. Otherwise, it is excluded. The execution table lists each row, the condition checked, the result, and whether the row is included. The variable tracker shows how the current row and included rows change step by step. Key moments clarify common confusions about NULL checks. The quiz tests understanding of condition results and row inclusion. The snapshot summarizes the key points about using IS NULL and IS NOT NULL in WHERE clauses.

Practice

(1/5)
1. Which SQL clause correctly finds rows where the email column has no value stored (is missing)?
easy
A. WHERE email = NULL
B. WHERE email = ''
C. WHERE email IS NULL
D. WHERE email IS NOT NULL

Solution

  1. Step 1: Understand NULL meaning in SQL

    NULL means no value or missing data, not an empty string or zero.
  2. Step 2: Use correct syntax to check NULL

    To check for NULL, use IS NULL, not = NULL or empty string.
  3. Final Answer:

    WHERE email IS NULL -> Option C
  4. Quick Check:

    Check NULL with IS NULL = WHERE email IS NULL [OK]
Hint: Use IS NULL to find missing values, never = NULL [OK]
Common Mistakes:
  • Using = NULL instead of IS NULL
  • Confusing empty string '' with NULL
  • Using IS NOT NULL to find missing values
2. Which of the following is the correct syntax to select rows where the phone column is NOT missing?
easy
A. SELECT * FROM contacts WHERE phone IS NOT NULL;
B. SELECT * FROM contacts WHERE phone = NOT NULL;
C. SELECT * FROM contacts WHERE phone != NULL;
D. SELECT * FROM contacts WHERE phone NOT NULL;

Solution

  1. Step 1: Recall correct syntax for NOT NULL check

    SQL uses IS NOT NULL to find rows where value exists.
  2. Step 2: Identify syntax errors in other options

    Options B, C, D use invalid syntax like = NOT NULL or != NULL or missing IS.
  3. Final Answer:

    SELECT * FROM contacts WHERE phone IS NOT NULL; -> Option A
  4. Quick Check:

    Use IS NOT NULL to find existing values = SELECT * FROM contacts WHERE phone IS NOT NULL; [OK]
Hint: Use IS NOT NULL, not = NOT NULL or != NULL [OK]
Common Mistakes:
  • Using = NOT NULL instead of IS NOT NULL
  • Using != NULL which is invalid
  • Omitting IS keyword
3. Given the table employees with columns id, name, and manager_id where some employees have no manager (NULL), what will this query return?
SELECT name FROM employees WHERE manager_id IS NULL;
medium
A. All employees who have a manager assigned
B. All employees who do not have a manager assigned
C. All employees including those with and without managers
D. Syntax error due to IS NULL usage

Solution

  1. Step 1: Understand IS NULL condition

    The condition manager_id IS NULL filters rows where manager_id has no value (missing).
  2. Step 2: Interpret query result

    The query returns names of employees without any manager assigned (NULL manager_id).
  3. Final Answer:

    All employees who do not have a manager assigned -> Option B
  4. Quick Check:

    IS NULL filters missing values = All employees who do not have a manager assigned [OK]
Hint: IS NULL finds missing values, so returns employees without managers [OK]
Common Mistakes:
  • Thinking IS NULL finds rows with values
  • Confusing NULL with empty string
  • Assuming syntax error with IS NULL
4. Identify the error in this query that tries to select rows where address is NOT NULL:
SELECT * FROM customers WHERE address != NULL;
medium
A. Should use = NULL instead of != NULL
B. Missing quotes around NULL
C. No error; query is correct
D. Using != NULL is invalid; should use IS NOT NULL

Solution

  1. Step 1: Check NULL comparison syntax

    In SQL, NULL cannot be compared with = or != operators.
  2. Step 2: Use correct syntax for NOT NULL

    The correct way is IS NOT NULL to check for non-missing values.
  3. Final Answer:

    Using != NULL is invalid; should use IS NOT NULL -> Option D
  4. Quick Check:

    Use IS NOT NULL, not != NULL [OK]
Hint: Never use = or != with NULL; use IS (NOT) NULL [OK]
Common Mistakes:
  • Using != NULL or = NULL
  • Adding quotes around NULL
  • Assuming != NULL works like other values
5. You have a table orders with columns order_id, customer_id, and shipped_date. Some orders are not shipped yet and have shipped_date as NULL. Which query lists all orders that have been shipped?
hard
A. SELECT * FROM orders WHERE shipped_date IS NOT NULL;
B. SELECT * FROM orders WHERE shipped_date IS NULL;
C. SELECT * FROM orders WHERE shipped_date != NULL;
D. SELECT * FROM orders WHERE shipped_date = NULL;

Solution

  1. Step 1: Understand meaning of shipped_date NULL

    NULL means order not shipped yet, so shipped orders have non-NULL dates.
  2. Step 2: Use correct syntax to find shipped orders

    Use IS NOT NULL to find rows where shipped_date has a value.
  3. Final Answer:

    SELECT * FROM orders WHERE shipped_date IS NOT NULL; -> Option A
  4. Quick Check:

    IS NOT NULL finds shipped orders = SELECT * FROM orders WHERE shipped_date IS NOT NULL; [OK]
Hint: IS NOT NULL finds rows with values, perfect for shipped orders [OK]
Common Mistakes:
  • Using = NULL or != NULL which are invalid
  • Using IS NULL to find shipped orders
  • Confusing NULL with empty string
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