AND, OR, NOT logical operators in MySQL - Time & Space Complexity
When using AND, OR, and NOT in MySQL queries, the database checks conditions to decide which rows to keep.
We want to understand how the time to check these conditions grows as the data gets bigger.
Analyze the time complexity of the following code snippet.
SELECT * FROM employees
WHERE department = 'Sales'
AND (salary > 50000 OR experience > 5)
AND NOT (status = 'Inactive');
This query selects employees in Sales who either earn more than 50000 or have more than 5 years experience, but excludes inactive ones.
- Primary operation: Checking each row against the combined conditions using AND, OR, NOT.
- How many times: Once for every row in the employees table.
As the number of rows grows, the database must check more rows one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 condition checks |
| 100 | About 100 condition checks |
| 1000 | About 1000 condition checks |
Pattern observation: The work grows directly with the number of rows; double the rows, double the checks.
Time Complexity: O(n)
This means the time to run the query grows in a straight line with the number of rows to check.
[X] Wrong: "Using AND, OR, NOT makes the query run much faster or slower regardless of data size."
[OK] Correct: The logical operators themselves just combine conditions; the main time depends on how many rows are checked, not the operators alone.
Understanding how logical operators affect query time helps you explain how databases filter data efficiently, a useful skill in many real tasks.
"What if we add an index on the department column? How would the time complexity change?"
Practice
all conditions to be true for the whole expression to be true?Solution
Step 1: Understand AND operator behavior
The AND operator returns true only if every condition joined by it is true.Step 2: Compare with other operators
OR requires at least one true condition, NOT reverses truth, XOR is exclusive or.Final Answer:
AND -> Option DQuick Check:
AND means all true = A [OK]
- Confusing AND with OR
- Thinking NOT requires all true
- Mixing XOR with AND
age is greater than 18 and status is 'active'?Solution
Step 1: Identify correct logical operator for both conditions
We want rows where both age > 18 and status = 'active' are true, so use AND.Step 2: Check syntax correctness
SELECT * FROM users WHERE age > 18 AND status = 'active'; uses AND correctly and has valid SQL syntax.Final Answer:
SELECT * FROM users WHERE age > 18 AND status = 'active'; -> Option AQuick Check:
AND joins conditions correctly = C [OK]
- Using OR instead of AND
- Incorrect NOT placement
- Syntax errors like missing AND
employees with columns department and salary, what rows will this query return?SELECT * FROM employees WHERE department = 'Sales' OR salary > 50000 AND NOT department = 'HR';
Solution
Step 1: Understand operator precedence
AND has higher precedence than OR, so condition is: department = 'Sales' OR (salary > 50000 AND NOT department = 'HR').Step 2: Interpret the conditions
Rows match if department is 'Sales', or if salary > 50000 and department is not 'HR'.Final Answer:
Rows where department is 'Sales' or salary > 50000 and department is not 'HR' -> Option CQuick Check:
AND before OR, NOT reverses condition = B [OK]
- Ignoring operator precedence
- Misreading NOT placement
- Assuming OR applies to all conditions
SELECT * FROM orders WHERE NOT order_date > '2023-01-01' AND status = 'shipped' OR;
Solution
Step 1: Check query syntax
The query ends with OR but no condition follows it, causing syntax error.Step 2: Verify other parts
NOT and AND usage is correct; order_date comparison is valid.Final Answer:
Missing condition after OR -> Option BQuick Check:
OR must have two conditions = A [OK]
- Leaving OR without a following condition
- Misplacing NOT operator
- Confusing AND and OR usage
Solution
Step 1: Translate the logic into conditions
We want customers NOT in 'VIP' group AND (have purchases OR balance > 1000).Step 2: Check operator precedence and parentheses
AND has higher precedence than OR, so parentheses around (purchases > 0 OR balance > 1000) ensure correct grouping.Step 3: Evaluate options
SELECT * FROM customers WHERE NOT group = 'VIP' AND (purchases > 0 OR balance > 1000); correctly uses NOT on group, AND with grouped OR conditions. Others misplace parentheses or NOT.Final Answer:
SELECT * FROM customers WHERE NOT group = 'VIP' AND (purchases > 0 OR balance > 1000); -> Option AQuick Check:
NOT group, AND grouped OR = D [OK]
- Missing parentheses causing wrong logic
- Applying NOT to wrong part
- Confusing AND and OR precedence
Ready to prove it?
Pass the test and this pattern moves unknown
