0
0
SQLquery~10 mins

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

Choose your learning style9 modes available
Concept Flow - WHERE with IS NULL and IS NOT NULL
Start Query
Scan Table Rows
Check WHERE Condition
Is value IS NULL?
YesInclude Row
Is value IS NOT NULL?
YesInclude 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.