Standard comparison operators in PostgreSQL - Time & Space Complexity
When using standard comparison operators in SQL, it is important to understand how the time to run queries changes as the data grows.
We want to know how the number of comparisons affects the total work done.
Analyze the time complexity of the following SQL query using comparison operators.
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: The database compares the salary of each employee to 50,000.
- How many times: Once for each employee row in the table.
As the number of employees grows, the number of comparisons grows at the same rate.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 comparisons |
| 100 | 100 comparisons |
| 1000 | 1000 comparisons |
Pattern observation: The work grows directly in proportion to the number of rows.
Time Complexity: O(n)
This means the time to run the query grows linearly with the number of rows to check.
[X] Wrong: "The comparison only happens once regardless of table size."
[OK] Correct: Each row must be checked individually, so the number of comparisons grows with the number of rows.
Understanding how comparison operations scale helps you explain query performance clearly and shows you know how databases handle filtering.
"What if the query used an index on salary? How would the time complexity change?"
Practice
Solution
Step 1: Understand the meaning of operators
The operator '=' means equal, so it cannot mean not equal.Step 2: Identify the not equal operator
In PostgreSQL, both '<>' and '!=' are used to mean not equal.Final Answer:
<> or != -> Option CQuick Check:
Not equal operator = <> or != [OK]
- Confusing '=' with not equal
- Using '<' or '>' as not equal
- Thinking '!=' is invalid in PostgreSQL
age is greater than or equal to 18 in PostgreSQL?Solution
Step 1: Recall the correct operator for greater than or equal
The correct operator is '>=' which means greater than or equal.Step 2: Check each option's syntax
age >= 18 uses '>=', which is correct. Options B and C use invalid operators '=>' and '=<' which are not valid in PostgreSQL. age >> 18 uses '>>' which is a bitwise operator, not comparison.Final Answer:
age >= 18 -> Option BQuick Check:
Greater or equal operator = >= [OK]
- Swapping operator symbols like => instead of >=
- Using bitwise operators instead of comparison
- Confusing <= and >= syntax
products with a column price, what will the following query return?SELECT * FROM products WHERE price < 50;
Solution
Step 1: Understand the comparison operator used
The operator '<' means less than, so the query filters rows where price is less than 50.Step 2: Interpret the query result
The query returns all rows from products where price is strictly less than 50.Final Answer:
All products with price less than 50 -> Option AQuick Check:
Operator '<' means less than [OK]
- Thinking '<' means less than or equal
- Confusing '<' with '>'
- Assuming syntax error due to operator
SELECT * FROM users WHERE age <> 30 AND age != 25;
Solution
Step 1: Check the use of comparison operators
Both '<>' and '!=' are valid not equal operators in PostgreSQL and can be used interchangeably.Step 2: Analyze the logical operator usage
The AND operator correctly combines two conditions requiring age to be not 30 and not 25 simultaneously.Final Answer:
No error; query is valid and works correctly -> Option DQuick Check:
Both <> and != are valid not equal operators [OK]
- Assuming <> and != cannot be mixed
- Thinking AND should be OR without reason
- Confusing not equal with equal operator
employees whose salary is between 3000 and 7000 inclusive. Which of the following WHERE clauses correctly uses standard comparison operators to achieve this?Solution
Step 1: Understand the requirement for inclusive range
Inclusive means salary can be equal to 3000 or 7000 or any value in between.Step 2: Analyze each option's logic
salary >= 3000 AND salary <= 7000 uses '>=' and '<=' which include the boundary values. salary > 3000 AND salary < 7000 excludes boundaries with '>' and '<'. salary = 3000 OR salary = 7000 only selects salaries exactly 3000 or 7000, missing values in between. salary <> 3000 AND salary <> 7000 excludes salaries equal to 3000 and 7000, opposite of requirement.Final Answer:
salary >= 3000 AND salary <= 7000 -> Option AQuick Check:
Use >= and <= for inclusive range [OK]
- Using > and < excludes boundary values
- Using OR instead of AND for range
- Using not equal operators instead of range
Ready to prove it?
Pass the test and this pattern moves unknown
