0
0
PostgreSQLquery~20 mins

ANY and ALL with arrays in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ANY and ALL Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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]);
AAll products with price less than 100, 200, or 300
BAll products with price exactly 100, 200, or 300
CAll products with price greater than 100, 200, or 300
DNo products, because the syntax is invalid
Attempts:
2 left
💡 Hint
ANY checks if the price matches any element in the array.
query_result
intermediate
2:00remaining
Using ALL with array comparison
What does this query return?
SELECT name FROM products WHERE price > ALL (ARRAY[50, 75, 90]);
AProducts with price less than 50, 75, and 90
BProducts with price greater than 50 or 75 or 90
CProducts with price greater than 50, 75, and 90
DSyntax error due to ALL usage
Attempts:
2 left
💡 Hint
ALL means the condition must be true for every element in the array.
📝 Syntax
advanced
2:00remaining
Identify the syntax error with ANY and arrays
Which option contains a syntax error when using ANY with arrays in PostgreSQL?
ASELECT * FROM orders WHERE quantity = ANY [10, 20, 30];
BSELECT * FROM orders WHERE quantity = ANY (ARRAY[10, 20, 30]);
C;)]03 ,02 ,01[YARRA( YNA = ytitnauq EREHW sredro MORF * TCELES
DSELECT * FROM orders WHERE quantity = ANY (ARRAY[10,20,30]);
Attempts:
2 left
💡 Hint
PostgreSQL requires ARRAY keyword and parentheses for arrays.
🧠 Conceptual
advanced
2: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[]);
AReturns all rows because condition is true for all elements (none exist)
BReturns no rows because comparison fails
CRaises an error due to empty array
DReturns rows where price is NULL
Attempts:
2 left
💡 Hint
Think about how ALL behaves with no elements to compare.
optimization
expert
3: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...);
AUsing = ANY with a large array directly
BUsing IN with unnest of the array
CUsing IN with a large list of IDs directly
DUsing = ANY with array and extra filter active = true
Attempts:
2 left
💡 Hint
Adding filters can help the planner optimize the query better.