Where clause filtering in C Sharp (C#) - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use a WHERE clause to filter data, we want to know how long it takes as the data grows.
We ask: How does filtering time change when there are more rows to check?
Analyze the time complexity of the following code snippet.
// Simulate filtering rows with a WHERE clause
List<int> FilterData(List<int> data, int threshold) {
var result = new List<int>();
foreach (var item in data) {
if (item > threshold) {
result.Add(item);
}
}
return result;
}
This code checks each item in a list and keeps only those greater than a threshold.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each item in the list once.
- How many times: Exactly once for every item in the input list.
As the list gets bigger, the code checks more items one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The number of checks grows directly with the number of items.
Time Complexity: O(n)
This means the time to filter grows in a straight line as the data size grows.
[X] Wrong: "Filtering with a WHERE clause is instant no matter how big the data is."
[OK] Correct: The code must check each item to decide if it matches, so more data means more work.
Understanding how filtering scales helps you explain how queries behave with growing data, a useful skill in many jobs.
"What if the data was already sorted and we stopped checking after the first item that failed the condition? How would the time complexity change?"
Practice
What does the WHERE clause do in a SQL query?
Solution
Step 1: Understand the purpose of WHERE clause
The WHERE clause is used to select only rows that meet a specific condition.Step 2: Compare with other SQL clauses
Sorting is done by ORDER BY, joining by JOIN, grouping by GROUP BY, so WHERE is for filtering rows.Final Answer:
Filters rows based on a condition -> Option BQuick Check:
WHERE clause = filter rows [OK]
- Confusing WHERE with ORDER BY
- Thinking WHERE joins tables
- Mixing WHERE with GROUP BY
Which of the following is the correct syntax to filter rows where Age is greater than 30?
SELECT * FROM Users WHERE ___;
Solution
Step 1: Identify correct comparison operator
The operator for 'greater than' is >, so 'Age > 30' is correct.Step 2: Check other options for syntax errors
'Age = > 30' and 'Age >> 30' are invalid syntax. 'Age >= 30' means 'greater or equal', not strictly greater.Final Answer:
Age > 30 -> Option AQuick Check:
Use > for greater than [OK]
- Using = > instead of >
- Using >> which is invalid
- Confusing > with >= operator
Given the table Employees with columns Name and Salary, what rows will this query return?
SELECT Name FROM Employees WHERE Salary < 50000;
Solution
Step 1: Understand the WHERE condition
The condition Salary < 50000 means select rows where salary is less than 50000.Step 2: Interpret the query result
The query returns only the Name column for employees meeting that condition.Final Answer:
Employees with salary less than 50000 -> Option AQuick Check:
WHERE Salary < 50000 filters salaries below 50000 [OK]
- Confusing < with >
- Thinking it returns all employees
- Assuming it returns salary column too
Identify the error in this query that tries to select users with age 18 or older:
SELECT * FROM Users WHERE Age => 18;
Solution
Step 1: Check the comparison operator
The operator => is not valid SQL syntax; the correct operator for 'greater or equal' is >=.Step 2: Verify other parts of the query
Numeric values like 18 do not need quotes, WHERE supports numeric comparisons, and SELECT * works with WHERE.Final Answer:
The operator => is invalid; should be >= instead -> Option CQuick Check:
Use >= for greater or equal, not => [OK]
- Using => instead of >=
- Adding quotes around numbers
- Thinking WHERE can't compare numbers
You want to select all products from a Products table where the Price is between 10 and 20 inclusive. Which WHERE clause is correct?
Solution
Step 1: Understand inclusive range filtering
Inclusive means including 10 and 20, so use >= and <= operators.Step 2: Analyze each option
Price > 10 AND Price < 20 excludes 10 and 20 (strictly greater and less). Price >= 10 OR Price <= 20 uses OR, which selects too many rows. Price BETWEEN 10 AND 20 EXCLUSIVE is invalid syntax.Final Answer:
Price >= 10 AND Price <= 20 -> Option DQuick Check:
Inclusive range uses >= and <= with AND [OK]
- Using > and < excludes boundary values
- Using OR instead of AND
- Trying invalid BETWEEN syntax
