ANY and ALL help you check if a value matches some or all items in a list. This makes it easy to compare one value against many at once.
ANY and ALL with arrays in PostgreSQL
SELECT column_name FROM table_name WHERE column_name operator ANY (array_expression); SELECT column_name FROM table_name WHERE column_name operator ALL (array_expression);
operator can be =, <, >, <=, >=, <> etc.
ANY means the condition is true if it matches at least one element in the array.
ALL means the condition must be true for every element in the array.
SELECT * FROM products WHERE price = ANY (ARRAY[10, 20, 30]);
SELECT * FROM products WHERE price > ALL (ARRAY[5, 7, 9]);
SELECT * FROM users WHERE username = ANY (ARRAY[]::text[]);
SELECT * FROM orders WHERE quantity <= ALL (ARRAY[100]);
This creates a products table and adds four fruits with prices. Then it finds products priced at 10 or 30 using ANY. Next, it finds products priced greater than both 15 and 25 using ALL.
CREATE TABLE products (id SERIAL PRIMARY KEY, name TEXT, price INT); INSERT INTO products (name, price) VALUES ('Apple', 10), ('Banana', 20), ('Cherry', 30), ('Date', 40); -- Find products with price equal to any in the list SELECT name, price FROM products WHERE price = ANY (ARRAY[10, 30]); -- Find products with price greater than all in the list SELECT name, price FROM products WHERE price > ALL (ARRAY[15, 25]);
ANY returns true if the condition matches at least one element in the array.
ALL returns true only if the condition matches every element in the array.
Using an empty array with ANY always returns false; with ALL it returns true.
Time complexity depends on array size because it checks each element.
Use ANY when you want to match any value; use ALL when you want to ensure all values meet the condition.
ANY checks if a value matches any element in an array.
ALL checks if a value matches all elements in an array.
These help compare one value against many easily in queries.
Practice
ANY keyword do when used with an array in PostgreSQL?Solution
Step 1: Understand the ANY keyword
ANY compares a single value to each element in an array and returns true if it matches any one of them.Step 2: Compare with ALL keyword
ALL requires the value to match every element, which is different from ANY.Final Answer:
It checks if a value matches at least one element in the array. -> Option AQuick Check:
ANY = matches any element [OK]
- Confusing ANY with ALL
- Thinking ANY returns all elements
- Assuming ANY returns first element only
ARRAY[3,5,7] in PostgreSQL?Solution
Step 1: Check correct ANY syntax
The correct syntax uses= ANY(ARRAY[...])with parentheses around ARRAY.Step 2: Identify wrong options
SELECT 5 = ANY ARRAY[3,5,7]; misses parentheses around ARRAY, causing syntax error. SELECT 5 = ANY[3,5,7]; uses square brackets incorrectly. SELECT 5 = ALL ARRAY(3,5,7); uses ALL instead of ANY and wrong ARRAY syntax without [].Final Answer:
SELECT 5 = ANY(ARRAY[3,5,7]); -> Option CQuick Check:
= ANY(ARRAY[...]) [OK]
- Omitting parentheses around ARRAY
- Using square brackets instead of ARRAY keyword
- Mixing ANY and ALL keywords
SELECT 4 > ALL ARRAY[2,3,5];What is the result?
Solution
Step 1: Understand ALL with comparison
The query uses4 > ALL ARRAY[2,3,5]but misses required parentheses around the array.Step 2: Correct syntax and error
Correct is4 > ALL (ARRAY[2,3,5]). Without (), it causes syntax error. (Note: if fixed, 4>5 is false so result false, but as given: error.)Final Answer:
syntax error -> Option DQuick Check:
Missing () around ARRAY -> syntax error [OK]
- Assuming query runs and gets false
- Forgetting parentheses around ARRAY
- Confusing with ANY behavior
SELECT * FROM users WHERE age = ALL [20, 30, 40];Solution
Step 1: Check array syntax in WHERE clause
PostgreSQL requires arrays to be written with ARRAY keyword, not square brackets alone.Step 2: Identify correct syntax
The correct syntax isage = ALL (ARRAY[20, 30, 40]). Using just [20, 30, 40] causes syntax error.Final Answer:
Using square brackets instead of ARRAY keyword -> Option BQuick Check:
Arrays need ARRAY keyword, not just brackets [OK]
- Using [] instead of ARRAY[]
- Thinking ALL can't compare numbers
- Missing operator after WHERE
ARRAY[100, 200, 300]. Which query correctly achieves this?Solution
Step 1: Understand the requirement
We want products with price less than every price in the array, meaning price must be less than 100 (the minimum).Step 2: Choose correct operator and keyword
Usingprice < ALL (ARRAY[100, 200, 300])checks if price is less than all elements. ANY would only require less than one.Final Answer:
SELECT * FROM products WHERE price < ALL (ARRAY[100, 200, 300]); -> Option AQuick Check:
Less than ALL means less than every element [OK]
- Using ANY instead of ALL for all elements
- Using > instead of < for less than
- Using = ALL which checks equality
Ready to prove it?
Pass the test and this pattern moves unknown
