Why filtering is essential in SQL - Performance Analysis
When we run database queries, filtering helps us get only the data we need.
We want to know how filtering affects the time it takes to get results.
Analyze the time complexity of the following SQL query with filtering.
SELECT *
FROM employees
WHERE department = 'Sales';
This query selects all employees who work in the Sales department.
Look at what repeats when the database runs this query.
- Primary operation: Checking each employee's department value.
- How many times: Once for every employee in the table.
As the number of employees grows, the database checks more rows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 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: "Filtering makes the query run instantly no matter the size."
[OK] Correct: The database still checks each row to see if it matches the filter, so bigger tables take more time.
Understanding how filtering affects query time shows you know how databases handle data efficiently.
"What if we add an index on the department column? How would the time complexity change?"
Practice
WHERE clause important in SQL?Solution
Step 1: Understand the purpose of filtering
Filtering is used to select only the rows that match certain criteria from a table.Step 2: Identify what
TheWHEREdoesWHEREclause sets conditions to limit the rows returned by a query.Final Answer:
It helps retrieve only the rows that meet specific conditions. -> Option DQuick Check:
Filtering = Select specific rows [OK]
- Thinking WHERE changes table structure
- Confusing filtering with sorting
- Believing WHERE creates new tables
age is greater than 30?Solution
Step 1: Recall correct filtering syntax
TheWHEREclause is used with a condition likeage > 30to filter rows.Step 2: Check each option's syntax
SELECT * FROM table WHERE age > 30; uses correct syntax. SELECT * FROM table FILTER age > 30; uses FILTER which is invalid in SQL. SELECT * FROM table HAVING age > 30; uses HAVING which is for groups, not rows. SELECT * FROM table WHERE age => 30; uses => which is invalid operator.Final Answer:
SELECT * FROM table WHERE age > 30; -> Option CQuick Check:
WHERE + condition = correct filter [OK]
- Using FILTER instead of WHERE
- Using HAVING without GROUP BY
- Using wrong comparison operators like =>
Employees with columns id, name, and salary, what will this query return?SELECT name FROM Employees WHERE salary >= 50000;
Solution
Step 1: Understand the WHERE condition
The conditionsalary >= 50000selects employees with salary 50000 or more.Step 2: Analyze the SELECT clause
The query selects only thenamecolumn for those filtered employees.Final Answer:
All employee names with salary greater than or equal to 50000 -> Option BQuick Check:
salary >= 50000 filters employees [OK]
- Thinking >= means only equal
- Confusing less than with greater than
- Selecting all columns instead of just names
SELECT * FROM Products WHERE price => 100;
Solution
Step 1: Check the comparison operator
The operator=>is not valid in SQL; the correct operator is>=.Step 2: Verify other parts of the query
The WHERE clause is present, SELECT * is valid, and semicolon is allowed.Final Answer:
The comparison operator => is invalid in SQL. -> Option AQuick Check:
Use >=, not => for comparisons [OK]
- Using => instead of >=
- Removing WHERE clause accidentally
- Thinking semicolon is an error
Orders with columns order_id, customer_id, and status. You want to find all orders that are either 'shipped' or 'delivered'. Which query correctly filters these orders?Solution
Step 1: Understand filtering for multiple values
To filter rows where status is either 'shipped' or 'delivered', use OR or IN.Step 2: Analyze each option
SELECT * FROM Orders WHERE status = 'shipped' OR status = 'delivered'; uses OR correctly. SELECT * FROM Orders WHERE status = 'shipped' AND 'delivered'; uses AND incorrectly. SELECT * FROM Orders WHERE status IN (shipped, delivered); omits quotes around strings, invalid syntax. SELECT * FROM Orders WHERE status = 'shipped', 'delivered'; has invalid syntax.Final Answer:
SELECT * FROM Orders WHERE status = 'shipped' OR status = 'delivered'; -> Option AQuick Check:
Use OR to combine conditions for filtering [OK]
- Using AND instead of OR for multiple options
- Incorrect syntax with commas in WHERE
- Confusing IN syntax
Ready to prove it?
Pass the test and this pattern moves unknown
