WHERE with OR operator in SQL - Time & Space Complexity
We want to understand how the time to run a SQL query changes when using the WHERE clause with an OR operator.
Specifically, how does the query time grow as the table gets bigger?
Analyze the time complexity of the following SQL query.
SELECT *
FROM employees
WHERE department = 'Sales'
OR department = 'Marketing';
This query selects all employees who work in either Sales or Marketing departments.
Look for repeated checks or scans in the query.
- Primary operation: Scanning each row in the employees table to check the department.
- How many times: Once for each row in the table.
As the number of employees grows, the database checks more rows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of rows.
Time Complexity: O(n)
This means the time to run the query grows in a straight line as the table gets bigger.
[X] Wrong: "Using OR makes the query twice as slow because it checks two conditions separately."
[OK] Correct: The database still scans each row once and checks both conditions together, so it does not double the work.
Understanding how conditions like OR affect query time helps you explain your reasoning clearly in interviews.
What if we changed OR to AND in the WHERE clause? How would the time complexity change?
Practice
WHERE clause with the OR operator do?Solution
Step 1: Understand the WHERE clause
The WHERE clause filters rows based on conditions.Step 2: Understand the OR operator
OR means if any condition is true, the row is selected.Final Answer:
It selects rows that meet at least one of the conditions. -> Option BQuick Check:
WHERE with OR = any condition true [OK]
- Thinking OR requires all conditions true
- Confusing OR with AND
- Assuming OR deletes or updates rows
WHERE clause with the OR operator to find employees in department 10 or 20?Solution
Step 1: Check correct OR syntax
OR must connect full conditions: column = value OR column = value.Step 2: Analyze each option
A uses AND incorrectly; C uses invalid syntax; D misuses IN without parentheses.Final Answer:
SELECT * FROM employees WHERE department_id = 10 OR department_id = 20; -> Option CQuick Check:
Correct OR syntax = SELECT * FROM employees WHERE department_id = 10 OR department_id = 20; [OK]
- Using AND instead of OR
- Writing OR inside parentheses incorrectly
- Misusing IN without parentheses
products with columns id, name, and category, what rows will this query return?SELECT * FROM products WHERE category = 'Books' OR category = 'Toys';
Solution
Step 1: Understand the OR condition
The query selects rows where category is 'Books' or 'Toys'.Step 2: Determine matching rows
Rows with category 'Books' or 'Toys' will be included, others excluded.Final Answer:
Products in either 'Books' or 'Toys' categories. -> Option AQuick Check:
OR selects rows matching any condition [OK]
- Thinking OR means both conditions must be true
- Assuming OR excludes matching rows
- Believing OR causes syntax error
SELECT * FROM orders WHERE status = 'Pending' OR;
Solution
Step 1: Analyze the WHERE clause
The WHERE clause ends with OR but no condition follows it.Step 2: Understand OR syntax
OR must connect two conditions; missing second condition causes syntax error.Final Answer:
Missing condition after OR operator. -> Option AQuick Check:
OR needs two conditions [OK]
- Leaving OR at end without condition
- Confusing OR with AND syntax
- Ignoring syntax errors in WHERE clause
students with columns id, grade, and city. Write a query to select students who have grade 'A' or live in 'New York' or 'Los Angeles'. Which query is correct?Solution
Step 1: Understand the conditions
Select students with grade 'A' OR city 'New York' OR city 'Los Angeles'.Step 2: Analyze each query
A has syntax error (missing column in last condition); B has syntax error and wrong logic; C has incorrect logic due to AND; D is correct and concise using IN.Final Answer:
SELECT * FROM students WHERE grade = 'A' OR city IN ('New York', 'Los Angeles'); -> Option DQuick Check:
Use OR with IN for multiple values [OK]
- Using AND between city conditions
- Omitting column name after OR
- Syntax errors with OR and IN
Ready to prove it?
Pass the test and this pattern moves unknown
