The ANY subquery
is one that is categorized as a comparison operator and similar to the meaning in English,
it is used to determine when certain data satisfies a criteria compared to what the ANY condition specifies.
When using the ANY
subquery in SQL Server
database, it means that a comparison must be made and for the condition to satisfy the outer query,
the value that introduces your subquery must be greater than at least one of the values listed in the returned results of the subquery.
Cities table:
CITY_ID | NAME | STATE |
1 | New York | New York |
2 | Los Angeles | California |
3 | Chicago | Illinois |
4 | San Antonio | Texas |
5 | San Diego | California |
States table:
STATE_ID | NAME |
1 | Arizona |
2 | California |
3 | Texas |
4 | Michigan |
Any Example:
Find the cities that have the any state in the states table using = any.
SELECT * FROM cities c
WHERE c.state = ANY (SELECT s.name FROM states s );
Result:
CITY_ID | NAME | STATE |
2 | Los Angeles | California |
4 | San Antonio | Texas |
5 | San Diego | California |