0
0
SQLquery~10 mins

Operator precedence in WHERE in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Operator precedence in WHERE
Start: Evaluate WHERE clause
Evaluate NOT operators
Evaluate AND operators
Evaluate OR operators
Filter rows based on final condition
Output rows
The WHERE clause conditions are evaluated following operator precedence: NOT first, then AND, then OR, to decide which rows to keep.
Execution Sample
SQL
SELECT * FROM employees
WHERE NOT department = 'Sales' AND salary > 50000 OR age < 30;
This query filters employees by combining NOT, AND, and OR conditions with precedence rules.
Execution Table
StepCondition EvaluatedEvaluation ResultExplanation
1NOT department = 'Sales'TRUE or FALSE per rowNOT flips TRUE/FALSE of department='Sales'
2NOT department = 'Sales' AND salary > 50000TRUE or FALSE per rowAND requires both sides TRUE
3(NOT department = 'Sales' AND salary > 50000) OR age < 30TRUE or FALSE per rowOR requires either side TRUE
4Filter rows where final condition is TRUERows keptOnly rows passing final condition remain
💡 All rows evaluated; rows not meeting final condition are excluded
Variable Tracker
Rowdepartment='Sales'NOT department='Sales'salary>50000AND resultage<30Final condition
1TRUEFALSETRUEFALSEFALSEFALSE
2FALSETRUETRUETRUEFALSETRUE
3FALSETRUEFALSEFALSETRUETRUE
4TRUEFALSEFALSEFALSETRUETRUE
Key Moments - 3 Insights
Why does NOT apply before AND and OR in the WHERE clause?
Because operator precedence rules say NOT is evaluated first, so in step 1 of the execution_table, NOT flips the condition before AND and OR combine results.
What happens if we ignore operator precedence and evaluate left to right?
The final filtered rows would be different because AND and OR combine conditions differently; the execution_table rows 2 and 3 show how AND and OR combine after NOT.
How does parentheses change the evaluation order?
Parentheses force evaluation inside them first, overriding precedence. Without parentheses, the order is NOT, then AND, then OR as shown in the concept_flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the variable_tracker, what is the value of 'NOT department='Sales'' for row 2?
ANULL
BTRUE
CFALSE
DDepends on salary
💡 Hint
Check the second row under 'NOT department='Sales'' column in variable_tracker
At which step in the execution_table is the AND operator evaluated?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Condition Evaluated' column for AND operator in execution_table
If we add parentheses like WHERE NOT (department = 'Sales' AND salary > 50000) OR age < 30, how does the evaluation order change?
ANOT applies to entire AND condition first
BAND applies after OR
COR applies before NOT
DNo change in order
💡 Hint
Parentheses force evaluation inside first, changing order from concept_flow
Concept Snapshot
WHERE clause evaluates conditions using operator precedence:
1. NOT is evaluated first
2. THEN AND combines conditions
3. THEN OR combines conditions
Use parentheses to override this order
This controls which rows are filtered in the query
Full Transcript
In SQL, the WHERE clause filters rows by evaluating conditions. The order of evaluation follows operator precedence: NOT is evaluated first, then AND, then OR. This means NOT flips conditions before AND combines them, and OR combines the results last. For example, in the query filtering employees, NOT department='Sales' is evaluated first for each row. Then AND combines that with salary>50000. Finally, OR includes rows where age<30. Parentheses can change this order by forcing evaluation inside them first. Understanding this order helps predict which rows the query returns.