Operator precedence in WHERE in SQL - Time & Space Complexity
We want to understand how the order of operations in a WHERE clause affects how many checks the database does.
How does the way conditions are grouped change the work done when filtering data?
Analyze the time complexity of the following code snippet.
SELECT *
FROM Employees
WHERE (department = 'Sales' AND salary > 50000) OR experience > 5;
This query filters employees who are either in Sales with salary above 50000 or have more than 5 years of experience.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each row against the WHERE conditions.
- How many times: Once per row in the Employees table.
Each row is checked one time through the conditions, so the work grows as the number of rows grows.
| 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 filter grows linearly with the number of rows in the table.
[X] Wrong: "Changing the order of AND and OR in WHERE changes how many rows are checked."
[OK] Correct: The database still checks every row once; only the logic grouping changes which rows pass, not how many are checked.
Understanding how conditions combine helps you write clear queries and reason about their performance, a useful skill in real projects.
"What if we added parentheses to change the condition grouping? How would that affect the time complexity?"
Practice
WHERE clause without parentheses: AND or OR?Solution
Step 1: Understand SQL operator precedence
In SQL,ANDhas higher precedence thanOR. This meansANDconditions are evaluated beforeORconditions when no parentheses are used.Step 2: Apply to WHERE clause evaluation
Without parentheses, SQL evaluates allANDconditions first, then appliesOR. This affects which rows match the query.Final Answer:
ANDis evaluated beforeOR-> Option BQuick Check:
Operator precedence: AND > OR [OK]
- Thinking OR is evaluated before AND
- Assuming both have equal precedence
- Believing evaluation order depends on database
WHERE clauses is syntactically correct in SQL?Solution
Step 1: Check logical operator usage
WHERE age > 30 AND salary < 50000 OR city = 'NY' usesANDandORcorrectly between conditions. Options A, B, and D have misplaced or missing operators.Step 2: Validate syntax correctness
SQL requires logical operators between conditions. WHERE age > 30 AND salary < 50000 OR city = 'NY' follows this rule, so it is syntactically valid.Final Answer:
WHERE age > 30 AND salary < 50000 OR city = 'NY' -> Option DQuick Check:
Logical operators must separate conditions [OK]
- Placing AND or OR without conditions
- Missing operators between conditions
- Using operators in wrong order
Employees with columns age, salary, and city, what rows will this query return?SELECT * FROM Employees WHERE age > 30 OR salary < 50000 AND city = 'NY';
Solution
Step 1: Apply operator precedence rules
SinceANDhas higher precedence thanOR, the condition is evaluated as:age > 30 OR (salary < 50000 AND city = 'NY').Step 2: Interpret the WHERE clause
The query returns rows where either age is greater than 30, or both salary is less than 50000 and city is 'NY'.Final Answer:
Rows where age > 30 or (salary < 50000 and city = 'NY') -> Option AQuick Check:
AND evaluated before OR [OK]
- Ignoring operator precedence and grouping incorrectly
- Assuming OR groups before AND
- Misreading the condition order
SELECT * FROM Employees WHERE age > 25 OR salary < 40000 AND;
Solution
Step 1: Analyze the WHERE clause syntax
The query ends withAND;which means the AND operator is not followed by a condition. This is a syntax error.Step 2: Confirm other parts are correct
The SELECT and FROM clauses are present and column names seem valid. The error is specifically the incomplete WHERE clause.Final Answer:
Missing condition after AND operator -> Option CQuick Check:
Logical operators must have conditions on both sides [OK]
- Leaving dangling AND or OR without conditions
- Forgetting to complete WHERE clause
- Assuming syntax is correct despite missing parts
WHERE clause correctly uses operator precedence to get this result?Options:
A) WHERE age > 40 AND salary < 60000 OR city = 'LA'
B) WHERE (age > 40 OR salary < 60000) AND city = 'LA'
C) WHERE age > 40 OR (salary < 60000 AND city = 'LA')
D) WHERE (age > 40 OR city = 'LA') AND salary < 60000
Solution
Step 1: Understand the desired logic
You want employees who are either older than 40, or who both work in 'LA' and have salary less than 60000.Step 2: Apply operator precedence and parentheses
Since AND has higher precedence, a clause like WHERE age > 40 AND salary < 60000 OR city = 'LA' groups as (age > 40 AND salary < 60000) OR city = 'LA'. But for our desired logic age > 40 OR (salary < 60000 AND city = 'LA'), we need explicit parentheses as in WHERE age > 40 OR (salary < 60000 AND city = 'LA').Step 3: Compare options
WHERE age > 40 AND salary < 60000 OR city = 'LA' groups as (age > 40 AND salary < 60000) OR city = 'LA' -- incorrect. WHERE (age > 40 OR salary < 60000) AND city = 'LA' requires city = 'LA' even for age > 40 -- incorrect. WHERE (age > 40 OR city = 'LA') AND salary < 60000 requires salary < 60000 always -- incorrect. WHERE age > 40 OR (salary < 60000 AND city = 'LA') correctly implements the logic.Final Answer:
WHERE age > 40 OR (salary < 60000 AND city = 'LA') -> Option AQuick Check:
Use parentheses to group AND conditions with OR correctly [OK]
- Not using parentheses and misreading condition grouping
- Assuming OR groups before AND
- Changing logic by wrong parentheses placement
Ready to prove it?
Pass the test and this pattern moves unknown
