Challenge - 5 Problems
ANY and ALL Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Using ANY with array comparison
Given the table products with a column
price and an array {100, 200, 300}, what does the following query return?SELECT name FROM products WHERE price = ANY (ARRAY[100, 200, 300]);
Attempts:
2 left
💡 Hint
ANY checks if the price matches any element in the array.
✗ Incorrect
The = ANY operator compares the price to each element in the array and returns true if any match is found. So it selects products priced exactly 100, 200, or 300.
❓ query_result
intermediate2:00remaining
Using ALL with array comparison
What does this query return?
SELECT name FROM products WHERE price > ALL (ARRAY[50, 75, 90]);
Attempts:
2 left
💡 Hint
ALL means the condition must be true for every element in the array.
✗ Incorrect
price > ALL means price must be greater than every element in the array, so price must be greater than 90 (the largest).
📝 Syntax
advanced2:00remaining
Identify the syntax error with ANY and arrays
Which option contains a syntax error when using ANY with arrays in PostgreSQL?
Attempts:
2 left
💡 Hint
PostgreSQL requires ARRAY keyword and parentheses for arrays.
✗ Incorrect
Option A uses square brackets without ARRAY keyword and parentheses, which is invalid syntax in PostgreSQL.
🧠 Conceptual
advanced2:00remaining
Understanding ALL with empty arrays
What is the result of this query if the array is empty?
SELECT * FROM products WHERE price > ALL (ARRAY[]::integer[]);
Attempts:
2 left
💡 Hint
Think about how ALL behaves with no elements to compare.
✗ Incorrect
ALL with an empty array always returns true because there is no element to contradict the condition, so all rows match.
❓ optimization
expert3:00remaining
Optimizing queries with ANY and large arrays
You have a large array of IDs and want to find matching rows in
users table by id. Which query is most efficient?A) SELECT * FROM users WHERE id = ANY (ARRAY[...large array...]); B) SELECT * FROM users WHERE id IN (SELECT unnest(ARRAY[...large array...])); C) SELECT * FROM users WHERE id = ANY (ARRAY[...large array...]) AND active = true; D) SELECT * FROM users WHERE id IN (...large list of IDs...);
Attempts:
2 left
💡 Hint
Adding filters can help the planner optimize the query better.
✗ Incorrect
Option D allows the planner to use indexes on active and id together, improving performance over just matching ANY or IN with large arrays.