Multiple Attribute Queries

The previous examples show how to create query expressions for single predicates. The Amazon SimpleDB query language also supports constructing expressions across multiple predicates using the union and intersection operators.

Multiple attribute queries work by producing a set of item names from each predicate and applying the union or intersection operator. The union operator combines the results from each predicate. The intersection operator only returns item names that appear in both result sets.

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

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

Query ExpressionDescriptionResult
['Keyword' = 'CD'] intersection ['Year' = '2007']

Retrieves all items that have a "CD" Keyword and were made in the year "2007."

The first predicate produces B000T9886K and the second produces B000T9886K and B00005JPLW. The intersection operator returns results that appear in both queries.

B000T9886K
[Keyword' = 'Frank Miller'] union ['Rating' starts-with '****']

Retrieves all items that have a "Frank Miller" Keyword or have a 4 star (****) rating or better.

The first predicate produces B00005JPLW and the second predicate produces 0385333498, 0802131786, 1579124585, and B000SF3NGK. The union operator returns results that appear in any of the queries.

0385333498, 0802131786, 1579124585, B00005JPLW, B000SF3NGK
['Year' >= '1900' and 'Year' < '2000'] intersection ['Keyword' = 'Book'] intersection ['Rating' starts-with '4' or 'Rating' = '****'] union ['Title' = '300'] union ['Author' = 'Paul Van Dyk']

Retrieves all "Book" items, published from 1900 to 1999, with a 4 star (****) rating, plus any item names with "Title" as "300", plus any item names with an "Author" named "Paul Van Dyk."

[Note]Note

Multiple predicate queries are evaluated from left to right. For more information, see Multi Predicate Query Processing Order.

0802131786, 1579124585, B00005JPLW, B000T9886K
not ['Rating' starts-with '*'] intersection ['Year' > '2000']

Retrieve all items that do not have a symbol-based "Rating" (starting with "*") and were published after the year "2000"

The first predicate is evaluated and negated which produces B000T9886K. The second produces B000T9886K, B00005JPLW, and B000SF3NGK. The intersection operator returns results that appear in both queries.

B000T9886K

Multiple predicate queries are always evaluated in left to right order with the exception of the not operator which always takes precedence. The following table explains the query evaluation steps for the following multiple predicate query.

 ['attribute1' = 'value1'] intersection not ['attribute2' = 'value2'] union ['attribute3' = 'value3']