WHERE clause filtering in MySQL - Time & Space Complexity
When we use a WHERE clause in a database query, it helps us find only the rows we want.
We want to know how the time to get results changes as the table gets bigger.
Analyze the time complexity of the following code snippet.
SELECT *
FROM employees
WHERE department = 'Sales';
This query finds all employees who work in the Sales department by checking each row.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each row in the employees table to see if the department is 'Sales'.
- How many times: Once for every row in the table.
As the number of rows grows, the database checks more rows one by one.
| 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.
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 a few rows even if the table is large."
[OK] Correct: Without an index, the database must check every row to find matches, so time grows with table size.
Understanding how filtering scales helps you explain query speed and shows you know how databases work under the hood.
What if we added an index on the department column? How would the time complexity change?
Practice
WHERE clause do in a SQL query?Solution
Step 1: Understand the purpose of WHERE clause
The WHERE clause is used to filter rows that meet a specific condition in a SELECT query.Step 2: Differentiate from 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 specified condition -> Option AQuick Check:
WHERE clause = filters rows [OK]
- Confusing WHERE with ORDER BY
- Thinking WHERE creates or joins tables
- Assuming WHERE changes table structure
employees where the age is greater than 30?Solution
Step 1: Check the basic SELECT syntax
The correct syntax to select all columns is SELECT * FROM table_name.Step 2: Verify the WHERE condition syntax
The comparison operator for greater than is >, not =>. Also, the condition should match the question (age > 30).Final Answer:
SELECT * FROM employees WHERE age > 30; -> Option BQuick Check:
Correct syntax with > operator [OK]
- Using => instead of >
- Omitting FROM keyword
- Using wrong comparison operator
products with columns id, name, and price, what will the query SELECT name FROM products WHERE price <= 50; return?Solution
Step 1: Understand the WHERE condition
The condition price <= 50 means selecting rows where price is less than or equal to 50.Step 2: Identify the selected columns
The query selects only the name column from those filtered rows.Final Answer:
All product names with price less than or equal to 50 -> Option AQuick Check:
price <= 50 filters products [OK]
- Confusing <= with >= operator
- Assuming it returns all products
- Thinking <= causes syntax error
SELECT * FROM orders WHERE status = 'shipped' OR;Solution
Step 1: Analyze the WHERE clause
The WHERE clause ends with OR but has no condition after it, which is invalid syntax.Step 2: Check other parts of the query
Single quotes around 'shipped' are correct, semicolon is optional, and * is valid to select all columns.Final Answer:
Missing condition after OR operator -> Option DQuick Check:
OR must be followed by a condition [OK]
- Leaving OR without a following condition
- Thinking quotes around strings cause error
- Assuming semicolon is mandatory
students with columns id, name, and score. You want to select students who scored between 70 and 90 inclusive. Which query correctly uses the WHERE clause to achieve this?Solution
Step 1: Understand the condition for scores between 70 and 90 inclusive
We want scores greater than or equal to 70 and less than or equal to 90.Step 2: Evaluate each option
SELECT * FROM students WHERE score > 70 AND score < 90; uses > and < which excludes the boundary values 70 and 90, incorrect. SELECT * FROM students WHERE score > 70 OR score < 90; uses OR which is incorrect because it includes scores outside the range. SELECT * FROM students WHERE score BETWEEN 70 AND 90; uses BETWEEN which is a concise way to express the inclusive range. SELECT * FROM students WHERE score = 70 AND score = 90; checks if score equals both 70 and 90 simultaneously, which is impossible.Step 3: Choose the best correct option
BETWEEN is the simplest and most readable way to filter inclusive ranges.Final Answer:
SELECT * FROM students WHERE score BETWEEN 70 AND 90; -> Option CQuick Check:
BETWEEN includes boundaries, perfect for range filtering [OK]
- Using OR instead of AND for range conditions
- Confusing BETWEEN boundaries as exclusive
- Using impossible conditions with AND equals
Ready to prove it?
Pass the test and this pattern moves unknown
