WHERE with AND operator in SQL - Time & Space Complexity
We want to understand how the time to run a SQL query changes when we use the WHERE clause with the AND operator.
Specifically, how does adding multiple conditions affect the work the database does?
Analyze the time complexity of the following SQL query.
SELECT *
FROM employees
WHERE department = 'Sales'
AND salary > 50000
AND hire_date < '2020-01-01';
This query selects all employees who work in Sales, earn more than 50,000, and were hired before 2020.
Look at what the database does repeatedly to answer this query.
- Primary operation: Checking each row in the employees table against all conditions.
- How many times: Once for every row in the table.
As the number of employees grows, the database checks more rows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The work grows directly with the number of rows. More rows mean more checks.
Time Complexity: O(n)
This means the time to run the query grows in a straight line as the table gets bigger.
[X] Wrong: "Adding more conditions with AND makes the query run much slower, like multiplying the work."
[OK] Correct: The database still checks each row once; it just tests more conditions per row, which is very fast compared to scanning rows.
Understanding how conditions in WHERE affect query time helps you write efficient queries and explain your reasoning clearly in interviews.
"What if we added an index on the department column? How would that change the time complexity?"
Practice
WHERE clause with the AND operator do in an SQL query?Solution
Step 1: Understand the WHERE clause
The WHERE clause filters rows based on conditions given in the query.Step 2: Understand the AND operator
The AND operator requires all conditions to be true for a row to be included.Final Answer:
Filters rows that meet all specified conditions -> Option CQuick Check:
WHERE + AND = all conditions true [OK]
- Thinking AND means any condition can be true
- Confusing WHERE with JOIN
- Assuming AND sorts data
age is greater than 18 AND city is 'New York'?Solution
Step 1: Check the operator between conditions
The question asks for AND, so the operator must be AND, not OR or comma.Step 2: Check the comparison operators
Use single equals (=) for comparison in SQL, not double equals (==).Final Answer:
SELECT * FROM users WHERE age > 18 AND city = 'New York'; -> Option AQuick Check:
AND + = correct syntax [OK]
- Using OR instead of AND
- Using comma instead of AND
- Using == instead of = for comparison
employees with columns name, department, and salary, what will this query return?SELECT name FROM employees WHERE department = 'Sales' AND salary > 50000;
Solution
Step 1: Analyze the WHERE clause conditions
The query filters rows where department equals 'Sales' AND salary is greater than 50000.Step 2: Understand the AND effect
Only employees meeting both conditions will be selected, so names returned are from Sales with salary above 50000.Final Answer:
Names of employees in Sales with salary over 50000 -> Option AQuick Check:
AND filters both conditions true [OK]
- Thinking OR is used instead of AND
- Assuming it returns all employees with salary > 50000
- Believing syntax error occurs without OR
SELECT * FROM products WHERE price > 100 AND category 'Electronics';
Solution
Step 1: Check the WHERE clause syntax
The condition after AND is missing a comparison operator before 'Electronics'. It should be category = 'Electronics'.Step 2: Verify other parts
price > 100 is correct, AND is valid, and FROM is present.Final Answer:
Missing comparison operator (=) before 'Electronics' -> Option BQuick Check:
Comparison operator needed before value [OK]
- Omitting = in conditions
- Replacing AND with OR incorrectly
- Ignoring syntax errors in WHERE clause
orders with columns order_id, customer_id, status, and total. Write a query to find all orders with status 'shipped' AND total greater than 100, but exclude orders where customer_id is NULL. Which query is correct?Solution
Step 1: Combine conditions with AND
We want orders where status is 'shipped' AND total is greater than 100 AND customer_id is not NULL.Step 2: Use correct syntax for NULL check
In SQL, to check for NULL, use IS NOT NULL, not != NULL.Final Answer:
SELECT * FROM orders WHERE status = 'shipped' AND total > 100 AND customer_id IS NOT NULL; -> Option DQuick Check:
Use AND and IS NOT NULL for correct filtering [OK]
- Using OR instead of AND
- Using != NULL instead of IS NOT NULL
- Mixing AND and OR without parentheses
Ready to prove it?
Pass the test and this pattern moves unknown
