= ANY / <> ALL
PG 7.4+→ booleanCompares a value against all elements of an array or subquery result. ANY returns true if the comparison is true for at least one element.
Signatures
value = ANY ( array ) → booleanvalue = ANY ( subquery ) → booleanvalue <> ALL ( array ) → booleanParameters
| Parameter | Type | Description |
|---|---|---|
| value | any | Value to compare |
| array | anyarray or subquery | Set of values to compare against |
Examples
SELECT 5 = ANY(ARRAY[1,3,5,7]);trueSELECT * FROM posts WHERE 'admin' = ANY(allowed_roles);Posts accessible to adminSELECT * FROM users WHERE id = ANY(ARRAY[1,5,42]::bigint[]);Specific users by ID arraySELECT * FROM tasks WHERE priority <> ALL(ARRAY['low','trivial']);Tasks that are not low or trivial priority`NULL = ANY(...)` always evaluates to NULL (not true), so rows where the comparison value is NULL will never match. Also, passing an untyped array literal can cause the planner to pick a suboptimal type, bypassing indexes on bigint columns.
✓ Instead: Always cast array parameters explicitly: `id = ANY($1::bigint[])`. Guard nullable comparison values with `IS NOT NULL` checks or use `IS NOT DISTINCT FROM` for NULL-safe matching.
Instead of building `IN (1,2,3,...)` SQL strings dynamically, pass an array parameter and use `id = ANY($1::bigint[])`. This is parameterizable, preventing SQL injection.
SELECT * FROM products WHERE id = ANY($1::integer[]);Products matching a dynamically passed ID arrayCommon Gotchas
NULL kills comparisons silently — nothing equals NULL, not even NULL
NULL = NULL is not TRUE. It is NULL. Queries filtering for NULL with = will silently return zero rows.
Arrays are 1-indexed in PostgreSQL, not 0-indexed
PostgreSQL arrays start at index 1 by default. array[0] returns NULL, not the first element — silently wrong.