0
0
PostgreSQLquery~5 mins

ANY and ALL with arrays in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the ANY keyword do when used with arrays in PostgreSQL?

ANY checks if a value matches any element in an array. It returns true if the value satisfies the condition for at least one element.

Click to reveal answer
beginner
How does ALL differ from ANY when used with arrays?

ALL checks if a value matches all elements in an array. It returns true only if the value satisfies the condition for every element.

Click to reveal answer
intermediate
Write a simple example query using ANY with an array.
SELECT * FROM products WHERE price > ANY (ARRAY[10, 20, 30]);

This finds products with price greater than at least one of 10, 20, or 30.

Click to reveal answer
beginner
What will this query return?<br>
SELECT 5 = ALL (ARRAY[5, 5, 5]);

It returns true because 5 equals all elements in the array.

Click to reveal answer
intermediate
Can ANY and ALL be used with operators other than =? Give an example.

Yes. For example:<br>

SELECT * FROM orders WHERE quantity < ALL (ARRAY[10, 20, 30]);

This finds orders where quantity is less than every number in the array.

Click to reveal answer
What does price > ANY (ARRAY[100, 200, 300]) mean?
APrice equals any of 100, 200, or 300
BPrice is greater than all of 100, 200, and 300
CPrice equals all of 100, 200, and 300
DPrice is greater than at least one of 100, 200, or 300
What will SELECT 10 = ALL (ARRAY[10, 10, 10]); return?
Afalse
Bnull
Ctrue
Derror
Which keyword checks if a value matches every element in an array?
AALL
BANY
CSOME
DIN
Is this query valid?<br>SELECT * FROM table WHERE col < ALL (ARRAY[5, 10, 15]);
AYes, it checks if col is less than all array elements
BNo, <code>ALL</code> cannot be used with <
CNo, arrays cannot be used with <code>ALL</code>
DYes, but it checks if col is less than any element
What does value = ANY (array) return if the array is empty?
Aerror
Bfalse
Cnull
Dtrue
Explain how ANY and ALL work with arrays in PostgreSQL and give a simple example for each.
Think about comparing a value to multiple values stored in an array.
You got /4 concepts.
    Describe a real-life scenario where using ANY or ALL with arrays in a query would be helpful.
    Imagine checking if a price is higher than some or all given thresholds.
    You got /3 concepts.