Sub topic 7
Filtering rows: `@>`, `<@`, `&&`, `ANY` and `ALL`
Assume you know SELECT, WHERE, NULL, and three-valued logic. The examples use a table named posts with a tags text[] column. PostgreSQL arrays use 1-based positions, but array search operators compare values rather than positions.
Containment and overlap operators
PostgreSQL provides three useful operators for comparing arrays:
| Operator | Meaning | Example |
|---|---|---|
| `@>` | Left array contains every value in the right array | `tags @> ARRAY['postgres']` |
| `<@` | Left array is contained in the right array | `tags <@ ARRAY['sql', 'postgres', 'gin']` |
| `&&` | The arrays share at least one value | `tags && ARRAY['gin', 'sql']` |
The right side must have a compatible array type. A typed constructor such as ARRAY['postgres'] usually gets its type from the comparison. You can add an explicit cast when PostgreSQL cannot infer it.
SELECT title
FROM posts
WHERE tags @> ARRAY['postgres'];
SELECT title
FROM posts
WHERE tags <@ ARRAY['sql', 'postgres', 'gin', 'indexing'];
SELECT title
FROM posts
WHERE tags && ARRAY['gin', 'sql'];@> is useful when a row must have all requested tags. It does not require the arrays to have the same length or order. A row with {'postgres', 'sql', 'tutorial'} contains {'sql', 'postgres'}.
<@ reverses the direction. It finds rows whose complete tag set is inside an allowed set. This can help enforce or inspect a category of permitted values.
&& is an overlap test. It returns true when at least one value appears in both arrays.
Duplicates and order
Containment is set-like. Array order does not affect these operators, and duplicate values do not make a containment test require duplicate matches.
SELECT ARRAY['a'] @> ARRAY['a', 'a'] AS contains_duplicates,
ARRAY['a', 'b'] && ARRAY['b', 'b'] AS overlaps_duplicates;Both results are true. If duplicate count and order matter, compare arrays with = or process their elements with unnest.
Comparing one value with `ANY` and `ALL`
ANY compares one scalar value with each element of an array. The expression is true if at least one comparison is true.
SELECT title
FROM posts
WHERE 'gin' = ANY (tags);This is equivalent to asking whether 'gin' is a member of tags. It is often convenient when the left side is a column or expression.
ALL requires the comparison to be true for every array element.
SELECT title
FROM posts
WHERE 'postgres' <> ALL (tags);This returns posts where no tag equals postgres. The equivalent containment form is often clearer:
SELECT title
FROM posts
WHERE NOT (tags @> ARRAY['postgres']);The comparison operator can be different from =:
SELECT title
FROM posts
WHERE 5 < ALL (ARRAY[10, 20, 30]);
SELECT title
FROM posts
WHERE 5 = ANY (ARRAY[3, 5, 9]);ANY and ALL are different from IN in syntax but related in purpose. For a list of scalar constants, IN is usually simpler. For a value stored in an array column, ANY expresses the intent directly.
NULLs and empty arrays
Array predicates follow SQL's three-valued logic. A result can be true, false, or unknown. A NULL element is not equal to any ordinary value, so it can prevent PostgreSQL from proving that an ANY or ALL expression is true or false.
SELECT
1 = ANY (ARRAY[2, NULL]) AS any_result,
1 <> ALL (ARRAY[2, NULL]) AS all_result;Both expressions produce NULL. The first comparison is false for 2 and unknown for NULL, so ANY cannot find a true comparison. The second comparison is true for 2 but unknown for NULL, so ALL cannot confirm that every comparison is true.
An empty array behaves differently. ANY over an empty array is false because no element matches. ALL over an empty array is true because there is no element that violates the condition. This is called vacuous truth.
SELECT
1 = ANY ('{}'::int[]) AS any_empty,
1 <> ALL ('{}'::int[]) AS all_empty;In a WHERE clause, both NULL and false exclude a row. If you need a guaranteed Boolean result, make the rule explicit with COALESCE, filtering, or a condition that rejects arrays containing NULL.
Choosing a predicate
Use @> when the row must contain a requested set of values. Use <@ when every row value must belong to an allowed set. Use && when one shared value is enough. Use ANY for a scalar-to-array comparison and ALL when every element must satisfy a comparison.
Exercise
Which predicate finds posts whose tags include both sql and postgres, regardless of order or extra tags?
Show answer
WHERE tags @> ARRAY['sql', 'postgres']