0
0
SQLquery~10 mins

WHERE with OR operator in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - WHERE with OR operator
Start Query
Scan Table Rows
Evaluate WHERE Condition
Condition 1 True?
YesInclude Row
Condition 2 True?
YesInclude Row
Exclude Row
Return Filtered Rows
End Query
The query scans each row, checks if either condition in the OR is true, includes the row if yes, otherwise excludes it.
Execution Sample
SQL
SELECT * FROM employees
WHERE department = 'Sales' OR salary > 70000;
This query selects employees who are either in the Sales department or have a salary greater than 70000.
Execution Table
StepRow DataCondition: department='Sales'Condition: salary>70000OR ResultInclude Row?
1{id:1, department:'Sales', salary:65000}TrueFalseTrueYes
2{id:2, department:'HR', salary:72000}FalseTrueTrueYes
3{id:3, department:'IT', salary:60000}FalseFalseFalseNo
4{id:4, department:'Sales', salary:75000}TrueTrueTrueYes
5{id:5, department:'Marketing', salary:50000}FalseFalseFalseNo
ExitAll rows processed---Query ends
💡 All rows checked; rows included if either condition is true.
Variable Tracker
VariableStartAfter Row 1After Row 2After Row 3After Row 4After Row 5Final
department='Sales'N/ATrueFalseFalseTrueFalseN/A
salary>70000N/AFalseTrueFalseTrueFalseN/A
OR ResultN/ATrueTrueFalseTrueFalseN/A
Include Row?N/AYesYesNoYesNoN/A
Key Moments - 3 Insights
Why does the query include a row if only one of the conditions is true?
Because the OR operator returns true if at least one condition is true, as shown in the execution_table rows 1 and 2 where only one condition is true but the row is included.
What happens if both conditions are false for a row?
The OR result is false, so the row is excluded. This is shown in rows 3 and 5 of the execution_table.
Does the OR operator check both conditions even if the first is true?
In SQL, the OR operator evaluates conditions until it finds one true condition. However, for clarity, the table shows both conditions evaluated for each row.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the OR Result for row 3?
ATrue
BFalse
CUnknown
DDepends on database
💡 Hint
Check the 'OR Result' column for Step 3 in the execution_table.
At which step does the condition 'salary > 70000' become true for the first time?
AStep 2
BStep 1
CStep 4
DStep 5
💡 Hint
Look at the 'Condition: salary>70000' column in the execution_table.
If the OR operator was replaced with AND, which row(s) would be included?
ARows 1 and 4
BRows 2 and 4
COnly Row 4
DAll rows
💡 Hint
Check which rows have both conditions true in the execution_table.
Concept Snapshot
WHERE with OR operator:
- Syntax: WHERE condition1 OR condition2
- Returns rows where at least one condition is true
- Evaluates each row's conditions
- Includes row if OR result is true
- Useful for filtering multiple criteria
Full Transcript
This visual execution shows how the SQL WHERE clause with the OR operator works. The query scans each row in the employees table and checks two conditions: if the department is 'Sales' or if the salary is greater than 70000. For each row, it evaluates both conditions and then applies the OR operator. If either condition is true, the row is included in the result. The execution table lists each row's data, condition results, the OR result, and whether the row is included. Key moments clarify why rows with one true condition are included and what happens when both are false. The quiz tests understanding of condition evaluation and the effect of changing OR to AND.