37

I have started using sql and have heard much about the ANY and ALL operators. Can somebody explain to me the kind of queries they are used in and how they work?

6
  • See this msdn post msdn.microsoft.com/en-us/library/ms187074.aspx Commented May 12, 2011 at 15:31
  • 1
    @Bala - Less relevant for this post since it's tagged mysql Commented May 12, 2011 at 15:38
  • 4
    Voted to reopen. How is this not a question? It's technical, straightforward, not necessarily common knowledge, and has an objective answer. Commented May 14, 2011 at 17:22
  • 8
    Why close this? It is a good question with a good answer and it helped me. Commented Dec 1, 2013 at 11:40
  • 4
    Not really an ambiguous question. Sometimes an explanation given by the community is more helpful than a link to mdsn. Commented Apr 27, 2015 at 23:38

5 Answers 5

45

The ANY and ALL operators allow you to perform a comparison between a single column value and a range of other values. For instance:

select * from Table1 t1 where t1.Col1 < ANY(select value from Table2)

ANY means that the condition will be satisfied if the operation is true for any of the values in the range. ALL means that the condition will be satisfied only if the operation is true for all values in the range.

To use an example that might hit closer to home, doing this:

select * from Table1 t1 where t1.Col1 = ANY(select value from Table2)

Is the same as doing this:

select * from Table1 t1 where t1.Col1 in (select value from Table2)
Sign up to request clarification or add additional context in comments.

1 Comment

mentioning that SOME and ANY are synonyms.
9

I have heard much about the ANY and ALL operators

I'm mildly surprised: I rarely see them used myself. Far more commonly seen are WHERE val IN (subquery) and WHERE EXISTS (subquery).

To borrow @Adam Robinson's example:

SELECT * 
  FROM Table1 AS t1 
 WHERE t1.Col1 < ANY (
                      SELECT value 
                        FROM Table2
                     );

I more usually see this written like this:

SELECT * 
  FROM Table1 AS t1 
 WHERE EXISTS (
               SELECT *
                 FROM Table2 AS t2
                WHERE t1.Col1 < t2.value
              );

I find this construct easier to read because the parameters of the predicate (t1.Col1 and t2.value respectively) are closer together.

2 Comments

in is simply alias for = any for subqueries.
I would usually see it written as SELECT * FROM Table1 AS t1 WHERE t1.Col1 < (SELECT MAX(value) FROM Table2); - which I think is clearer and likely to be optimised better
5

Answers above addressed some aspects of "ANY" and did not address "ALL".

Both of these are more useful when comparing against another table and its entries are changing dynamically.

Especially true for < ANY and > ANY, since for static arguments, you could just take MAX/MIN respectively, and drop the "ANY".

For example, this query -

SELECT ProductName, ProductID FROM Products WHERE ProductID > ANY (100, 200, 300);

can be simplified to -

SELECT ProductName, ProductID FROM Products WHERE ProductID > 100;

Note that the "ALL" query will end up comparing one column value with ALL (...) which will always be false unless "ALL" arguments are identical.

For ex -

SELECT ProductName, ProductID FROM Products WHERE ProductID = ALL (SELECT ProductID FROM OrderDetails);

which is always empty/ false when subquery is multi-valued like -

SELECT ProductName, ProductID FROM Products WHERE ProductID = ALL (10, 20, 30);

Comments

2

Adding to Adam's reply, be wary that the syntax can be ambiguous:

SELECT b1 = ANY((SELECT b2 FROM t2 ...)) FROM t1 ...;

Here ANY can be considered either as introducing a subquery, or as being an aggregate function, if the subquery returns one row with a Boolean value. (via postgresql.org)

2 Comments

By boolean value, do you mean integer value?
@Pacerier - No no, I mean that if ANY((SELECT b2 FROM t2 ...)) (as in boolean) is true, then true (as in boolean).
1

Sample query that may put some context into this. Let's say we have a database of major league baseball players and we have a database of common Puerto Rican last names. Let's say somebody wanted to see how common Puerto Rican players are on the MLB. They could run the following query:

SELECT mlb_roster.last_name FROM mlb_roster WHERE mlb_roster.last_name = ANY (SELECT common_pr_names.last_name FROM common_pr_names)

What the query is doing here is comparing the last names on the MLB roster and displaying only the ones that are also found on the list of common Puerto Rican names.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.