Why filtering behavior matters in PostgreSQL - Performance Analysis
When we run database queries, filtering helps us pick only the rows we want.
We want to know how filtering affects the time it takes to get results.
Analyze the time complexity of the following code snippet.
SELECT *
FROM orders
WHERE order_date >= '2023-01-01'
AND status = 'shipped';
This query selects all orders shipped since January 1, 2023, filtering rows by date and status.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning each row in the orders table to check the filter conditions.
- How many times: Once for every row in the table (n times).
As the number of orders grows, the database checks more rows to find matches.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 row checks |
| 100 | About 100 row checks |
| 1000 | About 1000 row 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 with the number of rows to check.
[X] Wrong: "Filtering always makes queries run faster regardless of data size."
[OK] Correct: Filtering still needs to look at each row to decide if it matches, so with more data, it takes more time.
Understanding how filtering affects query time helps you write better queries and explain your choices clearly.
"What if we added an index on order_date and status? How would the time complexity change?"
Practice
WHERE clause in a PostgreSQL query?Solution
Step 1: Understand the role of WHERE clause
The WHERE clause is used to specify conditions that rows must meet to be included in the result.Step 2: Compare options with WHERE purpose
Sorting is done by ORDER BY, joining by JOIN, and creating tables by CREATE TABLE, so these are incorrect.Final Answer:
To filter rows based on a condition -> Option DQuick Check:
WHERE filters rows = A [OK]
- Confusing WHERE with ORDER BY
- Thinking WHERE creates tables
- Mixing WHERE with JOIN
age is greater than 30?Solution
Step 1: Identify correct filtering syntax
The WHERE clause is used to filter rows based on conditions like age > 30.Step 2: Eliminate incorrect clauses
FILTER is not valid here, HAVING is for groups, ORDER BY sorts results, so only WHERE is correct.Final Answer:
SELECT * FROM users WHERE age > 30; -> Option BQuick Check:
WHERE filters rows correctly = A [OK]
- Using HAVING without GROUP BY
- Using FILTER instead of WHERE
- Confusing ORDER BY with filtering
employees with columns id, name, and salary, what will this query return?SELECT name FROM employees WHERE salary >= 50000;
Solution
Step 1: Analyze the WHERE condition
The condition salary >= 50000 filters employees with salary 50000 or more.Step 2: Understand SELECT output
Only the name column is selected for those filtered employees.Final Answer:
All employee names with salary equal or greater than 50000 -> Option AQuick Check:
WHERE salary >= 50000 filters names = D [OK]
- Thinking it returns all employees
- Confusing >= with <
- Expecting GROUP BY needed here
SELECT * FROM orders WHERE status = 'shipped' AND;
Solution
Step 1: Check WHERE clause syntax
The WHERE clause ends with AND but no condition follows, causing syntax error.Step 2: Validate other parts
HAVING is not needed here, ORDER BY is optional, and * is valid to select all columns.Final Answer:
Missing condition after AND -> Option CQuick Check:
AND must be followed by condition = B [OK]
- Leaving AND without condition
- Confusing WHERE and HAVING
- Thinking ORDER BY is mandatory
SELECT customer_id FROM orders WHERE total > 100 AND account_status = 'active';
Solution
Step 1: Understand filtering conditions
Both conditions total > 100 and account_status = 'active' filter rows correctly in WHERE clause.Step 2: Check if GROUP BY or HAVING needed
Since no aggregation is used, GROUP BY or HAVING is not required here.Final Answer:
This query correctly filters customers with total > 100 and active accounts -> Option AQuick Check:
WHERE filters multiple conditions correctly = C [OK]
- Using HAVING without aggregation
- Adding unnecessary GROUP BY
- Thinking account_status can't be filtered in WHERE
Ready to prove it?
Pass the test and this pattern moves unknown
