0
0
MySQLquery~10 mins

AND, OR, NOT logical operators in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - AND, OR, NOT logical operators
Start with condition1
Evaluate condition2
Apply AND operator
Apply OR operator
Apply NOT operator
Result: TRUE or FALSE
Use result to filter rows
Logical operators combine or modify conditions to filter database rows based on TRUE or FALSE results.
Execution Sample
MySQL
SELECT * FROM employees
WHERE (department = 'Sales' AND salary > 50000)
OR NOT (active = 1);
This query selects employees who are in Sales with salary over 50000, or who are not active.
Execution Table
StepCondition EvaluatedValue 1Value 2OperatorResult
1department = 'Sales'Sales--TRUE
2salary > 5000060000--TRUE
3TRUE AND TRUETRUETRUEANDTRUE
4active = 10--FALSE
5NOT FALSEFALSE-NOTTRUE
6TRUE OR TRUETRUETRUEORTRUE
7Row included in result---YES
💡 All conditions evaluated; final result TRUE means row is included.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6Final
department = 'Sales'unknownTRUETRUETRUETRUETRUETRUETRUE
salary > 50000unknownunknownTRUETRUETRUETRUETRUETRUE
active = 1unknownunknownunknownunknownFALSEFALSEFALSEFALSE
AND resultunknownunknownunknownTRUETRUETRUETRUETRUE
NOT resultunknownunknownunknownunknownunknownTRUETRUETRUE
OR resultunknownunknownunknownunknownunknownunknownTRUETRUE
Key Moments - 3 Insights
Why does the NOT operator change FALSE to TRUE in step 5?
NOT reverses the condition result. Since active = 1 is FALSE (active is 0), NOT FALSE becomes TRUE as shown in step 5.
Why is the AND operator result TRUE in step 3?
AND returns TRUE only if both conditions are TRUE. department = 'Sales' and salary > 50000 are both TRUE, so AND result is TRUE in step 3.
How does the OR operator affect the final result in step 6?
OR returns TRUE if either side is TRUE. Here, AND result is TRUE and NOT result is TRUE, so OR result is TRUE, including the row.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of 'salary > 50000' at step 2?
ATRUE
BFALSE
CUNKNOWN
DNULL
💡 Hint
Check the 'Value 1' column at step 2 showing salary 60000 > 50000.
At which step does the NOT operator change the condition result?
AStep 4
BStep 5
CStep 3
DStep 6
💡 Hint
Look for the step where 'NOT' is listed as the operator.
If 'active' was 1 instead of 0, what would be the OR result at step 6?
AUNKNOWN
BFALSE
CTRUE
DNULL
💡 Hint
If active=1, then active=1 condition is TRUE, NOT TRUE is FALSE, but AND result is TRUE, so OR TRUE OR FALSE is TRUE.
Concept Snapshot
AND, OR, NOT combine conditions in SQL WHERE clauses.
AND requires both conditions TRUE.
OR requires at least one TRUE.
NOT reverses TRUE/FALSE.
Used to filter rows based on multiple criteria.
Full Transcript
This visual execution shows how SQL logical operators AND, OR, and NOT work step-by-step. We start by evaluating simple conditions like department = 'Sales' and salary > 50000. Then AND combines these two conditions, returning TRUE only if both are TRUE. Next, we evaluate active = 1, which is FALSE in this example. The NOT operator reverses this FALSE to TRUE. Finally, OR combines the AND result and the NOT result. Since both are TRUE, the OR result is TRUE, so the row is included in the query result. The variable tracker shows how each condition's value changes at each step. Key moments clarify common confusions about how NOT reverses values and how AND and OR combine conditions. The quiz tests understanding of these steps by asking about specific condition results and operator effects.