Negation in Queries

You can use negation as a comparison operator or a set operator.

When used as a comparison operator, Amazon SimpleDB returns all items where one of its attribute-value pair does not match the query expression. For example, ['Keyword' != 'Book'] returns items that have a value other than "Book" in the "Keyword" attribute.

This can result in unusual results if the "Keyword" attribute contains multiple values. Because Amazon SimpleDB queries each attribute-value pair individually, this query returns any items that have "Book" and any other value in the "Keyword" attribute.

[Note]Note

If an item does not have any value within the attribute you are querying, it is not considered and is not included in the result set.

When used as a set operator, Amazon SimpleDB evaluates the predicate expression, resolves it into a set of item names, and finds the complement of the predicate result set by retrieving all item names that do not fall into the predicate result set. For example, not['Keyword' = 'Book'] finds all items that have a "Keyword" attribute with a value equal to "Book" and returns all other items.

This section shows negation queries and their results. To view the source data for the queries, see Sample Query Data Set.

The following table shows some negation queries, how they are interpreted, and the results they return from the sample dataset.

Query ExpressionDescriptionResult
['Keyword' != 'Book']

Retrieves all items that have a "Keyword" that does not equal "Book"

Because Amazon SimpleDB applies the query to each attribute-value pair, an item that contains this attribute with any value other than book (even if it also has book) is returned.

Amazon SimpleDB only evaluates items where a value for the "Keyword" attribute is present which is why the item "B000SF3NGK" does not appear in the result set.

0385333498, 1579124585, B000T9886K, B00005JPLW
not['Keyword' = 'Book']

Retrieves all items that do not contain a "Keyword" that equals "Book"

Amazon SimpleDB finds all items that have a "Keyword" that equals "Book" and returns all other items.

Notice that item "B000SF3NGK" appears in this set.

B000T9886K, B00005JPLW, B000SF3NGK