177

I'm using Postgres' native array type, and trying to find the records where the ID is not in the array recipient IDs.

I can find where they are IN:

SELECT COUNT(*) FROM messages WHERE (3 = ANY (recipient_ids))

But this doesn't work:

SELECT COUNT(*) FROM messages WHERE (3 != ANY (recipient_ids))
SELECT COUNT(*) FROM messages WHERE (3  = NOT ANY (recipient_ids))

What's the right way to test for this condition?

4
  • 1
    does WHERE 3 NOT IN recipient_ids work? Commented Jul 30, 2012 at 22:45
  • 5
    Related note: as for text[] and int[] array: select not(array[1,2,3] @> array[3]);
    – Steve Peak
    Commented Mar 29, 2014 at 13:47
  • 5
    Pro tip: If you are checking if a null column is contained or not contained in an array, it will always say no. It took me like 20 minutes of debugging several containing methods to come to the conclusion that you cannot check if null is contained in an array
    – Andre Pena
    Commented Apr 20, 2019 at 14:58
  • @AndrePena Regular tip, for whoever stumbles upon these comments: you can check for null containment in arrays.
    – Zegarek
    Commented Nov 14, 2023 at 10:06

9 Answers 9

226
SELECT COUNT(*) FROM "messages" WHERE NOT (3 = ANY (recipient_ids))

You can always negate WHERE (condition) with WHERE NOT (condition)

1
63

You could turn it around a bit and say "3 is not equal to all the IDs":

where 3 != all (recipient_ids)

From the fine manual:

9.21.4. ALL (array)

expression operator ALL (array expression)

The right-hand side is a parenthesized expression, which must yield an array value. The left-hand expression is evaluated and compared to each element of the array using the given operator, which must yield a Boolean result. The result of ALL is "true" if all comparisons yield true (including the case where the array has zero elements). The result is "false" if any false result is found.

3
  • this doesn't really explain why any doesn't work in this case Commented Mar 5, 2019 at 1:36
  • This should be accepted as it explained the reason properly. PS you can also find any and all on postgres doc, which says: "x <> ANY (a,b,c) is equivalent to x <> a OR <> b OR x <> c". ref: postgresqltutorial.com/postgresql-any postgresqltutorial.com/postgresql-all
    – Tyler Temp
    Commented Jul 13, 2019 at 3:03
  • 5
    Adding this because of @seanlinsley's comment. any([array]) returns true even if only one evaluation is true. So if you have where 3 != any([1,3]) (desired output is false) you will get true because this expression evaluates as 3!=1 == true and 3!=3 == false. Now you have any(true,false); this returns true (which is not what you wanted). In comparison, 3 != all([1,3]) evaluates as 3!=1 == true and 3!=3 == false now you have all(true,false) which evaluates to false (which is what you wanted all along).
    – Sean
    Commented Mar 4, 2022 at 0:57
44

Beware of NULLs

Both ALL:

(some_value != ALL(some_array))

And ANY:

NOT (some_value = ANY(some_array))

Would work as long as some_array is not null. If the array might be null, then you must account for it with coalesce(), e.g.

(some_value != ALL(coalesce(some_array, array[]::int[])))

Or

NOT (some_value = ANY(coalesce(some_array, array[]::int[])))

From the docs:

If the array expression yields a null array, the result of ANY will be null

If the array expression yields a null array, the result of ALL will be null

44

Augmenting the ALL/ANY Answers

I prefer all solutions that use all or any to achieve the result, appreciating the additional notes (e.g. about NULLs). As another augementation, here is a way to think about those operators.

You can think about them as short-circuit operators:

  • all(array) goes through all the values in the array, comparing each to the reference value using the provided operator. As soon as a comparison yields false, the process ends with false, otherwise true. (Comparable to short-circuit logical and.)
  • any(array) goes through all the values in the array, comparing each to the reference value using the provided operator. As soon as a comparison yields true, the process ends with true, otherwise false. (Comparable to short-circuit logical or.)

This is why 3 <> any('{1,2,3}') does not yield the desired result: The process compares 3 with 1 for inequality, which is true, and immediately returns true. A single value in the array different from 3 is enough to make the entire condition true. The 3 in the last array position is prob. never used.

3 <> all('{1,2,3}') on the other hand makes sure all values are not equal 3. It will run through all comparisons that yield true up to an element that yields false (the last in this case), to return false as the overall result. This is what the OP wants.

19

an update:

as of postgres 9.3,

you can use NOT in tandem with the @> (contains operator) to achieve this as well.

IE.

SELECT COUNT(*) FROM "messages" WHERE NOT recipient_ids @> ARRAY[3];

14

not (3 = any(recipient_ids))?

1
  • Thanks, I was using 3 <> ANY(ARRAY[1,2,3,4]). It should have worked in that way :\
    – yeyo
    Commented Mar 12, 2016 at 21:36
3

Note that the ANY/ALL operators will not work with array indexes. If indexes are in mind:

SELECT COUNT(*) FROM "messages" WHERE 3 && recipient_ids

and the negative:

SELECT COUNT(*) FROM "messages" WHERE NOT (3 && recipient_ids)

An index can then be created like:

CREATE INDEX recipient_ids_idx on tableName USING GIN(recipient_ids)
2
  • Unlike other answers, this answer actually makes use of the PostgreSQL array overlap operator. && Commented Jun 14, 2016 at 8:36
  • 7
    This won't work as written. Array operators like && and @> require both elements to be arrays, which 3 isn't. In order for this to work, the query would need to be written as: SELECT COUNT(*) FROM "messages" WHERE ARRAY[3] && recipient_ids.
    – Dologan
    Commented Jun 14, 2016 at 13:12
1

Use the following query select id from Example where NOT (id = ANY ('{1, 2}'))

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Nov 5, 2022 at 12:54
0

Since 9.5 array_position() is not null can also safely check for null containment.

SELECT COUNT(*) FROM messages WHERE (array_position(recipient_ids, your_val) IS null)

array_position ( anycompatiblearray, anycompatible [, integer ] ) → integer

Returns the subscript of the first occurrence of the second argument in the array, or NULL if it's not present. If the third argument is given, the search begins at that subscript. The array must be one-dimensional. Comparisons are done using IS NOT DISTINCT FROM semantics, so it is possible to search for NULL.

There's no operator in GIN index array_ops that uses this function, so the price for using it in a query is that it won't benefit from the index. Demo at db<>fiddle:

select array_position(array[null],null) is not null;
?column?
TRUE

<@,&& and @> array operators as well as IN(), SOME() and ANY() use regular = that yields null if there's null on either side of the operator, which is why none of the examples below works, even though all might seem like they could/should:

select null=any(array[null,''])               as "any",
       null=some(array[null,''])              as "some",
       null=all(array[null,null])             as "all",
       null in (null,'')                      as "in",
       null in (select unnest(array[null,'']))as "in(2)",
       array[null] <@ array[null,'']          as "<@",
       array[null] && array[null,'']          as "&&";
any some all in in(2) <@ &&
null null null null null FALSE FALSE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.