WHERE with comparison operators in SQL - Time & Space Complexity
When we use WHERE with comparison operators in SQL, the database checks each row to see if it matches the condition.
We want to understand how the time to run the query grows as the table gets bigger.
Analyze the time complexity of the following code snippet.
SELECT *
FROM Employees
WHERE Salary > 50000;
This query selects all employees whose salary is greater than 50,000.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking the salary value of each row against 50,000.
- How many times: Once for every row in the Employees table.
As the number of rows grows, the database must check more salaries.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 salary checks |
| 100 | 100 salary checks |
| 1000 | 1000 salary 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: "The database only looks at some rows, so the time stays the same no matter how big the table is."
[OK] Correct: Without special help like indexes, the database must check every row to be sure which ones match.
Understanding how filtering with WHERE works helps you explain how databases handle data efficiently in real projects.
"What if we add an index on the Salary column? How would the time complexity change?"
Practice
WHERE clause do in an SQL query?Solution
Step 1: Understand the purpose of WHERE
The WHERE clause is used to select only those rows that meet a specific condition.Step 2: Compare with other SQL clauses
Sorting is done by ORDER BY, joining by JOIN, and creating tables by CREATE TABLE, so these are not correct.Final Answer:
Filters rows based on a condition -> Option AQuick Check:
WHERE filters rows = A [OK]
- Confusing WHERE with ORDER BY
- Thinking WHERE creates or joins tables
- Assuming WHERE changes table structure
age is greater than 30?Solution
Step 1: Identify correct comparison operator
The operator for 'greater than' is >, soage > 30is correct.Step 2: Check syntax correctness
The option with SELECT * FROM table WHERE age = > 30; has invalid operator '= >', the one with '>>' uses bitwise shift, and the one with '>=' means 'greater or equal to', not strictly greater.Final Answer:
SELECT * FROM table WHERE age > 30; -> Option BQuick Check:
Use > for greater than, not >= or >> [OK]
- Using '= >' instead of '>'
- Confusing '>' with '>>'
- Using '>=' when only '>' is needed
Employees with columns id, name, and salary, what rows will this query return?SELECT * FROM Employees WHERE salary <= 50000;
Solution
Step 1: Understand the comparison operator used
The operator '<=' means 'less than or equal to', so it selects salaries 50000 or less.Step 2: Interpret the query result
The query returns all rows where salary is 50000 or below, not just exactly 50000 or above 50000.Final Answer:
All employees with salary less than or equal to 50000 -> Option CQuick Check:
salary <= 50000 means salary ≤ 50000 [OK]
- Thinking <= means only equal
- Confusing <= with <
- Assuming syntax error in query
SELECT * FROM Products WHERE price <>;
Solution
Step 1: Analyze the WHERE clause
The operator '<>' means 'not equal', but it must be followed by a value to compare.Step 2: Identify syntax error
Here, '<>' is not followed by any value, causing a syntax error.Final Answer:
Missing value after comparison operator -> Option DQuick Check:
Comparison operators need a value after them [OK]
- Leaving out value after operator
- Confusing <> with != (both valid but must have value)
- Assuming table or SELECT keyword is missing
Orders table where the order_date is after January 1, 2023, and the total_amount is at least 100. Which query correctly uses WHERE with comparison operators?Solution
Step 1: Understand the date condition
We want orders after January 1, 2023, so use 'order_date > '2023-01-01''.Step 2: Understand the amount condition
We want total_amount at least 100, so use 'total_amount >= 100'. Both conditions must be true, so use AND.Step 3: Check each option
SELECT * FROM Orders WHERE order_date > '2023-01-01' AND total_amount >= 100; matches both conditions correctly. SELECT * FROM Orders WHERE order_date >= '2023-01-01' OR total_amount > 100; uses OR and >= for date, which is incorrect. SELECT * FROM Orders WHERE order_date < '2023-01-01' AND total_amount > 100; uses < for date, wrong direction. SELECT * FROM Orders WHERE order_date = '2023-01-01' AND total_amount = 100; uses = which is too strict.Final Answer:
SELECT * FROM Orders WHERE order_date > '2023-01-01' AND total_amount >= 100; -> Option AQuick Check:
Use > for after date, >= for at least amount, AND to combine [OK]
- Using OR instead of AND
- Using >= for date when strictly after is needed
- Using = instead of comparison operators
Ready to prove it?
Pass the test and this pattern moves unknown
