ALL, ANY, SOME with subqueries in PostgreSQL - Time & Space Complexity
When using ALL, ANY, or SOME with subqueries, the database checks multiple values to decide if a condition is true.
We want to understand how the time needed grows as the number of values in the subquery grows.
Analyze the time complexity of the following SQL query using ANY.
SELECT employee_id, salary
FROM employees
WHERE salary > ANY (
SELECT salary
FROM employees
WHERE department_id = 10
);
This query finds employees whose salary is greater than at least one salary in department 10.
The query compares each employee's salary to multiple salaries from the subquery.
- Primary operation: Comparing one salary to each salary returned by the subquery.
- How many times: For each employee, it checks against all salaries in department 10.
As the number of employees (n) and the number of salaries in the subquery (m) grow, the comparisons increase.
| Input Size (employees n) | Subquery Size (m) | Approx. Operations |
|---|---|---|
| 10 | 5 | About 50 comparisons |
| 100 | 20 | About 2,000 comparisons |
| 1000 | 100 | About 100,000 comparisons |
Pattern observation: The total checks grow roughly by multiplying the number of outer rows by the number of subquery rows.
Time Complexity: O(n * m)
This means the time grows proportionally to the number of rows checked times the number of values in the subquery.
[X] Wrong: "The query only checks one value from the subquery, so it runs in constant time."
[OK] Correct: The query compares each outer row to all values returned by the subquery, so the number of comparisons grows with both sizes.
Understanding how ALL, ANY, and SOME work with subqueries helps you explain query performance clearly and shows you can think about how databases handle multiple comparisons.
"What if the subquery used DISTINCT to reduce duplicate salaries? How would that affect the time complexity?"
Practice
ALL keyword do in a PostgreSQL query with a subquery?Solution
Step 1: Understand the role of ALL
The ALL keyword compares a value to every value returned by a subquery.Step 2: Interpret the condition
It returns true only if the condition holds for all values from the subquery.Final Answer:
Checks if a condition is true for every value returned by the subquery. -> Option CQuick Check:
ALL means every value must satisfy condition [OK]
- Confusing ALL with ANY or SOME
- Thinking ALL returns all rows without condition
- Assuming ALL works like a JOIN
ANY with a subquery in PostgreSQL?Solution
Step 1: Recall correct ANY syntax
ANY is used as: column operator ANY (subquery), e.g., column = ANY (subquery).Step 2: Check each option
SELECT * FROM table WHERE column = ANY (SELECT col FROM othertable); matches correct syntax. Others have misplaced ANY or wrong order.Final Answer:
SELECT * FROM table WHERE column = ANY (SELECT col FROM othertable); -> Option AQuick Check:
ANY goes after operator and before subquery in parentheses [OK]
- Placing ANY before the column name
- Omitting parentheses around subquery
- Using ANY without an operator
products(id, price)and
discounts(min_price)What does this query return?
SELECT id FROM products WHERE price > ALL (SELECT min_price FROM discounts);
Solution
Step 1: Understand the ALL condition
The condition price > ALL (subquery) means price must be greater than every min_price returned.Step 2: Interpret the query result
Only products with price higher than all discount min_prices are selected.Final Answer:
Product ids with price greater than every min_price in discounts. -> Option BQuick Check:
price > ALL means greater than all values [OK]
- Confusing ALL with ANY or SOME
- Thinking it returns products with price greater than some min_price
- Assuming it returns all products
SELECT name FROM employees WHERE salary > ANY SELECT bonus FROM bonuses;
Solution
Step 1: Check ANY syntax
ANY must be followed by a subquery enclosed in parentheses.Step 2: Identify missing parentheses
The query lacks parentheses aroundSELECT bonus FROM bonusesafter ANY.Final Answer:
Missing parentheses around the subquery after ANY. -> Option DQuick Check:
ANY requires parentheses around subquery [OK]
- Omitting parentheses around subquery
- Misplacing ANY keyword
- Confusing ANY with ALL syntax
ANY to achieve this?Solution
Step 1: Understand the goal
We want customers with order total less than the maximum order total of any customer.Step 2: Analyze options
SELECT customer_id FROM orders WHERE total < ANY (SELECT total FROM orders); checks if total is less than any total in orders, which includes the maximum, so it works.Step 3: Why others are wrong
SELECT customer_id FROM orders WHERE total < ALL (SELECT total FROM orders); uses ALL, meaning less than every total, which is too strict. SELECT customer_id FROM orders WHERE total > ANY (SELECT total FROM orders); checks greater than any total, wrong direction. SELECT customer_id FROM orders WHERE total = ANY (SELECT MAX(total) FROM orders); compares to MAX(total) with ANY, which is redundant and incorrect.Final Answer:
SELECT customer_id FROM orders WHERE total < ANY (SELECT total FROM orders); -> Option AQuick Check:
Use ANY to compare with multiple values including max [OK]
- Using ALL instead of ANY for less than max
- Comparing with MAX inside ANY incorrectly
- Confusing > and < operators
Ready to prove it?
Pass the test and this pattern moves unknown
